Advertisement
green1ant

2_3 pre_prod

Oct 28th, 2018
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 4.63 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. {
  30. procedure PrintMatrix(var MyMatrix : TMatrix);
  31. var
  32.    LastIndex, i, j : Integer;
  33.    OutputFile : TextFile;
  34. begin
  35.    try
  36.       AssignFile(OutputFile, OutputFileName);
  37.       Rewrite(OutputFile);
  38.       LastIndex := High(MyMatrix);
  39.       for i := 0 to LastIndex do
  40.       begin
  41.          for j := 0 to LastIndex do
  42.          begin
  43.             Write(MyMatrix[i][j], ' ');
  44.             Write(OutputFile, MyMatrix[i][j], ' ');
  45.          end;
  46.          Writeln('');
  47.          Writeln(OutputFile, '');
  48.       end;
  49.       Writeln('-');
  50.       Writeln(OutputFile, '-');
  51.    except
  52.       Writeln(WritePermissionsError);
  53.       CloseFile(OutputFile);
  54.    end;
  55.    CloseFile(OutputFile);
  56. end;
  57. }
  58.  
  59. function EnterInputFileName() : string;
  60. var
  61.    Name : string;
  62. begin
  63.    Writeln('Enter input file name');
  64.    Readln(Name);
  65.    while not FileExists(Name) do
  66.    begin
  67.       Writeln(NoSuchFileError, 'Input file name again');
  68.       Readln(Name);
  69.    end;
  70.    EnterInputFileName := Name;
  71. end;
  72.  
  73. function EnterOutputFileName() : string;
  74. var
  75.    Name : string;
  76. begin
  77.    Writeln('Enter output file name');
  78.    Readln(Name);
  79.    EnterOutputFileName := Name;
  80. end;
  81.  
  82. procedure ReadFile(var Matrix : TMatrix; Order : Integer; var InputFile : TextFile);
  83. var
  84.    LastIndex, i, j : Integer;
  85. begin
  86.    SetLength(Matrix, Order, Order);
  87.    LastIndex := Order - 1;
  88.    Reset(InputFile);
  89.    for i := 0 to LastIndex do
  90.       for j := 0 to LastIndex do
  91.          Read(InputFile, Matrix[i][j]);
  92. end;
  93.  
  94. procedure OutputAnswer(Matrix : TMatrix; OutputFileName : string);
  95. var
  96.    OutputFile : TextFile;
  97.    i, j, LastIndex : Integer;
  98. begin
  99.    try
  100.       AssignFile(OutputFile, OutputFileName);
  101.       if not FileExists(OutputFileName) then
  102.          Rewrite(OutputFile)
  103.       else
  104.          Append(OutputFile);
  105.       LastIndex := High(Matrix);
  106.       for i := 0 to LastIndex do
  107.       begin
  108.          for j := 0 to LastIndex do
  109.          begin
  110.             Write(OutputFile, Matrix[i][j],' ');
  111.             Write(Matrix[i][j],' ');
  112.          end;
  113.          Writeln(OutputFile, '');
  114.          Writeln('');
  115.       end;
  116.       Writeln(OutputFile, '');
  117.       Writeln('');
  118.    except
  119.       Writeln(WritePermissionError);
  120.       CloseFile(OutputFile);
  121.    end;
  122.    CloseFile(OutputFile);
  123. end;
  124.  
  125. function CheckOrder(var InputFile : TextFile) : Integer;
  126. var
  127.    Item : Integer;
  128.    AssumedSize, Counter, i : Integer;
  129.    IsValid : Boolean;
  130. begin
  131.    AssumedSize := 0;
  132.    IsValid := True;
  133.    Reset(InputFile);
  134.    while not EoLN(InputFile) do
  135.    begin
  136.       Read(InputFile, Item);
  137.       Inc(AssumedSize);
  138.    end;
  139.    Readln(InputFile);
  140.    for i := 2 to AssumedSize do
  141.    begin
  142.       Counter := 0;
  143.       while not EoLN(InputFile) do
  144.       begin
  145.          Read(InputFile, Item);
  146.          Inc(Counter);
  147.       end;
  148.       if Counter <> AssumedSize then
  149.       begin
  150.          IsValid := False;
  151.       end;
  152.       Readln(InputFile);
  153.    end;
  154.    if not EoF(InputFile) then
  155.       IsValid := False;
  156.    if IsValid then
  157.       CheckOrder := AssumedSize
  158.    else
  159.       CheckOrder := -1;
  160. end;
  161.  
  162. procedure Main();
  163. var
  164.    Matrix : TMatrix;
  165.    InputFile : TextFile;
  166.    InputFileName, OutputFileName : string;
  167.    Order : Integer;
  168. begin
  169.    Writeln('This program can transpose the NxN matrix!');
  170.    try
  171.       InputFileName := EnterInputFileName();
  172.       AssignFile(InputFile, InputFileName);
  173.       Reset(InputFile);
  174.       Order := CheckOrder(InputFile);
  175.       if Order <> -1 then
  176.       begin
  177.          ReadFile(Matrix, Order, InputFile);
  178.          OutputFileName := EnterOutputFileName();
  179.          OutputAnswer(Matrix, OutputFileName);
  180.          TransposeMatrix(Matrix);
  181.          OutputAnswer(Matrix, OutputFileName);
  182.       end
  183.       else
  184.          Writeln(FileCompositionError);
  185.    except
  186.       Writeln(ReadFileError);
  187.    end;
  188.    CloseFile(InputFile);
  189.    Readln;
  190. end;
  191.  
  192.  
  193. begin
  194.    Main();
  195. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement