Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit LabForm4_2;
- interface
- uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Menus, Vcl.StdCtrls,
- Vcl.Samples.Spin, Vcl.Grids, Math;
- Const
- DEFAULT_GRID_WIDTH = 205;
- type
- TArr = array of Integer;
- TMainForm = class(TForm)
- SecondArrStringGrid: TStringGrid;
- SizeLabel: TLabel;
- TaskLabel: TLabel;
- ColSpinEdit: TSpinEdit;
- CalculateButton: TButton;
- SaveDialog: TSaveDialog;
- OpenDialog: TOpenDialog;
- MainMenu: TMainMenu;
- FileMenu: TMenuItem;
- OpenFile: TMenuItem;
- SaveInFile: TMenuItem;
- ALabel: TLabel;
- BLabel: TLabel;
- OutputLabel: TLabel;
- FirstArrStringGrid: TStringGrid;
- procedure ColSpinEditChange(Sender: TObject);
- procedure CalculateButtonClick(Sender: TObject);
- procedure FileMenuClick(Sender: TObject);
- function FindValid: Boolean;
- procedure FirstArrStringGridSetEditText(Sender: TObject; ACol,
- ARow: Integer; const Value: string);
- procedure SecondArrStringGridSetEditText(Sender: TObject; ACol,
- ARow: Integer; const Value: string);
- procedure FirstArrStringGridKeyPress(Sender: TObject; var Key: Char);
- procedure SecondArrStringGridKeyPress(Sender: TObject; var Key: Char);
- procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
- procedure OpenFileClick(Sender: TObject);
- procedure SaveInFileClick(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- var
- MainForm: TMainForm;
- implementation
- {$R *.dfm}
- function TMainForm.FindValid(): Boolean;
- var
- ExpressionResult: Double;
- I, J, N: Integer;
- Answer: Boolean;
- begin
- Answer := True;
- for I := 0 to StrToInt(ColSpinEdit.Text) - 1 do
- if (FirstArrStringGrid.Cells[I, 0] = '') then
- Answer := False;
- for I := 0 to StrToInt(ColSpinEdit.Text) - 1 do
- if (SecondArrStringGrid.Cells[I, 0] = '') then
- Answer := False;
- FindValid := Answer;
- end;
- procedure TMainForm.FirstArrStringGridKeyPress(Sender: TObject; var Key: Char);
- begin
- if not(Key in [#48 .. #57, #8, #13]) then
- Key := #0;
- end;
- procedure TMainForm.SecondArrStringGridKeyPress(Sender: TObject; var Key: Char);
- begin
- if not(Key in [#48 .. #57, #8, #13]) then
- Key := #0;
- end;
- procedure TMainForm.FirstArrStringGridSetEditText(Sender: TObject; ACol,
- ARow: Integer; const Value: string);
- var
- Str: string;
- I: Integer;
- begin
- MainForm.Height := 340;
- OutputLabel.Caption := '';
- Try
- if (StrToFloat(Value) > 9) or (StrToFloat(Value) < 0) then
- begin
- Str := Value;
- FirstArrStringGrid.Cells[ACol, ARow] := Copy(Str, 1, (Length(Str) - 1));
- MessageBox(0, 'Каждый разряд должен содержать одну цифру', 'Number value',
- mb_IconHand);
- end;
- Except
- End;
- CalculateButton.Enabled := FindValid;
- end;
- procedure TMainForm.SecondArrStringGridSetEditText(Sender: TObject; ACol,
- ARow: Integer; const Value: string);
- var
- Str: string;
- I: Integer;
- begin
- MainForm.Height := 340;
- OutputLabel.Caption := '';
- Try
- if (StrToFloat(Value) > 9) or (StrToFloat(Value) < 0) then
- begin
- Str := Value;
- SecondArrStringGrid.Cells[ACol, ARow] := Copy(Str, 1, (Length(Str) - 1));
- MessageBox(0, 'Каждый разряд должен содержать одну цифру', 'Number value',
- mb_IconHand);
- end;
- Except
- End;
- CalculateButton.Enabled := FindValid;
- end;
- Procedure FindSum(FirstNum, SecondNum: TArr; Digit: Integer; var Sum: Integer);
- Var
- PrevDig, NextDig, I, Order: Integer;
- Begin
- Order := 1;
- for I := 0 to Digit - 1 do
- Order := Order * 10;
- If Digit < High(FirstNum) + 1 then
- Begin
- PrevDig := ((FirstNum[Digit] + SecondNum[Digit]) Mod 10) * Order;
- NextDig := ((FirstNum[Digit] + SecondNum[Digit]) Div 10) * Order * 10;
- Sum := Sum + prevDig + nextDig;
- FindSum(FirstNum, SecondNum, Digit + 1, Sum);
- End;
- End;
- procedure TMainForm.CalculateButtonClick(Sender: TObject);
- Var
- FirstNumArr, SecondNumArr: TArr;
- Size, I, Digit, Sum: Integer;
- IsCorrect: Boolean;
- J: Integer;
- begin
- Digit := 0;
- Sum := 0;
- J := 0;
- IsCorrect := true;
- Size := ColSpinEdit.Value;
- SecondArrStringGrid.ColCount := Size;
- SetLength(FirstNumArr, Size);
- SetLength(SecondNumArr, Size);
- for I := Size - 1 downto 0 do
- Begin
- Try
- FirstNumArr[I] := StrToInt(FirstArrStringGrid.Cells[J,0]);
- Except
- IsCorrect := False
- End;
- Inc(J);
- End;
- J := 0;
- for I := Size - 1 downto 0 do
- Begin
- Try
- SecondNumArr[I] := StrToInt(SecondArrStringGrid.Cells[J,0]);
- Except
- IsCorrect := False
- End;
- Inc(J);
- End;
- if IsCorrect then
- Begin
- FindSum(FirstNumArr, SecondNumArr, Digit, Sum);
- OutputLabel.Caption := 'Сумма чисел: ' + IntToStr(Sum);
- MainForm.Height := 390;
- End
- Else
- ShowMessage('Данные введены некорректны. Введите числа.');
- end;
- procedure TMainForm.ColSpinEditChange(Sender: TObject);
- Var
- Size: Integer;
- begin
- MainForm.Height := 350;
- OutputLabel.Caption := '';
- Size := ColSpinEdit.Value;
- FirstArrStringGrid.ColCount := Size;
- SecondArrStringGrid.ColCount := Size;
- FirstArrStringGrid.Width := DEFAULT_GRID_WIDTH + (Size - 3) * 65;
- SecondArrStringGrid.Width := DEFAULT_GRID_WIDTH + (Size - 3) * 65;
- end;
- procedure TMainForm.FileMenuClick(Sender: TObject);
- begin
- SaveInFile.Enabled := Not(OutputLabel.Caption = '');
- end;
- procedure TMainForm.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
- begin
- CanClose := MessageDlg('Вы уверены, что хотите выйти из программы?' +
- #10#13 + 'Все несохранённые данные будут утеряны.',
- mtConfirmation, [mbYes, mbNo], 0) = mrYes;
- end;
- function CheckFile(Path: String): Boolean;
- var
- FileToCheck: TextFile;
- N, I, Num: Integer;
- begin
- AssignFile(FileToCheck, Path);
- Reset(FileToCheck);
- CheckFile := true;
- try
- Readln(FileToCheck, N);
- except
- CheckFile := false;
- end;
- if ((N < 1) or (N > 8)) then
- CheckFile := False;
- for I := 1 to N do
- Begin
- try
- Readln(FileToCheck, Num);
- except
- CheckFile := false;
- end;
- if (N < 0) Or (N > 9) then
- Checkfile := False;
- End;
- CloseFile(FileToCheck);
- end;
- procedure TMainForm.OpenFileClick(Sender: TObject);
- var
- InputFile: TextFile;
- Num, N, I: Integer;
- IsCorrect: Boolean;
- FirstArr, SecondArr: TArr;
- begin
- IsCorrect := True;
- if OpenDialog.Execute then
- begin
- if CheckFile(OpenDialog.FileName) then
- begin
- AssignFile(InputFile, OpenDialog.FileName);
- Reset(InputFile);
- Readln(InputFile, N);
- ColSpinEdit.Value := N;
- SetLength(FirstArr, N);
- SetLength(SecondArr, N);
- for I := 0 to N - 1 do
- Readln(InputFile, FirstArr[I]);
- for I := 0 to N - 1 do
- Readln(InputFile, SecondArr[I]);
- CloseFile(InputFile);
- for I := 0 to N - 1 do
- FirstArrStringGrid.Cells[I,0] := IntToStr(FirstArr[I]);
- for I := 0 to N - 1 do
- SecondArrStringGrid.Cells[I,0] := IntToStr(SecondArr[I]);
- ShowMessage('Данные из файла успешно загружены');
- end
- else
- Begin
- ShowMessage('Данные в файле некорректны. Каждый разряд массива должен содержать по одной цифре.');
- IsCorrect := False
- End;
- if IsCorrect = True then
- Begin
- CalculateButton.Enabled;
- CalculateButton.Click;
- End;
- end;
- end;
- procedure TMainForm.SaveInFileClick(Sender: TObject);
- var
- OutputFile: TextFile;
- I, J: Integer;
- begin
- if SaveDialog.Execute then
- begin
- AssignFile(OutputFile, SaveDialog.FileName);
- Rewrite(OutputFile);
- Writeln(OutputFile, 'Разрядность числа: ' + IntToStr(ColSpinEdit.Value));
- Writeln(OutputFile, 'Первое число:');
- for I := 0 to ColSpinEdit.Value do
- Write(OutputFile, FirstArrStringGrid.Cells[I, 0] + ' ');
- Writeln(OutputFile);
- Writeln(OutputFile, 'Второе число:');
- for I := 0 to ColSpinEdit.Value do
- Write(OutputFile, SecondArrStringGrid.Cells[I, 0] + ' ');
- Writeln(OutputFile);
- Write(OutputFile, OutputLabel.Caption);
- CloseFile(OutputFile);
- ShowMessage('Успешно сохранено');
- end;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement