Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program Project1;
- {$APPTYPE CONSOLE}
- uses
- SysUtils;
- type
- TMatrix = array of array of Integer;
- const
- ErrorMessage = 'Error! N should be natural value from 1 to 2147483647';
- ReadFileError = 'Error! File contains invalid data';
- FileCompositionError = 'Error! Your file is composed incorrectly';
- OutputFileName = 'output.txt';
- NoSuchFileError = 'Error! No such file. ';
- WritePermissionError = 'Error! Chech ''w'' permission of this file';
- procedure TransposeMatrix(var MyMatrix : TMatrix);
- var
- LastIndex, i, j, Temp : Integer;
- begin
- LastIndex := High(MyMatrix);
- for i := 0 to LastIndex do
- for j := i to LastIndex do
- begin
- Temp := MyMatrix[i][j];
- MyMatrix[i][j] := MyMatrix[j][i];
- MyMatrix[j][i] := Temp;
- end;
- end;
- function EnterInputFileName() : string;
- var
- Name : string;
- begin
- Writeln('Enter input file name');
- Readln(Name);
- while not FileExists(Name) do
- begin
- Writeln(NoSuchFileError, 'Input file name again');
- Readln(Name);
- end;
- EnterInputFileName := Name;
- end;
- function EnterOutputFileName() : string;
- var
- Name : string;
- begin
- Writeln('Enter output file name');
- Readln(Name);
- EnterOutputFileName := Name;
- end;
- procedure ReadFile(var Matrix : TMatrix; Order : Integer; var InputFile : TextFile);
- var
- LastIndex, i, j : Integer;
- begin
- SetLength(Matrix, Order, Order);
- LastIndex := Order - 1;
- Reset(InputFile);
- for i := 0 to LastIndex do
- for j := 0 to LastIndex do
- Read(InputFile, Matrix[i][j]);
- end;
- procedure OutputAnswer(Matrix : TMatrix; OutputFileName : string);
- var
- OutputFile : TextFile;
- i, j, LastIndex : Integer;
- begin
- try
- AssignFile(OutputFile, OutputFileName);
- if not FileExists(OutputFileName) then
- Rewrite(OutputFile)
- else
- Append(OutputFile);
- LastIndex := High(Matrix);
- for i := 0 to LastIndex do
- begin
- for j := 0 to LastIndex do
- begin
- Write(OutputFile, Matrix[i][j],' ');
- Write(Matrix[i][j],' ');
- end;
- Writeln(OutputFile, '');
- Writeln('');
- end;
- Writeln(OutputFile, '');
- Writeln('');
- except
- Writeln(WritePermissionError);
- CloseFile(OutputFile);
- end;
- CloseFile(OutputFile);
- end;
- function CheckOrder(var InputFile : TextFile) : Integer;
- var
- Item : Integer;
- AssumedSize, Counter, i : Integer;
- IsValid : Boolean;
- begin
- AssumedSize := 0;
- IsValid := True;
- Reset(InputFile);
- while not EoLN(InputFile) do
- begin
- Read(InputFile, Item);
- Inc(AssumedSize);
- end;
- Readln(InputFile);
- for i := 2 to AssumedSize do
- begin
- Counter := 0;
- while not EoLN(InputFile) do
- begin
- Read(InputFile, Item);
- Inc(Counter);
- end;
- if Counter <> AssumedSize then
- begin
- IsValid := False;
- end;
- Readln(InputFile);
- end;
- if not EoF(InputFile) then
- IsValid := False;
- if IsValid then
- CheckOrder := AssumedSize
- else
- CheckOrder := -1;
- end;
- procedure Main;
- var
- Matrix : TMatrix;
- InputFile : TextFile;
- InputFileName, OutputFileName : string;
- Order : Integer;
- begin
- Writeln('This program can transpose the NxN matrix!');
- try
- InputFileName := EnterInputFileName();
- AssignFile(InputFile, InputFileName);
- Reset(InputFile);
- Order := CheckOrder(InputFile);
- if Order <> -1 then
- begin
- ReadFile(Matrix, Order, InputFile);
- OutputFileName := EnterOutputFileName();
- OutputAnswer(Matrix, OutputFileName);
- TransposeMatrix(Matrix);
- OutputAnswer(Matrix, OutputFileName);
- end
- else
- Writeln(FileCompositionError);
- except
- Writeln(ReadFileError);
- end;
- CloseFile(InputFile);
- Readln;
- end;
- begin
- Main;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement