Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Program Project9;
- {$APPTYPE CONSOLE}
- uses
- SysUtils;
- type
- MatrixType = array of array of Integer;
- TArr = array of Real;
- function SizeOfMatrix(var MyFile: TextFile) : Integer;
- var
- Numb, n, NumbEx, Temp: Integer;
- IsCorrect, FirstIt: Boolean;
- begin
- n := 0;
- FirstIt := True;
- IsCorrect := True;
- while (not Eof(MyFile)) and IsCorrect do
- begin
- NumbEx := Numb;
- Numb := 0;
- while not Eoln(MyFile) do
- begin
- Inc(Numb);
- Read (MyFile, Temp);
- end;
- if NumbEx <> Numb then
- IsCorrect := False;
- if FirstIt then
- begin
- IsCorrect := True;
- FirstIt := False;
- end;
- Inc(n);
- Readln(MyFile);
- end;
- if (Numb = n) and IsCorrect then
- SizeOfMatrix := n
- else
- SizeOfMatrix := 0;
- end;
- procedure ReadFileName(var MyFile: TextFile);
- var
- FName: string;
- CorrectName: boolean;
- begin
- Writeln ('Enter a file name for data entry in the format Name.txt');
- begin
- repeat
- Readln(FName);
- if FileExists(FName) then
- CorrectName := True
- else
- begin
- Writeln ('The file name was entered incorrectly. Try again. Example: Name.txt');
- CorrectName := False;
- end;
- until CorrectName ;
- end;
- end;
- function IsTheMatrixCorrect(FName: string): boolean;
- var
- MyFile: TextFile;
- Number: Integer;
- TrueMatrix, IsValidInput: Boolean;
- begin
- TrueMatrix := True;
- AssignFile(MyFile, FName);
- reset(MyFile);
- while (not EOF(MyFile)) do
- begin
- try
- read(MyFile, Number)
- except
- TrueMatrix := false
- end;
- end;
- AssignFile(MyFile, FName);
- Reset(MyFile);
- if EoF(MyFile) then
- Writeln('File is empty.')
- else
- IsValidInput := True;
- CloseFile(MyFile);
- IsTheMatrixCorrect := TrueMatrix;
- end;
- procedure ReadMatrix(var Matrixx: MatrixType; FName: string; n: integer);
- var
- MyFileInPut: TextFile;
- i, j : Integer;
- Arr: array of array of Integer;
- begin
- AssignFile(MyFileInPut, FName);
- Reset(MyFileInPut);
- for i := 0 to n - 1 do
- for j:=0 to n - 1 do
- Read(myFileInput, Matrixx[i, j]);
- CloseFile(MyFileInPut);
- end;
- function Average(Matrixx: MatrixType; n: integer): TArr ;
- var
- Sum, Amount, i, j: Integer;
- Arr: TArr;
- begin
- SetLength(Arr, n);
- Arr[i] := 0;
- for i := 0 to n - 1 do
- begin
- Sum := 0;
- Amount := 0;
- for j := 0 to n - 1 do
- if MatrixType[j, i] > 0 then
- begin
- Sum := Sum + MatrixType[j, i];
- Inc(Amount);
- end;
- Arr[i] := Sum/Amount;
- end;
- Average := Arr;
- end;
- procedure MatrixOutput(const Matrixx: MatrixType; n: integer);
- var
- i, j: integer;
- begin
- for i := 0 to n do
- begin
- for j := 0 to n do
- write(Matrixx[i, j], ' ');
- writeln;
- end;
- writeln;
- end;
- procedure AverageOutput(Arr: TArr; n: integer);
- var
- i: integer;
- begin
- for i := 0 to n - 1 do
- begin
- write(Arr[i], ' ');
- end;
- writeln;
- end;
- procedure WriteToFile(const Arr: TArr ; FName : string; n: integer);
- var
- NewFile: TextFile;
- i: integer;
- begin
- AssignFile(NewFile, FName);
- rewrite(NewFile);
- for i := 0 to n - 1 do
- begin
- write(NewFile, Arr[i] );
- write(NewFile, ' ');
- end;
- CloseFile(NewFile);
- writeln('Saved to file: ', NewFile);
- end;
- var
- Matrixx: MatrixType;
- Size: Integer;
- FileName: string;
- Avrg: Real;
- MyTFile: TextFile;
- CorrectMatr: Boolean;
- Mass: TArr;
- begin
- Size := SizeOfMatrix(MyTFile);
- ReadFileName(FileName);
- CorrectMatr := IsTheMatrixCorrect(FileName);
- ReadMatrix(Matrixx,FileName, Size);
- Avrg := Average(Matrixx, Size);
- MatrixOutput(Matrixx, Size);
- AverageOutput(Mass, FileName, Size);
- WriteToFile(Mass, FileName, Size);
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement