Advertisement
green1ant

2_3 d7

Oct 28th, 2018
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 7.24 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 InputN(var N : Integer);
  16. //var
  17. //   IsCorrect : Boolean;
  18. //   Input : string;
  19. //begin
  20. //   IsCorrect := False;
  21. //   repeat
  22. //      try
  23. //         Writeln('Enter N - matrix size (value from 1 to ', High(Integer), ')');
  24. //         Readln(N);
  25. //         N := StrToInt(Input);
  26. //         if N > 0 then
  27. //            IsCorrect := True
  28. //         else
  29. //            Writeln(ErrorMessage);
  30. //      except
  31. //         on E : EConvertError do
  32. //            Writeln(ErrorMessage);
  33. //      end;
  34. //   until IsCorrect;
  35. //end;
  36.  
  37. procedure TransposeMatrix(var MyMatrix : TMatrix);
  38. var
  39.    LastIndex, i, j, Temp : Integer;
  40. begin
  41.    LastIndex := High(MyMatrix);
  42.    for i := 0 to LastIndex do
  43.       for j := i to LastIndex do
  44.       begin
  45.          Temp := MyMatrix[i][j];
  46.          MyMatrix[i][j] := MyMatrix[j][i];
  47.          MyMatrix[j][i] := Temp;
  48.       end;
  49. end;
  50.  
  51. procedure PrintMatrix(var MyMatrix : TMatrix);
  52. var
  53.    LastIndex, i, j : Integer;
  54.    OutputFile : TextFile;
  55. begin
  56.    try
  57.       AssignFile(OutputFile, OutputFileName);
  58.       Rewrite(OutputFile);
  59.       LastIndex := High(MyMatrix);
  60.       for i := 0 to LastIndex do
  61.       begin
  62.          for j := 0 to LastIndex do
  63.          begin
  64.             Write(MyMatrix[i][j], ' ');
  65.             Write(OutputFile, MyMatrix[i][j], ' ');
  66.          end;
  67.          Writeln('');
  68.          Writeln(OutputFile, '');
  69.       end;
  70.       Writeln('-');
  71.       Writeln(OutputFile, '-');
  72.    except
  73.       Writeln('Error!');
  74.    end;
  75.    CloseFile(OutputFile);
  76. end;
  77.  
  78. //procedure InputPureMatrix(N : Integer; var Matrix : TMatrix);
  79. //var
  80. //   i, j, Counter, Len, LastIndex: Integer;
  81. //   Name : string;
  82. //   InputFile : TextFile;
  83. //   IsCorrect : Boolean;
  84. //begin
  85. //   Writeln('Enter file name');
  86. //   Readln(Name);
  87. //   while not FileExists(Name) do
  88. //   begin
  89. //      Writeln('No such file. Input file name again');
  90. //      Readln(Name);
  91. //   end;
  92. //
  93. //   SetLength(Matrix, N, N);
  94. //   IsCorrect := True;
  95. //   LastIndex := N - 1;
  96. //   try
  97. //      AssignFile(InputFile, 'input.txt');
  98. //      Reset(InputFile);
  99. //
  100. //      while not EoF(InputFile) and IsCorrect do
  101. //      begin
  102. //         for i := 0 to LastIndex do
  103. //         begin
  104. //            for j := 0 to LastIndex do
  105. //            begin
  106. //               Read(InputFile, Matrix[i][j]);
  107. //               //Write('(', Matrix[i][j], ')');
  108. //               if (EoLN(InputFile)) and not (j = LastIndex) or
  109. //                 (i = LastIndex) and (j = LastIndex) and (not EoF(InputFile)) then
  110. //                  IsCorrect := False;
  111. //            end;
  112. //            //Writeln('');
  113. //         end;
  114. //      end;
  115. //      if IsCorrect then
  116. //      begin
  117. //         PrintMatrix(Matrix);
  118. //         TransposeMatrix(Matrix);
  119. //         PrintMatrix(Matrix)
  120. //      end
  121. //      else
  122. //         Writeln(FileCompositionError);
  123. //   except
  124. //      Writeln(ReadFileError);
  125. //   end;
  126. //   CloseFile(InputFile);
  127. //end;
  128.  
  129. function EnterInputFileName() : string;
  130. var
  131.    Name : string;
  132. begin
  133.    Writeln('Enter input file name');
  134.    Readln(Name);
  135.    while not FileExists(Name) do
  136.    begin
  137.       Writeln(NoSuchFileError, 'Input file name again');
  138.       Readln(Name);
  139.    end;
  140.    EnterInputFileName := Name;
  141. end;
  142.  
  143. function EnterOutputFileName() : string;
  144. var
  145.    Name : string;
  146. begin
  147.    Writeln('Enter output file name');
  148.    Readln(Name);
  149.    EnterOutputFileName := Name;
  150. end;
  151.  
  152. function ReadFile(var Matrix : TMatrix; var InputFile : TextFile) : Boolean;
  153. var
  154.    IsValid : Boolean;
  155.    AssumedSize, LastIndex, i, j, Counter, Temp : Integer;
  156.    TempStr : string;
  157. begin
  158.    Writeln('In ReadFile()');
  159.    IsValid := True;
  160.    AssumedSize := 0;
  161.    SetLength(Matrix, 3, 3);
  162.  
  163.    while not EoF(InputFile) do
  164.    begin
  165.       Readln(InputFile, TempStr);
  166.       Inc(AssumedSize);
  167.    end;
  168.  
  169.  
  170.    j := 0;
  171.    i := 0;
  172.    Counter := 1;
  173.    Reset(InputFile);
  174.    while not EoF(InputFile) and IsValid do
  175.    begin
  176.  
  177.       for j := 0 to AssumedSize - 1 do
  178.       begin
  179.          Writeln('i=', i, ', j=', j);
  180.  
  181.          Read(InputFile, Matrix[i][j]);
  182.          Writeln('[', i, '][', j, ']=', Matrix[i][j]);
  183.          if (j <> AssumedSize - 1) and EoLN(InputFile) then
  184.             IsValid := False;
  185.          if (j = AssumedSize - 1) and not EoLN(InputFile) then
  186.             IsValid := False;
  187.       end;
  188.  
  189.       Inc(i);
  190.  
  191. //      begin
  192. //         Inc(Counter);
  193. //         Read(InputFile, Matrix[i][j]);
  194. //         Writeln('[', i, '][', j, ']=', Matrix[i][j]);
  195. ////         if(j = 0) then
  196. ////            Inc(AssumedSize);
  197. //         Inc(i);
  198. //         Writeln('end;');
  199. //      end;
  200. //      Readln;
  201. //
  202. //      Inc(j);
  203.    end;
  204.  
  205.    Writeln('Assumed size', AssumedSize);
  206.  
  207. //   while not EoF(InputFile) and IsValid do
  208. //      begin
  209. //         for i := 0 to LastIndex do
  210. //         begin
  211. //            for j := 0 to LastIndex do
  212. //            begin
  213. //               Read(InputFile, Matrix[i][j]);
  214. //               //Write('(', Matrix[i][j], ')');
  215. //               if (EoLN(InputFile)) and not (j = LastIndex) or
  216. //                 (i = LastIndex) and (j = LastIndex) and (not EoF(InputFile)) then
  217. //                  IsCorrect := False;
  218. //            end;
  219. //            //Writeln('');
  220. //         end;
  221. //      end;
  222.    ReadFile := IsValid;
  223. end;
  224.  
  225. procedure OutputAnswer(Matrix : TMatrix; OutputFileName : string);
  226. var
  227.    OutputFile : TextFile;
  228.    i, j, LastIndex : Integer;
  229. begin
  230.  
  231.    try
  232.       AssignFile(OutputFile, OutputFileName);
  233.       if (FileSize(OutputFile) = 0) or not FileExists(OutputFileName) then
  234.          Rewrite(OutputFile)
  235.       else
  236.          Append(OutputFile);
  237.  
  238.       LastIndex := High(Matrix);
  239.       for i := 0 to LastIndex do
  240.       begin
  241.          for j := 0 to LastIndex do
  242.          begin
  243.             Write(OutputFile, Matrix[i][j],' ');
  244.             Write(Matrix[i][j],' ');
  245.          end;
  246.          Writeln(OutputFile, '');
  247.          Writeln('');
  248.       end;
  249.       Writeln(OutputFile, '');
  250.       Writeln('');
  251.    except
  252.       on E : Exception do
  253.          Writeln(WritePermissionError, E.Message);
  254.    end;
  255.    CloseFile(OutputFile);
  256. end;
  257.  
  258. procedure Main();
  259. var
  260.    Matrix : TMatrix;
  261.    N, i, j, LastIndex : Integer;
  262.    InputFile : TextFile;
  263.    InputFileName, OutputFileName : string;
  264. begin
  265.    Writeln('This program can transpose the NxN matrix!');
  266.    try
  267.       InputFileName := EnterInputFileName();
  268.       AssignFile(InputFile, InputFileName);
  269.       Reset(InputFile);
  270.       if(ReadFile(Matrix, InputFile)) then
  271.       begin
  272.          OutputFileName := EnterOutputFileName();
  273.          OutputAnswer(Matrix, OutputFileName);
  274.          TransposeMatrix(Matrix);
  275.          OutputAnswer(Matrix, OutputFileName);
  276.       end
  277.       else
  278.          Writeln(FileCompositionError);
  279.    except
  280.       Writeln(ReadFileError);
  281.    end;
  282.    CloseFile(InputFile);
  283.    Readln;
  284. end;
  285.  
  286.  
  287. begin
  288.    Main();
  289. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement