Advertisement
klasscho

Untitled

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