Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- { это мой собственный перечислимый тип с видами ошибок и константный массив под него, ну и некоторые константы, которые я юзала
- Type
- TErrors = (CORRECT, INCORRECT_NUMBER, INCORRECT_FILE_DATA, NOT_ENOUGH_FILE_DATA);
- TArrOI = Array Of Integer;
- TMatrix = Array Of TArrOI;
- Const
- DIGITS = ['0' .. '9'];
- NONE = #0;
- BACKSPACE = #8;
- ENTER = #13;
- ERROR_MESSAGE: Array [TErrors] Of String = ('', 'Элемент матрицы введён некорректно.', 'Файл содержит некорректные данные.',
- 'Файл содержит недостаточно данных.');
- MINUS_SIGN = '-';
- EMPTY_STRING = '';
- MIN_VALUE = -1000;
- MAX_VALUE = 1000;
- }
- Procedure ReadMatrixFromFile(Str: String; Var Matrix: TMatrix; Var Error: TErrors);
- Const
- DELIMITER = ' ';
- Var
- I, Row, Col, N, StrInd, ArrInd: Integer;
- Nums: Array Of String;
- Begin
- N := Length(Matrix[0]);
- Error := CORRECT;
- ArrInd := 0;
- SetLength(Nums, Sqr(N));
- For StrInd := 1 To Length(Str) Do
- Begin
- If (Str[StrInd] <> DELIMITER) Then
- Begin
- Nums[ArrInd] := Nums[ArrInd] + Str[StrInd];
- End
- Else
- If (StrInd > 1) And (Str[StrInd - 1] <> DELIMITER) Then
- Inc(ArrInd);
- End;
- If (ArrInd <> Sqr(N) - 1) Then
- Error := NOT_ENOUGH_FILE_DATA;
- If (Error = CORRECT) Then
- Begin
- I := 0;
- Row := 0;
- While (Row < N) And (Error = CORRECT) Do
- Begin
- Col := 0;
- While (Col < N) And (Error = CORRECT) Do
- Begin
- Try
- Matrix[Row, Col] := StrToInt(Nums[I]);
- Except
- Error := INCORRECT_FILE_DATA;
- End;
- If (Error = CORRECT) Then
- Begin
- If (Matrix[Row, Col] < MIN_VALUE) Or (Matrix[Row, Col] > MAX_VALUE) Then
- Error := INCORRECT_FILE_DATA;
- End;
- Inc(Col);
- Inc(I);
- End;
- Inc(Row);
- End;
- End;
- End;
- Procedure CheckDataInFile(Var Error: TErrors; Var Matrix: TMatrix; Path: String);
- Const
- CORRECT_SIZES = [1 .. 10];
- DELIMITER = ' ';
- Var
- Position, I, N, Count: Integer;
- F: TextFile;
- Str, NewStr: String;
- Begin
- Str := EMPTY_STRING;
- AssignFile(F, Path);
- Reset(F);
- While Not EoF(F) Do
- Begin
- Readln(F, NewStr);
- Str := Str + ' ' + Trim(NewStr);
- End;
- Str := Trim(Str);
- If (Str = EMPTY_STRING) Then
- Error := NOT_ENOUGH_FILE_DATA;
- If (Error = CORRECT) Then
- Begin
- Position := Pos(' ', Str);
- I := 1;
- NewStr := EMPTY_STRING;
- While (I < Position) Do
- Begin
- NewStr := NewStr + Str[I];
- Inc(I);
- End;
- Try
- N := StrToInt(NewStr);
- Except
- Error := INCORRECT_FILE_DATA;
- End;
- If (Error = CORRECT) And Not(N In CORRECT_SIZES) Then
- Begin
- Error := INCORRECT_FILE_DATA;
- End;
- If (Error = CORRECT) Then
- Begin
- Delete(Str, 1, Length(NewStr) + 1);
- SetLength(Matrix, N, N);
- ReadMatrixFromFile(Str, Matrix, Error);
- End;
- End;
- CloseFile(F);
- End;
- Procedure TMainForm.OpenMenuItemClick(Sender: TObject);
- Var
- Path: String;
- Error: TErrors;
- Matrix: TMatrix;
- Row, Col: Integer;
- Begin
- Path := EMPTY_STRING;
- Error := CORRECT;
- OpenFileDialog := TOpenTextFileDialog.Create(Self);
- OpenFileDialog.InitialDir := GetCurrentDir;
- OpenFileDialog.Options := [OfFileMustExist];
- OpenFileDialog.Filter := 'Текстовые файлы .txt|*.txt';
- If OpenFileDialog.Execute Then
- Path := OpenFileDialog.FileName;
- OpenFileDialog.Free;
- If (Path <> EMPTY_STRING) Then
- Begin
- CheckDataInFile(Error, Matrix, Path);
- If (Error = CORRECT) Then
- Begin
- DismentionComboBox.ItemIndex := Length(Matrix[0]);
- DrawMatrix(MatrixStringGrid, Length(Matrix[0]));
- For Row := 0 To High(Matrix[0]) Do
- For Col := 0 To High(Matrix[0]) Do
- MatrixStringGrid.Cells[Col + 1, Row + 1] := IntToStr(Matrix[Row, Col]);
- ResultButton.Enabled := True;
- End
- Else
- Application.MessageBox(PChar(ERROR_MESSAGE[Error]), PChar('Ошибка!'), MB_ICONERROR + MB_OK);
- End;
- End;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement