klasscho

Untitled

Dec 3rd, 2019
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.51 KB | None | 0 0
  1. Program Project9;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. uses
  6. SysUtils;
  7.  
  8. type
  9. MatrixType = array of array of Integer;
  10. TArr = array of Real;
  11.  
  12. function SizeOfMatrix(var MyFile: TextFile): Integer;
  13. var
  14. Numb, n, NumbEx, Temp: Integer;
  15. IsCorrect, FirstIt: Boolean;
  16. begin
  17. n := 0;
  18. FirstIt := True;
  19. IsCorrect := True;
  20. while (not Eof(MyFile)) and IsCorrect do
  21. begin
  22. NumbEx := Numb;
  23. Numb := 0;
  24. while not Eoln(MyFile) do
  25. begin
  26. Inc(Numb);
  27. Read(MyFile, Temp);
  28. end;
  29. if NumbEx <> Numb then
  30. IsCorrect := False;
  31. if FirstIt then
  32. begin
  33. IsCorrect := True;
  34. FirstIt := False;
  35. end;
  36. Inc(n);
  37. Readln(MyFile);
  38. end;
  39. if (Numb = n) and IsCorrect then
  40. SizeOfMatrix := n
  41. else
  42. SizeOfMatrix := 0;
  43. Reset(MyFile);
  44. end;
  45.  
  46. procedure ReadFileName(var MyFile: TextFile);
  47. var
  48. FName: string;
  49. CorrectName: Boolean;
  50. begin
  51. Writeln('Enter a file name for data entry in the format Name.txt');
  52. repeat
  53. Readln(FName);
  54. if FileExists(FName) then
  55. CorrectName := True
  56. else
  57. begin
  58. Writeln('The file name was entered incorrectly. Try again. Example: Name.txt');
  59. CorrectName := False;
  60. end;
  61. until CorrectName;
  62. AssignFile(MyFile, FName);
  63. Reset(MyFile);
  64. end;
  65.  
  66. function IsTheMatrixCorrect(var MyFile: TextFile): Boolean;
  67. var
  68. Number: Integer;
  69. TrueMatrix, IsValidInput: Boolean;
  70. begin
  71. TrueMatrix := True;
  72. while (not Eof(MyFile)) do
  73. begin
  74. try
  75. read(MyFile, Number)
  76. except
  77. TrueMatrix := False
  78. end;
  79. end;
  80. if Eof(MyFile) then
  81. IsValidInput := True
  82. else
  83. Writeln('File is empty.');
  84. Reset(MyFile);
  85. IsTheMatrixCorrect := TrueMatrix;
  86. end;
  87.  
  88. procedure ReadMatrix(var Matrixx: MatrixType; var MyFile: TextFile; n: Integer);
  89. var
  90. i, j: Integer;
  91. FName: string;
  92. begin
  93. for i := 0 to n - 1 do
  94. for j := 0 to n - 1 do
  95. Read(MyFile, Matrixx[i, j]);
  96. Reset(MyFile);
  97. end;
  98.  
  99. procedure FindMax(const Matrixx: MatrixType; n, iFMax, jFMax, iSMax, jSMax: Integer);
  100. var
  101. FirstMax, SecondMax, i, j: Integer;
  102. begin
  103. FirstMax := Matrixx[0][0];
  104. iFMax := 0;
  105. jFMax := 0;
  106. for i := 0 to n do
  107. begin
  108. for j := 0 to n do
  109. if (Matrixx[i][j] > FirstMax) then
  110. begin
  111. FirstMax := Matrixx[i][j];
  112. iFMax := i;
  113. jFMax := j;
  114. end;
  115. end;
  116. SecondMax := Matrixx[0][0];
  117. iSMax := 0;
  118. jSMax := 0;
  119. for i := 0 to n do
  120. begin
  121. for j := 0 to n do
  122. if ((Matrixx[i][j] > SecondMax) and((i <> iFMax) or (j <> jFMax))) then
  123. begin
  124. FirstMax := Matrixx[i][j];
  125. iSMax := i;
  126. jSMax := j;
  127. end;
  128. end;
  129.  
  130. end;
  131.  
  132. procedure MatrixOutput(const Matrixx: MatrixType; n: Integer);
  133. var
  134. i, j: Integer;
  135. begin
  136. for i := 0 to n - 1 do
  137. begin
  138. for j := 0 to n - 1 do
  139. Write(Matrixx[i, j], ' ');
  140. Writeln;
  141. end;
  142. Writeln;
  143. end;
  144.  
  145. procedure MaxFindOutput(const Matrixx: MatrixType; n, iFMax, jFMax, iSMax, jSMax: Integer);
  146. var
  147. i, j: Integer;
  148. begin
  149. for i := 0 to n - 1 do
  150. begin
  151. for j := 0 to n - 1 do
  152. Writeln(Matrixx[iFMax][jFMax]);
  153. Writeln(Matrixx[iSMax][jSMax]);
  154. Writeln;
  155. end;
  156. Writeln;
  157. end;
  158.  
  159.  
  160. procedure ReadFileOutputName(var MyFile: TextFile);
  161. var
  162. NewFName: string;
  163. CorrectName: Boolean;
  164. begin
  165. Writeln('Enter a file name for data entry in the format Name.txt');
  166. repeat
  167. Readln(NewFName);
  168. if Copy(NewFName, length(NewFName) - 3, 4) = '.txt' then
  169. CorrectName := True
  170. else
  171. begin
  172. Writeln('The file name was entered incorrectly. Try again. Example: Name.txt');
  173. CorrectName := False;
  174. end;
  175. until CorrectName;
  176. AssignFile(MyFile, NewFName);
  177. Rewrite(MyFile);
  178. end;
  179.  
  180. procedure WriteToFile(const Matrixx: MatrixType; n, iFMax, jFMax, iSMax, jSMax: Integer; var NewFile: TextFile);
  181. var
  182. i: Integer;
  183. begin
  184. for i := 0 to n - 1 do
  185. begin
  186. Write(NewFile, Matrixx[iFMax][jFMax]);
  187. Write(NewFile, Matrixx[iSMax][jSMax]);
  188. Write(NewFile, ' ');
  189. end;
  190. CloseFile(NewFile);
  191. Writeln('Saved to file. ');
  192. end;
  193.  
  194.  
  195. var
  196. Matrixx: MatrixType;
  197. Size, iFirstMax,jFirstMax ,iSecondMax, jSecondMax: Integer;
  198. MyTFile: TextFile;
  199. MyTFileName: string;
  200. CorrectMatr: Boolean;
  201. Mass: TArr;
  202.  
  203. begin
  204. Writeln('This program finds the arithmetic mean of the positive elements of each column of the square matrix.');
  205. repeat
  206. ReadFileName(MyTFile);
  207. CorrectMatr := IsTheMatrixCorrect(MyTFile);
  208. if CorrectMatr = True then
  209. begin
  210. Writeln('Given matrix:');
  211. Size := SizeOfMatrix(MyTFile);
  212. SetLength(Matrixx, Size, Size);
  213. ReadMatrix(Matrixx, MyTFile, Size);
  214. CloseFile(MyTFile);
  215. MatrixOutput(Matrixx, Size);
  216. SetLength(Mass, Size);
  217. FindMax(Matrixx, Size, iFirstMax, iSecondMax, jFirstMax, jSecondMax );
  218. Writeln(' Max elemets are: ');
  219. MaxFindOutPut(Matrixx, Size, iFirstMax, iSecondMax, jFirstMax, jSecondMax);
  220. ReadFileOutputName(MyTFile);
  221. WriteToFile(Matrixx, Size, iFirstMax, iSecondMax, jFirstMax, jSecondMax);
  222. end
  223. else
  224. begin
  225. CorrectMatr := False;
  226. Writeln('The name of the matrix inputs incorrect. Try again. Example: Name.txt');
  227. end;
  228. until CorrectMatr;
  229. Readln;
  230. end.
Advertisement
Add Comment
Please, Sign In to add comment