Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit LAB_4_1_Form;
- 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.Menus,
- AboutDev;
- const
- MAX_GRID_WIDTH_AMOUNT = 8;
- MAX_GRID_HEIGHT_AMOUNT = 8;
- MAX_EDT_LEN = 9;
- BYTE_MAX_VAL = 255;
- type
- T2DArray = array of array of Integer;
- TSet = set of Byte;
- TForm1 = class(TForm)
- sgA: TStringGrid;
- btnCreate: TButton;
- Button1: TButton;
- btnClear: TButton;
- lblN: TLabel;
- edtN: TEdit;
- btnCalc: TButton;
- lblAns: TLabel;
- edtAns: TEdit;
- MainMenu1: TMainMenu;
- N1: TMenuItem;
- N2: TMenuItem;
- N3: TMenuItem;
- N4: TMenuItem;
- N5: TMenuItem;
- N6: TMenuItem;
- odDialog: TOpenDialog;
- sdDialog: TSaveDialog;
- procedure btnCalcClick(Sender: TObject);
- procedure btnCreateClick(Sender: TObject);
- procedure Button1Click(Sender: TObject);
- procedure FormCreate(Sender: TObject);
- procedure btnClearClick(Sender: TObject);
- procedure sgAKeyPress(Sender: TObject; var Key: Char);
- procedure edtNKeyPress(Sender: TObject; var Key: Char);
- procedure N6Click(Sender: TObject);
- procedure N5Click(Sender: TObject);
- procedure N2Click(Sender: TObject);
- procedure N3Click(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- var
- Form1: TForm1;
- Matr: T2DArray;
- implementation
- {$R *.dfm}
- function Det(M: T2DArray; P: Integer; Exc: TSet): Integer;
- var
- I: Byte;
- Ans, Mul, BDet, IP: Integer;
- begin
- Ans := 0;
- IP := 0;
- for I := 0 to Length(M[0]) - 1 do
- if not(I in Exc) then
- begin
- if P = Length(M[0]) - 1 then
- begin
- BDet := M[P, I];
- Ans := BDet;
- end
- else
- begin
- if (IP mod 2 = 0) then
- Mul := 1
- else
- Mul := -1;
- Include(Exc, I);
- BDet := Det(M, P + 1, Exc);
- Exclude(Exc, I);
- Ans := Ans + Mul * M[P, I] * BDet;
- end;
- Inc(IP);
- end;
- Result := Ans;
- end;
- procedure CorrectStringGridView(var Grid: TStringGrid);
- var
- Mul: Integer;
- begin
- if Grid.Name = 'sgA' then
- Mul := MAX_GRID_WIDTH_AMOUNT
- else
- Mul := 1;
- if Grid.ColCount > MAX_GRID_WIDTH_AMOUNT then
- Grid.Height := Grid.DefaultRowHeight * Mul +
- GetSystemMetrics(SM_CXHSCROLL) + 3 + Mul
- else
- Grid.Height := Grid.DefaultRowHeight * Mul + 3 + Mul;
- if Grid.RowCount > MAX_GRID_HEIGHT_AMOUNT then
- Grid.Width := Grid.DefaultColWidth * Mul +
- GetSystemMetrics(SM_CXHSCROLL) + 3 + Mul
- else
- Grid.Width := Grid.DefaultColWidth * MAX_GRID_WIDTH_AMOUNT + 3 +
- MAX_GRID_WIDTH_AMOUNT;
- end;
- procedure TForm1.btnCreateClick(Sender: TObject);
- var
- N: Integer;
- begin
- with sgA do
- begin
- try
- N := StrToInt(edtN.Text);
- except
- MessageBox(0, PChar('Поля не дожны быть пустыми!'), 'Внимание',
- MB_ICONWARNING);
- Exit;
- end;
- if N > 11 then
- begin
- MessageBox(0, PChar('Порядок матрицы не должн превышать 11'),
- 'Внимание', MB_ICONWARNING);
- Exit;
- end;
- ColCount := N;
- RowCount := N;
- end;
- CorrectStringGridView(sgA);
- end;
- procedure EnableOutElements;
- begin
- with Form1 do
- begin
- lblAns.Visible := True;
- edtAns.Visible := True;
- end;
- end;
- procedure DisableOutElements;
- begin
- with Form1 do
- begin
- lblAns.Visible := False;
- edtAns.Visible := False;
- end;
- end;
- procedure TForm1.Button1Click(Sender: TObject);
- var
- I, J: Integer;
- begin
- Randomize;
- with sgA do
- begin
- SetLength(Matr, ColCount, ColCount);
- for I := 0 to ColCount - 1 do
- for J := 0 to ColCount - 1 do
- Cells[I, J] := IntToStr(1 + Random(ColCount * ColCount));
- end;
- end;
- procedure TForm1.btnClearClick(Sender: TObject);
- var
- I, J: Integer;
- begin
- DisableOutElements;
- with sgA do
- begin
- for I := 0 to ColCount - 1 do
- for J := 0 to ColCount - 1 do
- Cells[J, I] := '';
- ColCount := 0;
- RowCount := 0;
- end;
- edtN.Text := '';
- end;
- procedure TForm1.btnCalcClick(Sender: TObject);
- var
- I, J: Integer;
- begin
- with sgA do
- begin
- SetLength(Matr, ColCount, ColCount);
- for I := 0 to ColCount - 1 do
- for J := 0 to ColCount - 1 do
- try
- Matr[I, J] := StrToInt(Cells[J, I]);
- except
- MessageBox(0, PChar('Ячейки не должны быть пустыми'),
- PChar('Внимание!'), MB_ICONERROR);
- Exit;
- end;
- end;
- edtAns.Text := IntToStr(Det(Matr, 0, []));
- EnableOutElements;
- end;
- procedure CorrectExtendedInput(var EditText: TEdit; var Key: Char);
- begin
- if not(Key in [#8, #13, '0' .. '9', ',']) then
- begin
- Key := #0;
- end
- else if (Key = ',') and (Pos(Key, EditText.Text) > 0) then
- begin
- Key := #0;
- end
- end;
- procedure TForm1.edtNKeyPress(Sender: TObject; var Key: Char);
- begin
- DisableOutElements;
- CorrectExtendedInput(edtN, Key);
- end;
- procedure TForm1.FormCreate(Sender: TObject);
- begin
- with sgA do
- begin
- ColWidths[0] := DefaultColWidth;
- Width := DefaultColWidth * MAX_GRID_WIDTH_AMOUNT + 3 +
- MAX_GRID_WIDTH_AMOUNT;
- Height := DefaultRowHeight * MAX_GRID_WIDTH_AMOUNT + 3 +
- MAX_GRID_WIDTH_AMOUNT;
- end;
- end;
- procedure TForm1.N2Click(Sender: TObject);
- var
- InputFile: TextFile;
- FileName: String;
- StopReadN: Boolean;
- I, J, ErrorPos, N, Buf: Integer;
- RawInput: String;
- begin
- if odDialog.Execute then
- begin
- FileName := odDialog.FileName;
- AssignFile(InputFile, FileName);
- if FileExists(FileName) then
- begin
- Reset(InputFile);
- StopReadN := False;
- Readln(InputFile, RawInput);
- edtN.Text := RawInput;
- Val(RawInput, N, ErrorPos);
- if N > 0 then
- begin
- StopReadN := True;
- edtN.Text := IntToStr(N);
- end
- else
- ShowMessage('Порядок матрицы должен быть больше нуля...');
- if StopReadN then
- begin
- btnCreate.Click;
- CorrectStringGridView(sgA);
- for I := 0 to N - 1 do
- begin
- for J := 0 to N - 1 do
- begin
- if SeekEof(InputFile) then
- begin
- MessageBox(0, PChar('Недостаточно данных.'), 'Warning',
- MB_OK + MB_ICONERROR);
- btnClear.Click;
- Break;
- end
- else
- begin
- try
- Read(InputFile, Buf);
- sgA.Cells[J, I] := IntToStr(Buf);
- except
- MessageBox(0,
- PChar('Файл содержит некорректные данные...'),
- 'Warning', MB_OK + MB_ICONERROR);
- btnClear.Click;
- DisableOutElements;
- Exit;
- end;
- end;
- end;
- end;
- end;
- CloseFile(InputFile);
- end;
- end;
- end;
- procedure TForm1.N3Click(Sender: TObject);
- var
- OutputFile: TextFile;
- FileName: String;
- buttonSelected: Integer;
- I, J: Integer;
- begin
- if sdDialog.Execute then
- begin
- FileName := sdDialog.FileName;
- AssignFile(OutputFile, FileName);
- if FileExists(FileName) then
- begin
- buttonSelected := MessageDlg('Такой фай уже существует, перезаписать?',
- mtCustom, [mbYes, mbNo], 0);
- if buttonSelected = mrNo then
- Append(OutputFile)
- else
- begin
- Rewrite(OutputFile);
- end;
- end
- else
- Rewrite(OutputFile);
- Writeln(OutputFile, IntToStr(sgA.ColCount));
- for I := 0 to sgA.ColCount - 1 do
- begin
- for J := 0 to sgA.ColCount - 1 do
- Write(OutputFile, sgA.Cells[J, I] + ' ');
- Writeln(OutputFile);
- end;
- CloseFile(OutputFile);
- end;
- end;
- procedure TForm1.N5Click(Sender: TObject);
- begin
- MessageBox(0, 'Задание:' + #13#13 +
- 'Вычислить определитель заданной матрицы, пользуясь формулой разложения по первой строке.',
- 'О программе', MB_OK);
- end;
- procedure TForm1.N6Click(Sender: TObject);
- begin
- AboutMe.Show;
- end;
- procedure TForm1.sgAKeyPress(Sender: TObject; var Key: Char);
- begin
- with sgA do
- begin
- if not(Key in [#9, #8, '0' .. '9', '-']) then
- begin
- Key := #0;
- end
- else if (Key = '-') and (Pos(Key, Cells[Col, Row]) > 0) then
- begin
- Key := #0;
- end
- else if (Key = '-') and (Cells[Col, Row] <> '') then
- begin
- Key := #0;
- end
- else if (Length(Cells[Col, Row]) + 1 > MAX_EDT_LEN) and not(Key = #8) then
- Key := #0
- end;
- DisableOutElements;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement