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 InputN(var N : Integer);
- //var
- // IsCorrect : Boolean;
- // Input : string;
- //begin
- // IsCorrect := False;
- // repeat
- // try
- // Writeln('Enter N - matrix size (value from 1 to ', High(Integer), ')');
- // Readln(N);
- // N := StrToInt(Input);
- // if N > 0 then
- // IsCorrect := True
- // else
- // Writeln(ErrorMessage);
- // except
- // on E : EConvertError do
- // Writeln(ErrorMessage);
- // end;
- // until IsCorrect;
- //end;
- 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;
- procedure PrintMatrix(var MyMatrix : TMatrix);
- var
- LastIndex, i, j : Integer;
- OutputFile : TextFile;
- begin
- try
- AssignFile(OutputFile, OutputFileName);
- Rewrite(OutputFile);
- LastIndex := High(MyMatrix);
- for i := 0 to LastIndex do
- begin
- for j := 0 to LastIndex do
- begin
- Write(MyMatrix[i][j], ' ');
- Write(OutputFile, MyMatrix[i][j], ' ');
- end;
- Writeln('');
- Writeln(OutputFile, '');
- end;
- Writeln('-');
- Writeln(OutputFile, '-');
- except
- Writeln('Error!');
- end;
- CloseFile(OutputFile);
- end;
- //procedure InputPureMatrix(N : Integer; var Matrix : TMatrix);
- //var
- // i, j, Counter, Len, LastIndex: Integer;
- // Name : string;
- // InputFile : TextFile;
- // IsCorrect : Boolean;
- //begin
- // Writeln('Enter file name');
- // Readln(Name);
- // while not FileExists(Name) do
- // begin
- // Writeln('No such file. Input file name again');
- // Readln(Name);
- // end;
- //
- // SetLength(Matrix, N, N);
- // IsCorrect := True;
- // LastIndex := N - 1;
- // try
- // AssignFile(InputFile, 'input.txt');
- // Reset(InputFile);
- //
- // while not EoF(InputFile) and IsCorrect do
- // begin
- // for i := 0 to LastIndex do
- // begin
- // for j := 0 to LastIndex do
- // begin
- // Read(InputFile, Matrix[i][j]);
- // //Write('(', Matrix[i][j], ')');
- // if (EoLN(InputFile)) and not (j = LastIndex) or
- // (i = LastIndex) and (j = LastIndex) and (not EoF(InputFile)) then
- // IsCorrect := False;
- // end;
- // //Writeln('');
- // end;
- // end;
- // if IsCorrect then
- // begin
- // PrintMatrix(Matrix);
- // TransposeMatrix(Matrix);
- // PrintMatrix(Matrix)
- // end
- // else
- // Writeln(FileCompositionError);
- // except
- // Writeln(ReadFileError);
- // end;
- // CloseFile(InputFile);
- //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;
- function ReadFile(var Matrix : TMatrix; var InputFile : TextFile) : Boolean;
- var
- IsValid : Boolean;
- AssumedSize, LastIndex, i, j, Counter, Temp : Integer;
- TempStr : string;
- begin
- Writeln('In ReadFile()');
- IsValid := True;
- AssumedSize := 0;
- SetLength(Matrix, 3, 3);
- while not EoF(InputFile) do
- begin
- Readln(InputFile, TempStr);
- Inc(AssumedSize);
- end;
- j := 0;
- i := 0;
- Counter := 1;
- Reset(InputFile);
- while not EoF(InputFile) and IsValid do
- begin
- for j := 0 to AssumedSize - 1 do
- begin
- Writeln('i=', i, ', j=', j);
- Read(InputFile, Matrix[i][j]);
- Writeln('[', i, '][', j, ']=', Matrix[i][j]);
- if (j <> AssumedSize - 1) and EoLN(InputFile) then
- IsValid := False;
- if (j = AssumedSize - 1) and not EoLN(InputFile) then
- IsValid := False;
- end;
- Inc(i);
- // begin
- // Inc(Counter);
- // Read(InputFile, Matrix[i][j]);
- // Writeln('[', i, '][', j, ']=', Matrix[i][j]);
- //// if(j = 0) then
- //// Inc(AssumedSize);
- // Inc(i);
- // Writeln('end;');
- // end;
- // Readln;
- //
- // Inc(j);
- end;
- Writeln('Assumed size', AssumedSize);
- // while not EoF(InputFile) and IsValid do
- // begin
- // for i := 0 to LastIndex do
- // begin
- // for j := 0 to LastIndex do
- // begin
- // Read(InputFile, Matrix[i][j]);
- // //Write('(', Matrix[i][j], ')');
- // if (EoLN(InputFile)) and not (j = LastIndex) or
- // (i = LastIndex) and (j = LastIndex) and (not EoF(InputFile)) then
- // IsCorrect := False;
- // end;
- // //Writeln('');
- // end;
- // end;
- ReadFile := IsValid;
- end;
- procedure OutputAnswer(Matrix : TMatrix; OutputFileName : string);
- var
- OutputFile : TextFile;
- i, j, LastIndex : Integer;
- begin
- try
- AssignFile(OutputFile, OutputFileName);
- if (FileSize(OutputFile) = 0) or 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
- on E : Exception do
- Writeln(WritePermissionError, E.Message);
- end;
- CloseFile(OutputFile);
- end;
- procedure Main();
- var
- Matrix : TMatrix;
- N, i, j, LastIndex : Integer;
- InputFile : TextFile;
- InputFileName, OutputFileName : string;
- begin
- Writeln('This program can transpose the NxN matrix!');
- try
- InputFileName := EnterInputFileName();
- AssignFile(InputFile, InputFileName);
- Reset(InputFile);
- if(ReadFile(Matrix, InputFile)) then
- begin
- 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