Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.94 KB | None | 0 0
  1. program Project1;
  2.  
  3. uses
  4. System.SysUtils;
  5.  
  6. type
  7. TMatrix = array of array of Shortint;
  8. TVector = array of Integer;
  9. TChoiceInput = (Randomly, FromConsole, FromFile);
  10. TChoiceOutput = (ToFile, ToConsole);
  11.  
  12. function Confirm(): Boolean;
  13. var
  14. IsCorrect: Boolean;
  15. Input: Char;
  16. begin
  17. Writeln('Enter Y if Yes and N if No');
  18. repeat
  19. IsCorrect := True;
  20. Readln(Input);
  21. Input := UpCase(Input);
  22. if not (Input in ['Y', 'N']) then
  23. begin
  24. IsCorrect := False;
  25. Writeln('Invalid input! Please, try again:');
  26. end;
  27. until IsCorrect;
  28. Confirm := Input = 'Y';
  29. end;
  30.  
  31. function GetInputWay(): TChoiceInput;
  32. var
  33. Input: Char;
  34. IsCorrect: Boolean;
  35. begin
  36. repeat
  37. IsCorrect := True;
  38. Readln(Input);
  39. if not (Input in ['1'..'3']) then
  40. begin
  41. IsCorrect := False;
  42. Writeln('Invalid answer! Please, try again:');
  43. end
  44. else
  45. case Input of
  46. '1' : GetInputWay := Randomly;
  47. '2' : GetInputWay := FromConsole;
  48. '3' : GetInputWay := FromFile;
  49. end;
  50. until IsCorrect;
  51. end;
  52.  
  53. function GetOutputWay(): TChoiceOutput;
  54. var
  55. Input: Char;
  56. IsCorrect: Boolean;
  57. begin
  58. repeat
  59. IsCorrect := True;
  60. Readln(Input);
  61. if not (Input in ['1'..'2']) then
  62. begin
  63. IsCorrect := False;
  64. Writeln('Invalid answer! Please, try again:');
  65. end
  66. else
  67. case Input of
  68. '1' : GetOutputWay := ToFile;
  69. '2' : GetOutputWay := ToConsole;
  70. end;
  71. until IsCorrect;
  72. end;
  73.  
  74. function GetFileName(): string;
  75. var
  76. FileName: string;
  77. IsCorrect: Boolean;
  78. begin
  79. Writeln('Enter the name of your file:');
  80. repeat
  81. IsCorrect := True;
  82. Readln(FileName);
  83. if not FileExists(FileName) then
  84. begin
  85. IsCorrect := False;
  86. Writeln('Invalid input or file doesn''t exist! Please, try again:');
  87. end;
  88. until IsCorrect;
  89. GetFileName := FileName;
  90. end;
  91.  
  92. function GetMatrixDim(): Integer;
  93. const
  94. MaxDim = 100;
  95. MinDim = 1;
  96. var
  97. Dim: Integer;
  98. IsError: Boolean;
  99. begin
  100. Writeln('Enter the dimension of your matrix:');
  101. repeat
  102. IsError := False;
  103. try
  104. Readln(Dim);
  105. if (Dim > MaxDim) or (Dim < MinDim) then
  106. begin
  107. IsError := True;
  108. Writeln('Invalid range! Please, try again:');
  109. end;
  110. except
  111. IsError := True;
  112. Writeln('Invalid input! Please, try again:');
  113. end;
  114. until not IsError;
  115. GetMatrixDim := Dim;
  116. end;
  117.  
  118. function RandomMatrix(Matrix: TMatrix): TMatrix;
  119. var
  120. i, j: Integer;
  121. begin
  122. Randomize;
  123. for i := 0 to High(Matrix) do
  124. for j := 0 to High(Matrix) do
  125. Matrix[i, j] := - 128 + Random(257);
  126. RandomMatrix := Matrix;
  127. end;
  128.  
  129. function FillMatrixConsole(Matrix: TMatrix): TMatrix;
  130. const
  131. MaxValue = 127;
  132. MinValue = -128;
  133. var
  134. i, j: Integer;
  135. IsCorrect: Boolean;
  136. begin
  137. Writeln('Enter the values of matrix''s elements (integer from [', MinValue, '..', MaxValue, ']):');
  138. for i := 0 to High(Matrix) do
  139. for j := 0 to High(Matrix) do
  140. repeat
  141. IsCorrect := True;
  142. try
  143. Readln(Matrix[i, j]);
  144. if (Matrix[i, j] > MaxValue) or (Matrix[i, j] < MinValue) then
  145. begin
  146. IsCorrect := False;
  147. Writeln('Invalid range! Please, try again:');
  148. end;
  149. except
  150. IsCorrect := False;
  151. Writeln('Invalid input! Please, repeat:');
  152. end;
  153. until IsCorrect;
  154. FillMatrixConsole := Matrix;
  155. end;
  156.  
  157. function ReadMatrixFile(FileName: string; var Error: Boolean): TMatrix;
  158. const
  159. MaxDim = 100;
  160. MinDim = 1;
  161. MaxValue = 127;
  162. MinValue = -128;
  163. var
  164. Matrix: TMatrix;
  165. i, j: Integer;
  166. DataFile: TextFile;
  167. Dim: Integer;
  168. begin
  169. Error := False;
  170. try
  171. AssignFile(DataFile, FileName);
  172. Reset(DataFile);
  173. Read(DataFile, Dim);
  174. if (Dim < MinDim) or (Dim > MaxDim) then
  175. begin
  176. Error := True;
  177. Writeln('Dimension out of range, try again!');
  178. end
  179. else
  180. begin
  181. i := 0;
  182. SetLength(Matrix, Dim, Dim);
  183. while (i < Length(Matrix)) and not Error do
  184. begin
  185. j := 0;
  186. while (j < Length(Matrix)) and not Error do
  187. begin
  188. Read(DataFile, Matrix[i, j]);
  189. if (Matrix[i, j] > MaxValue) or (Matrix[i, j] < MinValue) then
  190. begin
  191. Error := True;
  192. Writeln('Values are out of range, try again:');
  193. end
  194. else
  195. Inc(j);
  196. end;
  197. Inc(i);
  198. end;
  199. end;
  200. except
  201. Error := True;
  202. Writeln('Invalid file data or file not available! Plese, try again:');
  203. end;
  204. CloseFile(DataFile);
  205. ReadMatrixFile := Matrix;
  206. end;
  207.  
  208. function GetMatrixFile(): TMatrix;
  209. var
  210. IsError: Boolean;
  211. FileName: string;
  212. begin
  213. Writeln('Create txt file with the values of matrix dimension and elements.');
  214. repeat
  215. FileName := GetFileName;
  216. GetMatrixFile := ReadMatrixFIle(FileName, IsError);
  217. until not IsError;
  218. end;
  219.  
  220. function BuiltVector(Matrix: TMatrix): TVector;
  221. var
  222. i, j: Integer;
  223. TempVector: TVector;
  224. begin
  225. Setlength(TempVector, Length(Matrix));
  226. for i := 0 to High(Matrix) do
  227. begin
  228. TempVector[i] := Matrix[i, 0];
  229. for j := 1 to High(Matrix) do
  230. if Matrix[i, j] > TempVector[i] then
  231. TempVector[i] := Matrix[i, j];
  232. end;
  233. BuiltVector := TempVector;
  234. end;
  235.  
  236. procedure WriteVectorToFile(Vector: TVector; FileName: string; var Error: Boolean);
  237. var
  238. i: Byte;
  239. OutputFile: TextFile;
  240. begin
  241. try
  242. Error := False;
  243. AssignFile(OutputFile, FileName);
  244. Rewrite(OutputFile);
  245. for i := 0 to High(Vector) do
  246. Writeln(OutputFile, Vector[i]);
  247. except
  248. Error := True;
  249. Writeln('An error occurred, try again:');
  250. end;
  251. CloseFile(OutputFile);
  252. end;
  253.  
  254. procedure OutputVectorFile(Vector: TVector);
  255. var
  256. IsError: Boolean;
  257. FileName: string;
  258. begin
  259. Writeln('Create file for output and enter it''s name:');
  260. repeat
  261. FileName := GetFileName;
  262. WriteVectorToFile(Vector, FileName, IsError);
  263. until not IsError;
  264. Writeln('Answer is successfully saved in your file!');
  265. end;
  266.  
  267. procedure OutputConsole(Vector: TVector);
  268. var
  269. i: Byte;
  270. begin
  271. Writeln('Vector, built of max values:');
  272. for i := 0 to High(Vector) do
  273. Writeln(Vector[i]);
  274. end;
  275.  
  276. procedure Main();
  277. var
  278. MainMatrix: TMatrix;
  279. MainVector: TVector;
  280. InputWay: TChoiceInput;
  281. OutputWay: TChoiceOutput;
  282. MatrixDim: Integer;
  283. begin
  284. Writeln('Choose how do you want to fill up the matrix:');
  285. Writeln('1.Randomly');
  286. Writeln('2.From console');
  287. Writeln('3.From file');
  288. InputWay := GetInputWay;
  289. if InputWay = Randomly then
  290. begin
  291. MatrixDim := GetMatrixDim;
  292. SetLength(MainMatrix, MatrixDim, MatrixDim);
  293. MainMatrix := RandomMatrix(MainMatrix);
  294. end
  295. else
  296. if InputWay = FromConsole then
  297. begin
  298. MatrixDim := GetMatrixDim;
  299. SetLength(MainMatrix, MatrixDim, MatrixDim);
  300. MainMatrix := FillMatrixConsole(MainMatrix);
  301. end
  302. else
  303. if InputWay = FromFile then
  304. begin
  305. MainMatrix := GetMatrixFile;
  306. MatrixDim := Length(MainMatrix);
  307. end;
  308. MainVector := BuiltVector(MainMatrix);
  309. Writeln('Choose how do you want to output data: ');
  310. Writeln('1.To File');
  311. Writeln('2.To Console');
  312. OutputWay := GetOutputWay;
  313. if OutputWay = ToFile then
  314. begin
  315. OutputVectorFile(MainVector);
  316. OutputConsole(MainVector);
  317. end
  318. else
  319. if OutputWay = ToConsole then
  320. OutputConsole(MainVector);
  321. end;
  322.  
  323. begin
  324. Main;
  325. Readln;
  326. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement