Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit LabForm1_2;
- interface
- uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Menus;
- type
- TMainForm = class(TForm)
- Label1: TLabel;
- Label2: TLabel;
- InputNumber: TEdit;
- CalculateButton: TButton;
- OutputLabel: TLabel;
- OpenDialog: TOpenDialog;
- SaveDialog: TSaveDialog;
- MainMenu: TMainMenu;
- FileMenu: TMenuItem;
- OpenFile: TMenuItem;
- SaveInFile: TMenuItem;
- Procedure Calculate(Num: Integer);
- procedure CalculateButtonClick(Sender: TObject);
- procedure OpenFileClick(Sender: TObject);
- procedure SaveInFileClick(Sender: TObject);
- procedure FileMenuClick(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- var
- MainForm: TMainForm;
- implementation
- {$R *.dfm}
- Procedure TMainForm.Calculate(Num: Integer);
- Var
- Comp, I: Integer;
- Begin
- Comp:=1;
- for I := 2 to 2 * Num do
- if I mod 2 = 0 then
- Comp := Comp * I;
- Outputlabel.Caption := 'Произведение: ' + IntToStr(Comp);
- End;
- procedure TMainForm.CalculateButtonClick(Sender: TObject);
- Var
- IsCorrect: Boolean;
- begin
- IsCorrect := True;
- if ((StrToInt(InputNumber.Text) < 1) or (StrToInt(InputNumber.Text) > 9)) then
- begin
- IsCorrect := false;
- MessageDlg('Введите натуральное число в диапозоне от 1 до 9', mtError, [mbOk], 0);
- end;
- if (IsCorrect = true) then
- Begin
- Try
- Calculate(StrToInt(InputNumber.Text));
- Except
- MessageDlg('Введите натуральное число', mtError, [mbOk], 0);
- End;
- End;
- end;
- function CheckFile(Path: String): Boolean;
- var
- FileToCheck: TextFile;
- N: Integer;
- begin
- AssignFile(FileToCheck, Path);
- Reset(FileToCheck);
- CheckFile := true;
- try
- Readln(FileToCheck, N);
- if (N < 1) or (N > 9) then
- CheckFile := false;
- except
- CheckFile := false;
- end;
- CloseFile(FileToCheck);
- end;
- procedure TMainForm.OpenFileClick(Sender: TObject);
- var
- InputFile: TextFile;
- N: Integer;
- begin
- if OpenDialog.Execute then
- begin
- if CheckFile(OpenDialog.FileName) then
- begin
- AssignFile(InputFile, OpenDialog.FileName);
- Reset(InputFile);
- Read(InputFile, N);
- InputNumber.Text := IntToStr(N);
- CloseFile(InputFile);
- ShowMessage('Данные из файла успешно загружены');
- end
- else
- ShowMessage('Данные в файле некорректны');
- end;
- end;
- procedure TMainForm.FileMenuClick(Sender: TObject);
- begin
- SaveInFile.Enabled := NOT (OutputLabel.Caption = '');
- end;
- procedure TMainForm.SaveInFileClick(Sender: TObject);
- var
- OutputFile: TextFile;
- begin
- if SaveDialog.Execute then
- begin
- AssignFile(OutputFile, SaveDialog.FileName);
- Rewrite(OutputFile);
- Write(OutputFile, MainForm.OutputLabel.Caption);
- CloseFile(OutputFile);
- ShowMessage('Успешно сохранено');
- end;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement