// Below gives an example in C++ Builder
// "in.txt" is a text file with the same forlder of ~.exe as follows:
// 5 6
// 0 0 0 0 0 0
// 0 1 1 1 1 0
// 0 1 1 1 1 0
// 0 1 1 1 1 0
// 0 0 0 0 0 0
#include <stdio.h>
...
using namespace std; // Add this statement for using "cin"/"cout"
//------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{ int m, n, i, j;
freopen("in.txt", "r", stdin);
// read data in "in.txt" as standard input 將in.txt當成stdin
freopen("out.txt", "w", stdout); // write into file "out.txt"
cin >> m >> n; // get matrix size m*n from in.txt (stdin)
int ** maze;
maze = new int * [m];
for (int i=0; i<m; i++)
{ maze[i] = new int [n];
for (int j=0; j<n; j++)
cin >> maze[i][j]; // 自in.txt (stdin) 讀資料
}
String s; // write data into Form1
for (int i=0; i<m; i++)
{ for (int j=0, s=""; j<n; j++)
s = s + IntToStr(maze[i][j]) + " ";
Form1->Memo1->Lines->Add(s);
}
// write data into file "out.txt"
cout << m << " " << n << endl;
for (i=0; i<m; i++)
{ for (j=0; j<n; j++)
cout << maze[i][j] <<" ";
cout << endl;
}
}
int ** maze; // Or using static memory int maze [100][100];
int m, n;
void __fastcall TForm1::Button1Click(TObject *Sender)
{ FILE *fp;
AnsiString out, fname;
int i, j;
if (OpenDialog1->Execute())
{ fname = OpenDialog1->FileName;
fp = fopen(fname.c_str(), "r+");
fscanf(fp, "%d %d", &m, &n); // Reda integers m & n
m = m_height;
n = m_width;
maze = new int * [m]; // declare 2D array dynamically
for (i=0; i<m; i++) maze[i] = new int [n];
Memo1->Lines->Add("m="+IntToStr(m_height));
Memo1->Lines->Add("n="+IntToStr(m_width));
for (i=0; i<m_height; i++)
for (j=0; j<m_width; j++)
fscanf(fp, "%d", &maze[i][j]);
for (i=0; i<m; i++) // Print maze[][] in Memo1
{ out = "";
for (j=0; j<n; j++) out += " "+IntToStr(maze[i][j]);
Memo1->Lines->Add(out);
}
fclose(fp);
}
else Memo1->Lines->Add("Nothing happens.");
}
// Set up StringGrid1~5 to show the read-in m*n matrix
void __fastcall TForm1::Button2Click(TObject *Sender)
{ int i, j;
int grid_size = Edit3->Text.ToInt();
// Print out maze[][] in StringGrid1
StringGrid1->RowCount = m; StringGrid1->ColCount = n;
for (i=0; i<m; i++)
for (j=0; j<n; j++) StringGrid1->Cells[j][i] = maze[i][j];
StringGrid2->RowCount = m; StringGrid2->ColCount = n;
StringGrid2->DefaultDrawing = false; // Please set it as true to see the difference. (可避免垂直分隔線過粗;StringGrid2-5 請設之!但 StringGrid1 不必 (預設為 true) ,可自行實驗有何分別!)
StringGrid2->FixedCols = 0; StringGrid2->FixedRows = 0;
for (i=0; i<m; i++) StringGrid2->RowHeights[i] = grid_size;
for (i=0; i<n; i++) StringGrid2->ColWidths[i] = grid_size;
for (i=0; i<m; i++)
for (j=0; j<n; j++)
StringGrid2->Cells[j][i] = StringGrid1->Cells[j][i];
StringGrid3->RowCount = m; StringGrid3->ColCount = n;
StringGrid3->DefaultDrawing = false; StringGrid3->FixedCols=0; StringGrid3->FixedRows=0;
for (i=0; i<m; i++) StringGrid3->RowHeights[i] = grid_size;
for (i=0; i<n; i++) StringGrid3->ColWidths[i] = grid_size;
StringGrid4->RowCount = m; StringGrid4->ColCount = n;
StringGrid4->DefaultDrawing = false; StringGrid4->FixedCols=0; StringGrid4->FixedRows=0;
for (i=0; i<m; i++) StringGrid4->RowHeights[i] = grid_size;
for (i=0; i<n; i++) StringGrid4->ColWidths[i] = grid_size;
StringGrid5->RowCount = m; StringGrid5->ColCount = n;
StringGrid5->FixedCols=0; StringGrid5->FixedRows=0;
StringGrid5->GridLineWidth = 0; StringGrid5->DefaultDrawing = false;
for (i=0; i<m; i++) StringGrid5->RowHeights[i] = grid_size;
for (i=0; i<n; i++) StringGrid5->ColWidths[i] = grid_size;
StringGrid2->Refresh();
StringGrid3->Refresh();
StringGrid4->Refresh();
StringGrid5->Refresh();
}
// 對 StringGrid2 做 OnDrawCell 事件的設定,以產生不同的顯示結果!
void __fastcall TForm1::StringGrid2DrawCell(TObject *Sender, int ACol, int ARow, TRect &Rect, TGridDrawState State)
{ StringGrid2->Canvas->Brush->Color = clWhite;
StringGrid2->Canvas->Font->Color = clBlue;
StringGrid2->Canvas->FillRect(Rect);
AnsiString text = StringGrid2->Cells[ACol][ARow];
StringGrid2->Canvas->TextRect(Rect, Rect.Left+8, Rect.Top+8, text);
}
// 對 StringGrid2 做 OnClick 事件的設定
void __fastcall TForm1::StringGrid2Click(TObject *Sender)
{ TRect Recto = StringGrid1->CellRect(StringGrid1->Col, StringGrid1->Row);
int width = Recto.Width(); int height = Recto.Height();
Label1->Caption = "Cell: ["+IntToStr(StringGrid2->Col)+", "+IntToStr(StringGrid2->Row)+"] = "+StringGrid2->Cells[StringGrid2->Col][StringGrid2->Row];;
Label2->Caption = "Size: "+IntToStr(width)+"X"+IntToStr(height);
Label3->Caption = StringGrid1->Cells[StringGrid2->Col][StringGrid2->Row];
}
// StringGrid5 background color (在 Button2 中已設 without GridLine)
void __fastcall TForm1::StringGrid5DrawCell(TObject *Sender, int ACol, int ARow, TRect &Rect, TGridDrawState State)
{ AnsiString text = StringGrid2->Cells[ACol][ARow];
if (text == "0")
StringGrid5->Canvas->Brush->Color = TColor RGB(200, 205, 155) ;
else StringGrid5->Canvas->Brush->Color = TColor RGB(80, 80, 80);
StringGrid5->Canvas->FillRect(Rect);
}
// -----------------------------------------
// ------- 對 StringGrid3 做 OnDrawCell 事件的設定 -----------
void __fastcall TForm1::StringGrid3DrawCell(TObject *Sender, int ACol, int ARow, TRect &Rect, TGridDrawState State)
{ AnsiString text = StringGrid2->Cells[ACol][ARow];
if (text == "0")
StringGrid3->Canvas->Brush->Color = TColor RGB(250, 245, 135);
else
StringGrid3->Canvas->Brush->Color = TColor (RGB(80, 80, 80));
StringGrid3->Canvas->FillRect(Rect);
StringGrid3->Canvas->TextRect(Rect, Rect.Left+6, Rect.Top+8, text);
}
//------- 對 StringGrid4 做 OnDrawCell 設定-------
void __fastcall TForm1::StringGrid4DrawCell(TObject *Sender, int ACol, int ARow, TRect &Rect, TGridDrawState State)
{ AnsiString text = StringGrid2->Cells[ACol][ARow];
if (text == "0")
StringGrid4->Canvas->Brush->Color = TColor RGB(250, 245, 135) ;
else StringGrid4->Canvas->Brush->Color = TColor RGB(80, 80, 80);
StringGrid4->Canvas->FillRect(Rect);
}