Advertisement
green1ant

2_3 pre_prod

Oct 28th, 2018
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 3.94 KB | None | 0 0
  1. program Project1;
  2. {$APPTYPE CONSOLE}
  3. uses
  4.    SysUtils;
  5. type
  6.    TMatrix = array of array of Integer;
  7. const
  8.    ErrorMessage = 'Error! N should be natural value from 1 to 2147483647';
  9.    ReadFileError = 'Error! File contains invalid data';
  10.    FileCompositionError = 'Error! Your file is composed incorrectly';
  11.    OutputFileName = 'output.txt';
  12.    NoSuchFileError = 'Error! No such file. ';
  13.    WritePermissionError = 'Error! Chech ''w'' permission of this file';
  14.  
  15. procedure TransposeMatrix(var MyMatrix : TMatrix);
  16. var
  17.    LastIndex, i, j, Temp : Integer;
  18. begin
  19.    LastIndex := High(MyMatrix);
  20.    for i := 0 to LastIndex do
  21.       for j := i to LastIndex do
  22.       begin
  23.          Temp := MyMatrix[i][j];
  24.          MyMatrix[i][j] := MyMatrix[j][i];
  25.          MyMatrix[j][i] := Temp;
  26.       end;
  27. end;
  28.  
  29. function EnterInputFileName() : string;
  30. var
  31.    Name : string;
  32. begin
  33.    Writeln('Enter input file name');
  34.    Readln(Name);
  35.    while not FileExists(Name) do
  36.    begin
  37.       Writeln(NoSuchFileError, 'Input file name again');
  38.       Readln(Name);
  39.    end;
  40.    EnterInputFileName := Name;
  41. end;
  42.  
  43. function EnterOutputFileName() : string;
  44. var
  45.    Name : string;
  46. begin
  47.    Writeln('Enter output file name');
  48.    Readln(Name);
  49.    EnterOutputFileName := Name;
  50. end;
  51.  
  52. procedure ReadFile(var Matrix : TMatrix; Order : Integer; var InputFile : TextFile);
  53. var
  54.    LastIndex, i, j : Integer;
  55. begin
  56.    SetLength(Matrix, Order, Order);
  57.    LastIndex := Order - 1;
  58.    Reset(InputFile);
  59.    for i := 0 to LastIndex do
  60.       for j := 0 to LastIndex do
  61.          Read(InputFile, Matrix[i][j]);
  62. end;
  63.  
  64. procedure OutputAnswer(Matrix : TMatrix; OutputFileName : string);
  65. var
  66.    OutputFile : TextFile;
  67.    i, j, LastIndex : Integer;
  68. begin
  69.    try
  70.       AssignFile(OutputFile, OutputFileName);
  71.       if not FileExists(OutputFileName) then
  72.          Rewrite(OutputFile)
  73.       else
  74.          Append(OutputFile);
  75.       LastIndex := High(Matrix);
  76.       for i := 0 to LastIndex do
  77.       begin
  78.          for j := 0 to LastIndex do
  79.          begin
  80.             Write(OutputFile, Matrix[i][j],' ');
  81.             Write(Matrix[i][j],' ');
  82.          end;
  83.          Writeln(OutputFile, '');
  84.          Writeln('');
  85.       end;
  86.       Writeln(OutputFile, '');
  87.       Writeln('');
  88.    except
  89.       Writeln(WritePermissionError);
  90.       CloseFile(OutputFile);
  91.    end;
  92.    CloseFile(OutputFile);
  93. end;
  94.  
  95. function CheckOrder(var InputFile : TextFile) : Integer;
  96. var
  97.    Item : Integer;
  98.    AssumedSize, Counter, i : Integer;
  99.    IsValid : Boolean;
  100. begin
  101.    AssumedSize := 0;
  102.    IsValid := True;
  103.    Reset(InputFile);
  104.    while not EoLN(InputFile) do
  105.    begin
  106.       Read(InputFile, Item);
  107.       Inc(AssumedSize);
  108.    end;
  109.    Readln(InputFile);
  110.    for i := 2 to AssumedSize do
  111.    begin
  112.       Counter := 0;
  113.       while not EoLN(InputFile) do
  114.       begin
  115.          Read(InputFile, Item);
  116.          Inc(Counter);
  117.       end;
  118.       if Counter <> AssumedSize then
  119.       begin
  120.          IsValid := False;
  121.       end;
  122.       Readln(InputFile);
  123.    end;
  124.    if not EoF(InputFile) then
  125.       IsValid := False;
  126.    if IsValid then
  127.       CheckOrder := AssumedSize
  128.    else
  129.       CheckOrder := -1;
  130. end;
  131.  
  132. procedure Main;
  133. var
  134.    Matrix : TMatrix;
  135.    InputFile : TextFile;
  136.    InputFileName, OutputFileName : string;
  137.    Order : Integer;
  138. begin
  139.    Writeln('This program can transpose the NxN matrix!');
  140.    try
  141.       InputFileName := EnterInputFileName();
  142.       AssignFile(InputFile, InputFileName);
  143.       Reset(InputFile);
  144.       Order := CheckOrder(InputFile);
  145.       if Order <> -1 then
  146.       begin
  147.          ReadFile(Matrix, Order, InputFile);
  148.          OutputFileName := EnterOutputFileName();
  149.          OutputAnswer(Matrix, OutputFileName);
  150.          TransposeMatrix(Matrix);
  151.          OutputAnswer(Matrix, OutputFileName);
  152.       end
  153.       else
  154.          Writeln(FileCompositionError);
  155.    except
  156.       Writeln(ReadFileError);
  157.    end;
  158.    CloseFile(InputFile);
  159.    Readln;
  160. end;
  161.  
  162.  
  163. begin
  164.    Main;
  165. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement