1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
| program textmatrix;
uses
SysUtils;
// Matrix properties
const
MaxCol = 5;
MaxRow = 5;
// The Matrix
type
TMatrixCol = array[0..MaxCol] of Char; // no -1 ReadLn need +1 (AZT)
TMatrixRows = array[0..MaxRow-1] of TMatrixCol;
// Fill #0 into a Matrix (reset)
procedure ResetMatrix(var aMatrix: TMatrixRows);
var R, C: integer;
begin
for R := 0 to MaxRow-1 do
for C := 0 to MaxCol-1 do
aMatrix[R, C] := #0;
end;
// reading text file into a Matrix
procedure ReadFile(aFileName: string; var aMatrix: TMatrixRows);
var
F : TextFile;
L : integer;
begin
// assign file
Assign(F, aFileName);
// opening in read
Reset(F);
// init Line counter
L := 0;
// reading rows of file
while (not EOF(F)) or (L = MaxRow) do
begin
ReadLn(F, aMatrix[L]);
// increment Line counter
inc(L);
end;
// close file
Close(F);
end;
// show a Matrix as array
procedure WriteMatrixArray(aMatrix: TMatrixRows);
var R, C: integer;
begin
for R := 0 to MaxRow-1 do
begin
write('[');
for C := 0 to MaxCol-1 do
begin
if C < (MaxCol-1) then
write(aMatrix[R, C], ', ')
else
write(aMatrix[R, C]);
end;
writeln(']');
end;
end;
// show a Matrix as text
procedure WriteMatrix(aMatrix: TMatrixRows);
var R: integer;
begin
for R := 0 to MaxRow-1 do
writeln(aMatrix[R]);
end;
var
Mtx: TMatrixRows;
begin
// reseting Matrix
ResetMatrix(Mtx);
// reading Text file (here: [program name].txt)
ReadFile(changeFileExt(paramStr(0), '.txt'), Mtx);
// show Matrix as array
WriteMatrixArray(Mtx);
Writeln;
// show Matrix as Text
WriteMatrix(Mtx);
// wait
readLn;
end. |
Partager