Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit Input;
- interface
- uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls, Vcl.Mask,
- Vcl.Menus, Output;
- Const
- INT_TYPE = 'Целое число';
- FLOAT_TYPE = 'Действительное число';
- type
- TInputForm = class(TForm)
- ComboBox1: TComboBox;
- NumberInputLabel: TEdit;
- Bevel1: TBevel;
- OpenDialog: TOpenDialog;
- MainMenu: TMainMenu;
- FileMenu: TMenuItem;
- OpenFile: TMenuItem;
- SaveDialog: TSaveDialog;
- Label1: TLabel;
- CalculateButton: TButton;
- SaveFile: TMenuItem;
- procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
- procedure OpenFileClick(Sender: TObject);
- procedure CalculateButtonClick(Sender: TObject);
- procedure NumberInputLabelChange(Sender: TObject);
- procedure NumberInputLabelKeyPress(Sender: TObject; var Key: Char);
- procedure FileMenuClick(Sender: TObject);
- procedure SaveFileClick(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- var
- InputForm: TInputForm;
- implementation
- {$R *.dfm}
- Function IsFileExist(Address: String): Boolean; export;
- Var
- IsCorrect: Boolean;
- Begin
- If FileExists(Address) Then
- IsCorrect := True
- Else
- Begin
- IsCorrect := False;
- End;
- IsFileExist := IsCorrect;
- End;
- procedure TInputForm.CalculateButtonClick(Sender: TObject);
- Var
- IsCorrect: Boolean;
- begin
- OutputLabel.Show;
- if NumberInputLabel.Text = ',' then
- Outputlabel.FinalLabel.Caption := 'Ошибка ввода'
- else
- Begin
- IsCorrect := false;
- if (Combobox1.Text <> INT_TYPE) And (ComboBox1.Text <> FLOAT_TYPE) then
- Begin
- Outputlabel.FinalLabel.Caption := 'Выберите тип данных';
- Outputlabel.Width := 400;
- End
- Else
- Begin
- if ComboBox1.Text = INT_TYPE then
- Try
- StrToInt(NumberInputLabel.Text);
- Outputlabel.FinalLabel.Caption := 'Верно';
- Outputlabel.Width := 300;
- IsCorrect := True;
- Except
- Outputlabel.FinalLabel.Caption := 'Ошибка ввода';
- Outputlabel.Width := 400;
- End;
- if ComboBox1.Text = FLOAT_TYPE then
- Try
- StrToFloat(NumberInputLabel.Text);
- Outputlabel.FinalLabel.Caption := 'Верно';
- IsCorrect := True;
- Except
- Outputlabel.FinalLabel.Caption := 'Ошибка ввода';
- End;
- End;
- End;
- end;
- procedure TInputForm.FileMenuClick(Sender: TObject);
- begin
- SaveFile.Enabled := OutputLabel.Showing;
- end;
- procedure TInputForm.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
- begin
- CanClose := MessageDlg('Вы уверены, что хотите выйти из программы?' +
- #10#13 + 'Все несохранённые данные будут утеряны.',
- mtConfirmation, [mbYes, mbNo], 0) = mrYes;
- end;
- procedure TInputForm.NumberInputLabelChange(Sender: TObject);
- begin
- OutputLabel.Hide;
- end;
- procedure TInputForm.NumberInputLabelKeyPress(Sender: TObject; var Key: Char);
- begin
- if not(Key in [#48 .. #57, #8, '.', ',', '-']) then
- Key := #0;
- if Key = '.' then
- key := ',';
- end;
- function CheckFile(Path: String): Boolean;
- var
- FileToCheck: TextFile;
- Num: Double;
- begin
- AssignFile(FileToCheck, Path);
- Reset(FileToCheck);
- CheckFile := true;
- try
- Readln(FileToCheck, Num);
- except
- CheckFile := false;
- end;
- if ((Num < -9999) or (Num > 9999)) then
- CheckFile := False;
- CloseFile(FileToCheck);
- end;
- procedure TInputForm.OpenFileClick(Sender: TObject);
- var
- InputFile: TextFile;
- Num: Double;
- IsCorrect: Boolean;
- begin
- IsCorrect := True;
- if OpenDialog.Execute then
- begin
- if IsFileExist(OpenDialog.FileName) then
- if CheckFile(OpenDialog.FileName) then
- begin
- AssignFile(InputFile, OpenDialog.FileName);
- Reset(InputFile);
- Readln(InputFile, Num);
- NumberInputLabel.Text := FloatToStr(Num);
- CloseFile(InputFile);
- ShowMessage('Данные из файла успешно загружены');
- end
- else
- Begin
- ShowMessage('Данные в файле некорректны.');
- IsCorrect := False
- End
- else
- Begin
- ShowMessage('Файл не существует.');
- IsCorrect := False
- End;
- if IsCorrect = True then
- Begin
- CalculateButton.Enabled;
- CalculateButton.Click;
- End;
- end;
- end;
- procedure TInputForm.SaveFileClick(Sender: TObject);
- Var
- OutputFile: TextFile;
- begin
- if SaveDialog.Execute() then
- Begin
- if IsFileExist(SaveDialog.FileName) then
- Begin
- AssignFile(OutputFile, SaveDialog.FileName);
- Rewrite(OutputFile);
- Write(OutputFile, 'Тип данных: ');
- Writeln(OutputFile, ComboBox1.Text);
- Write(OutputFile, 'Число: ');
- Writeln(OutputFile, NumberInputLabel.Text);
- Write(OutputFile, 'Вывод: ');
- Writeln(OutputFile, OutputLabel.FinalLabel.Caption);
- CloseFile(OutputFile);
- ShowMessage('Успешно сохранено');
- End
- else
- ShowMessage('Файл не существует.')
- End;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement