Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Unit UnitMain;
- Interface
- Uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Grids, Vcl.StdCtrls, Vcl.ComCtrls,
- Vcl.Menus, Vcl.ExtCtrls;
- Type
- TMatrix = Array Of Array Of Integer;
- TfrmForm = Class(TForm)
- pBack: TPanel;
- btConfirmMatrix: TButton;
- btConfirmSize: TButton;
- btFindDeterminant: TButton;
- eSize: TEdit;
- lbMatrixRequirement: TLabel;
- lbSizeRequierement: TLabel;
- lbTaskInfo: TLabel;
- lbWelcome: TLabel;
- mmMainMenu: TMainMenu;
- miFileMenu: TMenuItem;
- miOpenFromFile: TMenuItem;
- miSaveToFile: TMenuItem;
- miInfoAboutDeveloper: TMenuItem;
- miInstruction: TMenuItem;
- pumPopupMenu: TPopupMenu;
- strgrMatrix: TStringGrid;
- svdSaveToFileDialog: TSaveDialog;
- opdOpenFromFileDialog: TOpenDialog;
- lbAnswer: TLabel;
- lbFinalResult: TLabel;
- Procedure FormCloseQuery(Sender: TObject; Var CanClose: Boolean);
- Procedure FormCreate(Sender: TObject);
- Procedure miInfoAboutDeveloperClick(Sender: TObject);
- Procedure miInstructionClick(Sender: TObject);
- Procedure miOpenFromFileClick(Sender: TObject);
- Procedure eSizeChange(Sender: TObject);
- Procedure eSizeKeyDown(Sender: TObject; Var Key: Word; Shift: TShiftState);
- Procedure eSizeKeyPress(Sender: TObject; Var Key: Char);
- Procedure btConfirmMatrixClick(Sender: TObject);
- Procedure btConfirmSizeClick(Sender: TObject);
- Procedure strgrMatrixKeyDown(Sender: TObject; Var Key: Word;
- Shift: TShiftState);
- Procedure strgrMatrixKeyPress(Sender: TObject; Var Key: Char);
- Procedure btFindDeterminantClick(Sender: TObject);
- Procedure miSaveToFileClick(Sender: TObject);
- End;
- Var
- frmForm: TfrmForm;
- PreviousSize: Integer;
- InputMatrix: TMatrix;
- Implementation
- {$R *.dfm}
- Function IsFileCorrect(Path: String): Boolean;
- Const
- MIN_SIZE = 1;
- MAX_SIZE = 10;
- MIN_VALUE = -99;
- MAX_VALUE = 99;
- Var
- InputFile: TextFile;
- IsCorrect: Boolean;
- Size, MatrixSize, I, J: Integer;
- Matrix: TMatrix;
- Begin
- AssignFile(InputFile, Path);
- IsCorrect := True;
- Try
- Try
- Reset(InputFile);
- Readln(InputFile, Size);
- If (Size < MIN_SIZE) Or (Size > MAX_SIZE) Then
- IsCorrect := False
- Else
- Begin
- MatrixSize := Size;
- SetLength(Matrix, MatrixSize, MatrixSize);
- I := 0;
- While (IsCorrect) And (I < MatrixSize) Do
- Begin
- J := 0;
- While (IsCorrect) And (J < MatrixSize) Do
- Begin
- Read(InputFile, Matrix[I, J]);
- If (Matrix[I, J] < MIN_VALUE) Or (Matrix[I, J] > MAX_VALUE) Then
- IsCorrect := False;
- Inc(J);
- End;
- Inc(I);
- End;
- End;
- Finally
- CloseFile(InputFile);
- End;
- Except
- IsCorrect := False;
- End;
- IsFileCorrect := IsCorrect;
- End;
- Procedure GetSizeFromFile(Path: String; Var Size: Single; Var IsCorrect: Boolean);
- Var
- InputFile: TextFile;
- Null: String;
- Begin
- IsCorrect := True;
- Try
- AssignFile(InputFile, Path);
- Try
- Reset(InputFile);
- Readln(InputFile, Null);
- If (Null <> '') Then
- IsCorrect := True
- Else
- Begin
- IsCorrect := False;
- Application.MessageBox('Файл пуст!', 'Ошибка', MB_ICONERROR);
- End;
- Finally
- Close(InputFile);
- End;
- Except
- Application.MessageBox('Ошибка доступа!', 'Ошибка', MB_ICONERROR);
- End;
- If (IsCorrect) Then
- Begin
- Try
- Try
- Reset(InputFile);
- Readln(InputFile, Size);
- Finally
- Close(InputFile);
- End;
- Except
- IsCorrect := False;
- End;
- End;
- End;
- Function GetMatrixFromFile(Path: String; Size: Single; Matrix: TMatrix; Var IsCorrect: Boolean): TMatrix;
- Var
- InputFile: TextFile;
- I, J: Integer;
- Null: String;
- Begin
- IsCorrect := True;
- Try
- AssignFile(InputFile, Path);
- Reset(InputFile);
- Readln(InputFile, Null);
- If (Null <> '') Then
- IsCorrect := True
- Else
- Begin
- IsCorrect := False;
- Application.MessageBox('Данные в файле введены неверно или отсутствуют!', 'Ошибка', MB_ICONERROR);
- End;
- Except
- Application.MessageBox('Ошибка доступа!', 'Ошибка', MB_ICONERROR);
- End;
- If (IsCorrect) Then
- Begin
- Try
- Reset(InputFile);
- Readln(InputFile);
- For I := Low(Matrix) To High(Matrix) Do
- For J := Low(Matrix[0]) To High(Matrix[0]) Do
- Read(InputFile, Matrix[I, J]);
- Except
- IsCorrect := False;
- End;
- Close(InputFile);
- End;
- GetMatrixFromFile := Matrix;
- End;
- Function FindDeterminant(Matrix: TMatrix): Integer;
- Var
- Sign, I, K, J, M, Counter, Sum: Integer;
- Minor: TMatrix;
- Begin
- Sum := 0;
- If High(Matrix) = 0 Then
- FindDeterminant := Matrix[0, 0]
- Else
- Begin
- SetLength(Minor, High(Matrix), High(Matrix));
- Counter := 0;
- For K := 0 To High(Matrix) Do
- Begin
- Sign := -1;
- For M := 0 To K Do
- Sign := -Sign;
- For I := 1 To High(Matrix) Do
- For J := 0 To High(Matrix) Do
- Begin
- If (J <> K) Then
- Begin
- Minor[I - 1, (Counter Mod High(Matrix))] := Matrix[I, J];
- Inc(Counter);
- End;
- End;
- Sum := Sum + (Sign * Matrix[0, K] * FindDeterminant(Minor));
- End;
- FindDeterminant := Sum;
- End;
- End;
- Procedure TfrmForm.FormCloseQuery(Sender: TObject; Var CanClose: Boolean);
- Begin
- If Application.MessageBox(PChar('Вы уверены, что хотите выйти?'), PChar('Выход'),
- MB_ICONQUESTION + MB_YESNO + MB_DEFBUTTON1 + MB_TASKMODAL) = IDYES Then
- CanClose := True
- Else
- Begin
- CanClose := False;
- End;
- End;
- Procedure TfrmForm.FormCreate(Sender: TObject);
- Begin
- UnitMain.PreviousSize := 1;
- End;
- Procedure TfrmForm.miInfoAboutDeveloperClick(Sender: TObject);
- Const
- FIRST_MESSAGE = 'Ф.И.О.: Карась А.С. a.k.a Clownfish' + #13#10;
- SECOND_MESSAGE = 'Группа: 251004' + #13#10;
- THIRD_MESSAGE = 'Контакты: предварительная запись вживую по адресу' + #13#10;
- FOURTH_MESSAGE = 'г.Гродно, ул.Мостовая, д.31';
- Begin
- Application.MessageBox(FIRST_MESSAGE + SECOND_MESSAGE + THIRD_MESSAGE + FOURTH_MESSAGE,'О разработчике');
- End;
- Procedure TfrmForm.miInstructionClick(Sender: TObject);
- Const
- FIRST_MESSAGE = '- Вводимыми значениями могут являться только целые числа!' + #13#10;
- SECOND_MESSAGE = '- Диапазон ввода размера матрицы: 1...10' + #13#10;
- THIRD_MESSAGE = '- Диапазон вводимых значений: -99...99. ' + #13#10;
- FOURTH_MESSAGE = '- Для ввода из файла используйте вкладку ''Файл'' - ''Открыть''.' + #13#10;
- FIFTH_MESSAGE = '- Для сохранения в файл используйте вкладку ''Файл'' - ''Сохранить''.' + #13#10;
- Begin
- Application.MessageBox(FIRST_MESSAGE + SECOND_MESSAGE + THIRD_MESSAGE
- + FOURTH_MESSAGE + FIFTH_MESSAGE, 'Справка');
- End;
- Procedure TfrmForm.miOpenFromFileClick(Sender: TObject);
- Var
- SizeFromFile: Single;
- IsCorrect: Boolean;
- I, J: Integer;
- Matrix, MatrixFromFile: TMatrix;
- Begin
- If opdOpenFromFileDialog.Execute() Then
- If IsFileCorrect(opdOpenFromFileDialog.FileName) Then
- Begin
- GetSizeFromFile(opdOpenFromFileDialog.FileName, SizeFromFile, IsCorrect);
- UnitMain.PreviousSize := Round(SizeFromFile);
- SetLength(Matrix, UnitMain.PreviousSize, UnitMain.PreviousSize);
- SetLength(MatrixFromFile, UnitMain.PreviousSize, UnitMain.PreviousSize);
- MatrixFromFile := GetMatrixFromFile(opdOpenFromFileDialog.FileName, UnitMain.PreviousSize, Matrix, IsCorrect);
- If (IsCorrect) Then
- Begin
- eSize.Text := SizeFromFile.ToString;
- btConfirmSizeClick(Sender);
- For I := Low(MatrixFromFile) To High(MatrixFromFile) Do
- For J := Low(MatrixFromFile[0]) To High(MatrixFromFile[0]) Do
- Begin
- strgrMatrix.Cells[J, I] := FloatToStr(MatrixFromFile[I, J]);
- End;
- btConfirmMatrixClick(Sender);
- End;
- End
- Else
- Application.MessageBox('Данные в файле некорректны, попробуйте ещё раз.', 'Ошибка!', MB_ICONERROR);
- End;
- Procedure TfrmForm.eSizeChange(Sender: TObject);
- Begin
- If Length(eSize.Text) > 0 Then
- btConfirmSize.Enabled := True
- Else
- Begin
- btConfirmSize.Enabled := False;
- End;
- End;
- Procedure TfrmForm.eSizeKeyDown(Sender: TObject; Var Key: Word;
- Shift: TShiftState);
- Begin
- TEdit(Sender).ReadOnly := (Shift=[ssShift]) Or (Shift=[ssCtrl]);
- End;
- Procedure TfrmForm.eSizeKeyPress(Sender: TObject; Var Key: Char);
- Begin
- If eSize.Text = '1' Then
- Begin
- btConfirmSize.Enabled := False;
- eSize.MaxLength := 2;
- If Not ((Key = '0') Or (Key = #8)) Then
- Key := #0
- Else
- btConfirmSize.Enabled := True;
- End
- Else
- Begin
- eSize.MaxLength := 1;
- btConfirmSize.Enabled := True;
- End;
- If (eSize.SelStart = 0) And (Key = '0') Then
- Key := #0;
- End;
- Procedure TfrmForm.btConfirmSizeClick(Sender: TObject);
- Const
- MIN_VALUE = 1;
- MAX_VALUE = 10;
- Var
- IsCorrect: Boolean;
- Value, I, J: Integer;
- Begin
- IsCorrect := True;
- Try
- Value := StrToInt(eSize.Text);
- If (Value < MIN_VALUE) Or (Value > MAX_VALUE) Then
- IsCorrect := False;
- Except
- IsCorrect := False;
- End;
- If IsCorrect Then
- Begin
- lbMatrixRequirement.Visible := True;
- strgrMatrix.Visible := True;
- strgrMatrix.Enabled := True;
- btConfirmMatrix.Visible := True;
- btConfirmMatrix.Enabled := True;
- btConfirmSize.Default := False;
- btConfirmMatrix.Default := True;
- btConfirmSize.Enabled := False;
- btFindDeterminant.Enabled := False;
- If UnitMain.PreviousSize <> StrToInt(eSize.Text) Then
- Begin
- For I := 1 To strgrMatrix.RowCount - 1 Do
- For J := 1 To strgrMatrix.ColCount - 1 Do
- strgrMatrix.Cells[J, I] := '';
- End;
- UnitMain.PreviousSize := StrToInt(eSize.Text);
- SetLength(UnitMain.InputMatrix, UnitMain.PreviousSize, UnitMain.PreviousSize);
- strgrMatrix.RowCount := UnitMain.PreviousSize;
- strgrMatrix.ColCount := UnitMain.PreviousSize;
- End
- Else
- Begin
- Application.MessageBox('Проверьте корректность данных и повторите попытку!', 'Ошибка');
- strgrMatrix.Enabled := False;
- btConfirmMatrix.Enabled := False;
- btConfirmSize.Default := True;
- btConfirmMatrix.Default := False;
- btConfirmSize.Enabled := True;
- btFindDeterminant.Enabled := True;
- End;
- End;
- Procedure TfrmForm.strgrMatrixKeyDown(Sender: TObject; Var Key: Word;
- Shift: TShiftState);
- Begin
- TEdit(Sender).ReadOnly := (Shift=[ssShift]) Or (Shift=[ssCtrl]);
- End;
- Procedure TfrmForm.strgrMatrixKeyPress(Sender: TObject; Var Key: Char);
- Var
- Col, Row: Word;
- Temp: String;
- Begin
- btConfirmMatrix.Enabled := True;
- btFindDeterminant.Enabled := False;
- Col := strgrMatrix.Col;
- Row := strgrMatrix.Row;
- Col := strgrMatrix.Col;
- Row := strgrMatrix.Row;
- If Not(Key In ['0'..'9', #08, #45]) Then
- Key := #0;
- If (strgrMatrix.Cells[Col, Row] = '0') And (Key = '0') Then
- Begin
- Delete(Temp, Length(Temp), 1);
- Key := #0;
- End;
- Temp := strgrMatrix.Cells[Col, Row];
- If Length(strgrMatrix.Cells[Col, Row]) > 0 Then
- Try
- If (Temp[1] = '-') Then
- Begin
- If (Key = '-') Then
- Key := #0;
- If (Length(strgrMatrix.Cells[Col, Row]) = 1) And (Key = '0') Then
- Key := #0;
- If (Length(strgrMatrix.Cells[Col, Row]) > 2) And Not (Key In [#8, #127]) Then
- Begin
- Delete(Temp, Length(Temp), 1);
- Key := #0;
- End;
- End
- Else
- Begin
- If (Key = '-') Then
- Begin
- Try
- StrToInt(strgrMatrix.Cells[Col, Row] + '-');
- Except
- Key := #0;
- End;
- End;
- If (Length(strgrMatrix.Cells[Col, Row]) > 1) And Not (Key In [#8, #127]) Then
- Begin
- Delete(Temp, Length(Temp), 1);
- Key := #0;
- End;
- End;
- Finally
- //пока ничего
- End;
- End;
- Procedure TfrmForm.btConfirmMatrixClick(Sender: TObject);
- Const
- MAX_VALUE = 99;
- Var
- I, J, TempElem: Integer;
- IsBlanc: Boolean;
- IsCorrect: Boolean;
- Str: String;
- Begin
- IsBlanc:= False;
- For I := 1 To strgrMatrix.RowCount - 1 Do
- For J := 1 To strgrMatrix.ColCount - 1 Do
- Begin
- Str := strgrMatrix.Cells[J, I];
- If Str = '' Then
- IsBlanc:= True;
- End;
- If IsBlanc Then
- Application.MessageBox('Вы не заполнили последовательность полностью. Пожалуйста, заполните пустые поля!','Ошибка')
- Else
- Begin
- I := 0;
- IsCorrect := True;
- While IsCorrect And (I < strgrMatrix.RowCount - 1) Do
- Begin
- J := 0;
- While IsCorrect And (J < strgrMatrix.ColCount - 1) Do
- Begin
- Try
- TempElem := StrToInt(strgrMatrix.Cells[J, I]);
- If (Abs(TempElem) > MAX_VALUE) Then
- IsCorrect := False;
- Except
- IsCorrect := False;
- End;
- Inc(J);
- If Not IsCorrect Then
- Begin
- Application.MessageBox('Вы допустили ошибку в ячейке.', 'Ошибка!', MB_ICONERROR);
- strgrMatrix.SetFocus;
- End;
- End;
- Inc(I);
- End;
- If IsCorrect Then
- Begin
- For I := Low(UnitMain.InputMatrix) To High(UnitMain.InputMatrix) Do
- For J := Low(UnitMain.InputMatrix[0]) To High(UnitMain.InputMatrix[0]) Do
- UnitMain.InputMatrix[I, J] := StrToInt(strgrMatrix.Cells[J, I]);
- btFindDeterminant.Visible := True;
- btFindDeterminant.Enabled := True;
- btFindDeterminant.Default := True;
- btConfirmMatrix.Default := False;
- btConfirmMatrix.Enabled := False;
- End;
- End;
- End;
- Procedure TfrmForm.btFindDeterminantClick(Sender: TObject);
- Var
- Determinant: Integer;
- Begin
- lbAnswer.Visible := True;
- Determinant := FindDeterminant(InputMatrix);
- lbFinalResult.Visible := True;
- lbFinalResult.Caption := IntToStr(Determinant);
- miSaveToFile.Enabled := True;
- End;
- Procedure TfrmForm.miSaveToFileClick(Sender: TObject);
- Var
- OutputFile: TextFile;
- I, J: Integer;
- Begin
- If svdSaveToFileDialog.Execute() And FileExists(svdSaveToFileDialog.FileName) Then
- Begin
- AssignFile(OutputFile, svdSaveToFileDialog.FileName);
- Try
- Try
- Rewrite(OutputFile);
- Writeln(OutputFile, 'Входные данные: ');
- Writeln(OutputFile, UnitMain.PreviousSize);
- For I := Low(UnitMain.InputMatrix) To High(UnitMain.InputMatrix) Do
- Begin
- For J := Low(UnitMain.InputMatrix[0]) To High(UnitMain.InputMatrix[0]) Do
- Write(OutputFile, UnitMain.InputMatrix[I, J], ' ');
- Write(OutputFile, #13#10);
- End;
- Write(OutputFile, 'Ответ: ' + #13#10);
- Write(OutputFile, frmForm.lbFinalResult.Caption);
- Application.MessageBox('Данные успешно записаны в файл!', 'Сохранение', MB_ICONINFORMATION);
- Finally
- CloseFile(OutputFile);
- End;
- Except
- Application.MessageBox('Отказано в доступе! Измените параметры файла! ', 'Ошибка!', MB_ICONERROR);
- End;
- End
- Else
- Application.MessageBox('Введено некорректное имя файла или закрыто окно сохранения!', 'Ошибка!', MB_ICONERROR);
- End;
- End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement