Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit Lab_1_4_Forms;
- interface
- uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Menus, Vcl.Grids, Vcl.StdCtrls;
- type
- TForm1 = class(TForm)
- MainMenu1: TMainMenu;
- LabelTask: TLabel;
- LabelGetLength: TLabel;
- EditLengthArr: TEdit;
- ButtonFillArr: TButton;
- SpreadSheep: TStringGrid;
- LabelSum: TLabel;
- EditSum: TEdit;
- ButtonFile: TMenuItem;
- ButtonOpenFile: TMenuItem;
- ButtonSaveFile: TMenuItem;
- ButtonInstruction: TMenuItem;
- ButtonAbout: TMenuItem;
- ButtonFindSum: TButton;
- OpenDialog1: TOpenDialog;
- SaveDialog1: TSaveDialog;
- procedure EditLengthArrKeyPress(Sender: TObject; var Key: Char);
- procedure EditLengthArrChange(Sender: TObject);
- procedure ButtonFillArrClick(Sender: TObject);
- procedure SpreadSheepSelectCell(Sender: TObject; ACol, ARow: Integer;
- var CanSelect: Boolean);
- procedure SpreadSheepKeyPress(Sender: TObject; var Key: Char);
- procedure ButtonFindSumClick(Sender: TObject);
- procedure ButtonOpenFileClick(Sender: TObject);
- procedure ButtonAboutClick(Sender: TObject);
- procedure ButtonInstructionClick(Sender: TObject);
- procedure ButtonSaveFileClick(Sender: TObject);
- procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- var
- Form1: TForm1;
- implementation
- {$R *.dfm}
- uses UnitError, UnitAbout, UnitInstruction, UnitExit;
- const
- MIN_ELEMENT = -20;
- MAX_ELEMENT = 20;
- MIN_SIZE = 3;
- MAX_SIZE = 20;
- Type
- TArr = Array Of Integer;
- Var
- LengthArr: Integer;
- Arr: TArr;
- function FillArr(Sender: TObject; ArrLen: Integer; SprSheep: TStringGrid; var IsCorrect: Boolean): TArr;
- var
- I: Integer;
- A: TArr;
- begin
- SetLength(A, LengthArr);
- I := 1;
- IsCorrect := True;
- while (IsCorrect) and (I <= ArrLen)do
- begin
- try
- A[I - 1] := StrToInt(SprSheep.Cells[I, 1]);
- except
- IsCorrect := False;
- UnitError.FormError.LabelError.Caption := 'Ошибка! Получено некорректное значение элемента массива!';
- UnitError.FormError.ShowModal();
- end;
- Inc(I);
- end;
- FillArr := A;
- end;
- procedure FillNumbersSpreadSheep(Sender: TObject; ArrLen: Integer; SprSheep: TStringGrid);
- var
- I: Integer;
- CanChange: Boolean;
- begin
- for I := 1 to ArrLen do
- begin
- SprSheep.Cells[I, 0] := '№' + IntToStr(I);
- end;
- end;
- procedure TForm1.ButtonAboutClick(Sender: TObject);
- begin
- UnitAbout.FormAbout.ShowModal();
- end;
- procedure TForm1.ButtonFillArrClick(Sender: TObject);
- var
- IsCorrect: Boolean;
- I: Integer;
- begin
- ButtonFillArr.Enabled := False;
- IsCorrect := True;
- try
- LengthArr := StrToInt(EditLengthArr.Text);
- except
- IsCorrect := False;
- UnitError.FormError.LabelError.Caption := 'Ошибка! Получено некорректное значение длины массива!';
- UnitError.FormError.ShowModal();
- end;
- if (IsCorrect) then
- begin
- SpreadSheep.ColCount := LengthArr + 1;
- SetLength(Arr, LengthArr);
- SpreadSheep.ColWidths[0] := 80;
- SpreadSheep.Cells[0,0] := '№ элемента';
- SpreadSheep.Cells[0,1] := 'Элемент';
- FillNumbersSpreadSheep(Sender, LengthArr, SpreadSheep);
- SpreadSheep.Visible := True;
- SpreadSheep.Enabled := True;
- for I := 1 to LengthArr do
- begin
- SpreadSheepSelectCell(Sender, I, 1, IsCorrect);
- end;
- ButtonFindSum.Visible := True;
- ButtonFindSum.Enabled := True;
- end;
- end;
- procedure TForm1.ButtonFindSumClick(Sender: TObject);
- var
- IsFill, IsCorrect: Boolean;
- I, Sum: Integer;
- begin
- Sum := 0;
- IsCorrect := True;
- for I := 1 to LengthArr do
- if (SpreadSheep.Cells[I, 1].IsEmpty) then
- IsFill := False
- else
- IsFill := True;
- if (IsFill) then
- Arr := FillArr(Sender, LengthArr, SpreadSheep, IsCorrect)
- else
- begin
- UnitError.FormError.LabelError.Caption := 'Ошибка! Получено недостаточное количество элементов массива!';
- UnitError.FormError.ShowModal();
- IsCorrect := False;
- end;
- if (IsCorrect) then
- begin
- LabelSum.Visible := True;
- for I := 0 to High(Arr) do
- Sum := Sum + (I + 1) * Arr[I];
- EditSum.Text := IntToStr(Sum);
- EditSum.Visible := True;
- ButtonSaveFile.Enabled := True;
- end;
- end;
- procedure TForm1.ButtonInstructionClick(Sender: TObject);
- begin
- UnitInstruction.FormInstruction.ShowModal();
- end;
- procedure TForm1.ButtonOpenFileClick(Sender: TObject);
- var
- F: TextFile;
- Path, Error: String;
- I, Res, Size: Integer;
- IsCorrect: Boolean;
- A: TArr;
- begin
- IsCorrect := True;
- Error := '';
- Res := 0;
- Size := 0;
- If OpenDialog1.Execute() Then
- Begin
- Path := OpenDialog1.FileName;
- AssignFile(F, Path);
- Try
- Reset(F);
- Try
- Readln(F, Size);
- if (Size < MIN_SIZE) or (Size > MAX_SIZE) then
- begin
- IsCorrect := False;
- Error := Error + 'Размер массива за пределами диапазона допустимых значений. ';
- end;
- I := 0;
- SetLength(A, Size);
- While (IsCorrect) and (I < Size) Do
- Begin
- Read(F, A[I]);
- If (A[I] < MIN_ELEMENT) Or (A[I] > MAX_ELEMENT) Then
- Begin
- IsCorrect := False;
- Error := Error + 'Размер элемента за пределами диапазона допустимых значений. ';
- End;
- if (I < High(A)) and (EOF(F)) then
- begin
- IsCorrect := False;
- Error := Error + 'Недостаточно элементов в файле. ';
- end;
- Inc(I);
- End;
- Finally
- CloseFile(F);
- End;
- Except
- IsCorrect := False;
- Error := Error + 'Нет доступа к файлу';
- End;
- If not(IsCorrect) Then
- Begin
- UnitError.FormError.LabelError.Caption := 'Ошибка считывания с файла. ' + Error;
- Res := UnitError.FormError.ShowModal();
- End;
- if Res > 0 Then
- UnitError.FormError.LabelError.Caption := ''
- else
- begin
- LengthArr := Size;
- EditLengthArr.Text := IntToStr(Size);
- ButtonFillArrClick(Sender);
- SpreadSheep.Enabled := False;
- for I := 1 to LengthArr do
- begin
- SpreadSheep.Cells[I, 1] := IntToStr(A[I - 1]);
- end;
- end;
- End;
- end;
- procedure TForm1.ButtonSaveFileClick(Sender: TObject);
- var
- F: TextFile;
- Path, Error: String;
- Res, Answer: Integer;
- IsCorrect: Boolean;
- begin
- IsCorrect := True;
- Res := 0;
- Error := '';
- if SaveDialog1.Execute() then
- begin
- Path := SaveDialog1.FileName;
- AssignFile(F, Path);
- Try
- Rewrite(F);
- Try
- Write(F, 'Полученная сумма: ', EditSum.Text)
- Finally
- CloseFile(F);
- End;
- Except
- IsCorrect := False;
- Error := 'Нет доступа к файлу';
- End;
- if not(IsCorrect) then
- begin
- UnitError.FormError.LabelError.Caption := 'Ошибка считывания с файла. ' + Error;
- Res := UnitError.FormError.ShowModal();
- end;
- if Res > 0 Then
- UnitError.FormError.LabelError.Caption := '';
- end;
- end;
- procedure TForm1.EditLengthArrChange(Sender: TObject);
- var
- I, J: Integer;
- begin
- ButtonFillArr.Enabled := True;
- SpreadSheep.Visible := False;
- SpreadSheep.Enabled := False;
- ButtonSaveFile.Enabled := False;
- ButtonFindSum.Visible := False;
- ButtonFindSum.Enabled := False;
- EditSum.Visible := False;
- LabelSum.Visible := False;
- for I := 0 to SpreadSheep.ColCount - 1 do
- for J := 0 to SpreadSheep.RowCount - 1 do
- SpreadSheep.Cells[I, J] := '';
- if EditlengthArr.Text = '' then
- begin
- ButtonFillArr.Enabled := False;
- end;
- end;
- procedure TForm1.EditLengthArrKeyPress(Sender: TObject; var Key: Char);
- Var
- Number: Integer;
- begin
- if not(Key in ['0'..'9', #8, #13])then
- Key := #0;
- if (Length(EditLengthArr.Text) = 0) and (Key = '0') Then
- Key := #0;
- if (Length(EditLengthArr.Text) = 1) and (EditLengthArr.Text[1] = '2') and (Key in ['1'..'9']) then
- Key := #0;
- if (Length(EditLengthArr.Text) = 1) and not(EditLengthArr.Text[1] in ['2', '1']) and (Key in ['0'..'9']) then
- Key := #0;
- if (Length(EditLengthArr.Text) > 0) and (Key = #13) then
- ButtonFillArrClick(Sender);
- end;
- procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
- Var
- Res: Integer;
- begin
- Res := UnitExit.FormExit.ShowModal();
- If Res = mrOk Then
- CanClose := True
- Else
- CanClose := False;
- end;
- procedure TForm1.SpreadSheepKeyPress(Sender: TObject; var Key: Char);
- var
- MaxLength: Integer;
- begin
- if not(Key in ['0'..'9', '-', #8, #13])then
- Key := #0;
- MaxLength := 2;
- if (Length(SpreadSheep.Cells[SpreadSheep.Col, SpreadSheep.Row]) > 0) and (SpreadSheep.Cells[SpreadSheep.Col, SpreadSheep.Row][1] = '-') then
- MaxLength := 3;
- if (Length(SpreadSheep.Cells[SpreadSheep.Col, SpreadSheep.Row]) > 0) and (Key = '-') then
- Key := #0;
- if (Length(SpreadSheep.Cells[SpreadSheep.Col, SpreadSheep.Row]) > 0) and (SpreadSheep.Cells[SpreadSheep.Col, SpreadSheep.Row][1] = '0') and not(Key = #8) Then
- Key := #0;
- if (Length(SpreadSheep.Cells[SpreadSheep.Col, SpreadSheep.Row]) = 1) and (SpreadSheep.Cells[SpreadSheep.Col, SpreadSheep.Row][1] = '2') and (Key in ['1'..'9']) then
- Key := #0;
- if (Length(SpreadSheep.Cells[SpreadSheep.Col, SpreadSheep.Row]) = 1) and (SpreadSheep.Cells[SpreadSheep.Col, SpreadSheep.Row][1] in ['3'..'9']) and (Key in ['0'..'9']) then
- Key := #0;
- if (Length(SpreadSheep.Cells[SpreadSheep.Col, SpreadSheep.Row]) = 2) and (SpreadSheep.Cells[SpreadSheep.Col, SpreadSheep.Row] = '-2') and not(Key in ['0', #8]) then
- Key := #0;
- if (Length(SpreadSheep.Cells[SpreadSheep.Col, SpreadSheep.Row]) = 1) and (SpreadSheep.Cells[SpreadSheep.Col, SpreadSheep.Row] = '-') and (Key = '0') then
- Key := #0;
- if (Length(SpreadSheep.Cells[SpreadSheep.Col, SpreadSheep.Row]) = 2) and (SpreadSheep.Cells[SpreadSheep.Col, SpreadSheep.Row][1] = '-') and (SpreadSheep.Cells[SpreadSheep.Col, SpreadSheep.Row][2] in ['3'..'9']) and (Key in ['0'..'9']) then
- Key := #0;
- if (Length(SpreadSheep.Cells[SpreadSheep.Col, SpreadSheep.Row]) = MaxLength) and not(Key = #8)then
- Key := #0;
- end;
- procedure TForm1.SpreadSheepSelectCell(Sender: TObject; ACol, ARow: Integer;
- var CanSelect: Boolean);
- begin
- if (ACol = 0) or (ARow = 0) then
- CanSelect := False;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement