Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit Unit1;
- 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
- TForm1 = class(TForm)
- Label1: TLabel;
- Label2: TLabel;
- Button1: TButton;
- Label3: TLabel;
- Edit1: TEdit;
- MainMenu1: TMainMenu;
- PopupMenu1: TPopupMenu;
- SaveDialog1: TSaveDialog;
- OpenDialog1: TOpenDialog;
- dd1: TMenuItem;
- N1: TMenuItem;
- N2: TMenuItem;
- N3: TMenuItem;
- N4: TMenuItem;
- procedure Button1Click(Sender: TObject);
- procedure Edit1Change(Sender: TObject);
- procedure Edit1KeyPress(Sender: TObject; var Key: Char);
- procedure N1Click(Sender: TObject);
- procedure N2Click(Sender: TObject);
- procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
- procedure N3Click(Sender: TObject);
- procedure N4Click(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- var
- Form1: TForm1;
- Path, Result : String;
- IsFileOpen : Boolean;
- implementation
- {$R *.dfm}
- procedure TForm1.Button1Click(Sender: TObject);
- Var
- Degree, I, Number, TempResult : Integer;
- Const
- Step = 6;
- Label2Start = 101;
- Label3Start = 114;
- begin
- Degree := StrToInt(Edit1.Text);
- Number := 1;
- For I := 1 to Degree do
- Number := Number * 2;
- // этот кусочек отвечает за сдвиг пропорционально кол-ву цифр в результате
- Label3.Left := Label3Start;
- Label2.Left := Label2Start;
- TempResult := Number;
- While ((TempResult div 10) > 0) do
- Begin
- Label3.Left := Label3.Left - Step;
- Label2.Left := Label2.Left - Step;
- TempResult := TempResult div 10;
- End;
- Label2.Caption := '2 ' + ' = ' + IntToStr(Number);
- Label3.Caption := IntToStr(Degree);
- Result := '2 в степени ' + IntToStr(Degree) + ' равно ' + IntToStr(Number);
- N4.Enabled := True;
- end;
- procedure TForm1.Edit1Change(Sender: TObject);
- begin
- N4.Enabled := False;
- If ((Length(Edit1.Text) = 0)) then
- Begin
- Button1.Enabled := False;
- End
- Else
- Begin
- Button1.Enabled := True;
- End;
- Label2.Caption := '';
- Label3.Caption := '';
- end;
- procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
- begin
- If ((Key = #13) And (Button1.Enabled)) Then
- Button1.Click;
- If (Not(Key In ['0'..'9', #08, #46])) Then
- Key := #0;
- If ((Length(Edit1.Text) = 0) and (Key = '0')) Then
- Key := #0;
- If ((Key = #08) or (Key = #46)) then
- Begin
- Label3.Caption := '';
- Label2.Caption := '';
- End;
- If ((Length(Edit1.Text) = 1) and (StrToInt(Edit1.Text) = 3)) then
- If Not(Key In [#08, #46, '0']) Then
- Key := #0;
- If ((Length(Edit1.Text) = 2)) then
- If Not(Key In [#08, #46]) Then
- Key := #0;
- If (Length(Edit1.Text) = 1) and Not((StrToInt(Edit1.Text) = 2) or (StrToInt(Edit1.Text) = 1)) then
- Begin
- If (StrToInt(Edit1.Text) = 3) then
- If Not(Key In [#08, #46, '0']) Then
- Key := #0;
- If Not(Key In [#08, #46]) and Not(StrToInt(Edit1.Text) = 3) Then
- Key := #0;
- End;
- end;
- procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
- begin
- CanClose := MessageBox(Form1.Handle, 'Вы уверены, что хотите выйти?', 'Выход', MB_YESNO + MB_ICONQUESTION)=ID_YES;
- end;
- procedure TForm1.N1Click(Sender: TObject);
- begin
- Application.MessageBox('Программа вычисляет 2 в степени n, где n - натуральное число.'#13#10'Это лабораторная 1.2 (в название формы не поместилось)'#13#10'Внимание! Степень числа должна быть не более 30.', 'Инструкция', 0);
- end;
- procedure TForm1.N2Click(Sender: TObject);
- begin
- Application.MessageBox('Арефин Владислав гр.251004', 'Разрабочик', 0);
- end;
- Function CheckFileData(Num: String): Boolean;
- Const
- MAX_LIM = 30;
- MIN_LIM = 1;
- Var
- NewNum: Integer;
- IsCorrect: Boolean;
- Begin
- NewNum := 0;
- IsCorrect := True;
- Try
- NewNum := StrToInt(Num);
- Except
- MessageBox(Form1.Handle, Pchar('Проверьте корректность данных.'), 'Ошибка', MB_ICONSTOP);
- IsCorrect := False;
- End;
- If(IsCorrect And ((NewNum > MAX_LIM) Or (NewNum < MIN_LIM))) Then
- Begin
- Num := IntToStr(NewNum);
- MessageBox(Form1.Handle, Pchar('Недопустимое значение!'), 'Ошибка', MB_ICONSTOP);
- IsCorrect := False;
- End;
- CheckFileData := IsCorrect;
- End;
- Function Open (): String;
- Begin
- With Form1 Do
- Begin
- If OpenDialog1.Execute Then
- Begin
- Path := OpenDialog1.FileName;
- IsFileOpen := True;
- End
- Else
- IsFileOpen := False;
- End;
- Open := Path;
- End;
- Function TakeDataFromFile(Side: String; Var FileOutput: TextFile): String;
- Var
- IsRight : Boolean;
- Begin
- IsRight := True;
- Try
- Read(FileOutput, Side);
- IsRight := CheckFileData(Side);
- Except
- End;
- If(Not(IsRight)) Then
- TakeDataFromFile := ''
- Else
- TakeDataFromFile := Side;
- End;
- procedure TForm1.N3Click(Sender: TObject);
- Var
- FileInput: TextFile;
- Num : Integer;
- Str : String;
- begin
- Num := 0;
- Path := Open;
- AssignFile(FileInput, Path);
- Reset(FileInput);
- If(IsFileOpen) Then
- Str := TakeDataFromFile(IntToStr(Num), FileInput);
- if (Length(Str) > 0) then
- Begin
- Edit1.Text := Str;
- End
- Else
- Edit1.Text := #0;
- CloseFile(FileInput);
- end;
- procedure TForm1.N4Click(Sender: TObject);
- Var
- FileOutput: TextFile;
- begin
- Path := Open;
- If (IsFileOpen) Then
- Begin
- AssignFile(FileOutput, Path);
- Rewrite(FileOutput);
- Write(FileOutput, Result);
- Application.MessageBox('Запись файла выполнена успешно.', 'Результат', 0);
- CloseFile(FileOutput);
- Label3.Caption := '';
- Label2.Caption := '';
- Edit1.Text := '';
- End;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement