Advertisement
okrlbok

тутуту

Feb 12th, 2024
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 KB | None | 0 0
  1. { это мой собственный перечислимый тип с видами ошибок и константный массив под него, ну и некоторые константы, которые я юзала
  2. Type
  3. TErrors = (CORRECT, INCORRECT_NUMBER, INCORRECT_FILE_DATA, NOT_ENOUGH_FILE_DATA);
  4. TArrOI = Array Of Integer;
  5. TMatrix = Array Of TArrOI;
  6.  
  7. Const
  8. DIGITS = ['0' .. '9'];
  9. NONE = #0;
  10. BACKSPACE = #8;
  11. ENTER = #13;
  12. ERROR_MESSAGE: Array [TErrors] Of String = ('', 'Элемент матрицы введён некорректно.', 'Файл содержит некорректные данные.',
  13. 'Файл содержит недостаточно данных.');
  14. MINUS_SIGN = '-';
  15. EMPTY_STRING = '';
  16. MIN_VALUE = -1000;
  17. MAX_VALUE = 1000;
  18. }
  19.  
  20. Procedure ReadMatrixFromFile(Str: String; Var Matrix: TMatrix; Var Error: TErrors);
  21. Const
  22. DELIMITER = ' ';
  23. Var
  24. I, Row, Col, N, StrInd, ArrInd: Integer;
  25. Nums: Array Of String;
  26. Begin
  27. N := Length(Matrix[0]);
  28. Error := CORRECT;
  29. ArrInd := 0;
  30. SetLength(Nums, Sqr(N));
  31. For StrInd := 1 To Length(Str) Do
  32. Begin
  33. If (Str[StrInd] <> DELIMITER) Then
  34. Begin
  35. Nums[ArrInd] := Nums[ArrInd] + Str[StrInd];
  36. End
  37. Else
  38. If (StrInd > 1) And (Str[StrInd - 1] <> DELIMITER) Then
  39. Inc(ArrInd);
  40. End;
  41. If (ArrInd <> Sqr(N) - 1) Then
  42. Error := NOT_ENOUGH_FILE_DATA;
  43. If (Error = CORRECT) Then
  44. Begin
  45. I := 0;
  46. Row := 0;
  47. While (Row < N) And (Error = CORRECT) Do
  48. Begin
  49. Col := 0;
  50. While (Col < N) And (Error = CORRECT) Do
  51. Begin
  52. Try
  53. Matrix[Row, Col] := StrToInt(Nums[I]);
  54. Except
  55. Error := INCORRECT_FILE_DATA;
  56. End;
  57. If (Error = CORRECT) Then
  58. Begin
  59. If (Matrix[Row, Col] < MIN_VALUE) Or (Matrix[Row, Col] > MAX_VALUE) Then
  60. Error := INCORRECT_FILE_DATA;
  61. End;
  62. Inc(Col);
  63. Inc(I);
  64. End;
  65. Inc(Row);
  66. End;
  67. End;
  68. End;
  69.  
  70. Procedure CheckDataInFile(Var Error: TErrors; Var Matrix: TMatrix; Path: String);
  71. Const
  72. CORRECT_SIZES = [1 .. 10];
  73. DELIMITER = ' ';
  74. Var
  75. Position, I, N, Count: Integer;
  76. F: TextFile;
  77. Str, NewStr: String;
  78. Begin
  79. Str := EMPTY_STRING;
  80. AssignFile(F, Path);
  81. Reset(F);
  82. While Not EoF(F) Do
  83. Begin
  84. Readln(F, NewStr);
  85. Str := Str + ' ' + Trim(NewStr);
  86. End;
  87. Str := Trim(Str);
  88. If (Str = EMPTY_STRING) Then
  89. Error := NOT_ENOUGH_FILE_DATA;
  90. If (Error = CORRECT) Then
  91. Begin
  92. Position := Pos(' ', Str);
  93. I := 1;
  94. NewStr := EMPTY_STRING;
  95. While (I < Position) Do
  96. Begin
  97. NewStr := NewStr + Str[I];
  98. Inc(I);
  99. End;
  100. Try
  101. N := StrToInt(NewStr);
  102. Except
  103. Error := INCORRECT_FILE_DATA;
  104. End;
  105. If (Error = CORRECT) And Not(N In CORRECT_SIZES) Then
  106. Begin
  107. Error := INCORRECT_FILE_DATA;
  108. End;
  109. If (Error = CORRECT) Then
  110. Begin
  111. Delete(Str, 1, Length(NewStr) + 1);
  112. SetLength(Matrix, N, N);
  113. ReadMatrixFromFile(Str, Matrix, Error);
  114. End;
  115. End;
  116. CloseFile(F);
  117. End;
  118.  
  119. Procedure TMainForm.OpenMenuItemClick(Sender: TObject);
  120. Var
  121. Path: String;
  122. Error: TErrors;
  123. Matrix: TMatrix;
  124. Row, Col: Integer;
  125. Begin
  126. Path := EMPTY_STRING;
  127. Error := CORRECT;
  128. OpenFileDialog := TOpenTextFileDialog.Create(Self);
  129. OpenFileDialog.InitialDir := GetCurrentDir;
  130. OpenFileDialog.Options := [OfFileMustExist];
  131. OpenFileDialog.Filter := 'Текстовые файлы .txt|*.txt';
  132. If OpenFileDialog.Execute Then
  133. Path := OpenFileDialog.FileName;
  134. OpenFileDialog.Free;
  135. If (Path <> EMPTY_STRING) Then
  136. Begin
  137. CheckDataInFile(Error, Matrix, Path);
  138. If (Error = CORRECT) Then
  139. Begin
  140. DismentionComboBox.ItemIndex := Length(Matrix[0]);
  141. DrawMatrix(MatrixStringGrid, Length(Matrix[0]));
  142. For Row := 0 To High(Matrix[0]) Do
  143. For Col := 0 To High(Matrix[0]) Do
  144. MatrixStringGrid.Cells[Col + 1, Row + 1] := IntToStr(Matrix[Row, Col]);
  145. ResultButton.Enabled := True;
  146. End
  147. Else
  148. Application.MessageBox(PChar(ERROR_MESSAGE[Error]), PChar('Ошибка!'), MB_ICONERROR + MB_OK);
  149. End;
  150. End;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement