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;
- Edit2: TEdit;
- Label3: TLabel;
- Label4: TLabel;
- Edit1: TEdit;
- Button1: TButton;
- Label7: TLabel;
- Label8: TLabel;
- MainMenu1: TMainMenu;
- PopupMenu1: TPopupMenu;
- N1: TMenuItem;
- N2: TMenuItem;
- N3: TMenuItem;
- N4: TMenuItem;
- N5: TMenuItem;
- OpenDialog1: TOpenDialog;
- SaveDialog1: TSaveDialog;
- procedure Edit1Change(Sender: TObject);
- procedure Button1Click(Sender: TObject);
- procedure Edit1KeyPress(Sender: TObject; var Key: Char);
- procedure Edit2KeyPress(Sender: TObject; var Key: Char);
- procedure N4Click(Sender: TObject);
- procedure N5Click(Sender: TObject);
- procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
- procedure N2Click(Sender: TObject);
- procedure N3Click(Sender: TObject);
- procedure Edit2Change(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- var
- Form1: TForm1;
- Path, Result : String;
- IsFileOpen, Flag : Boolean;
- implementation
- {$R *.dfm}
- procedure TForm1.Button1Click(Sender: TObject);
- var
- N : Integer;
- Eps, X, Y0, Y: Real;
- begin
- Try
- X := StrToFloat(Edit1.Text);
- Except
- Application.MessageBox('Что-то не так', 'Ашыпка', 0);
- Edit1.Text := '';
- End;
- Try
- Eps := StrToFloat(Edit2.Text);
- Except
- Application.MessageBox('Что-то не так', 'Ашыпка', 0);
- Edit2.Text := '';
- End;
- Y0 := 1;
- N := 0;
- Repeat
- Y := Y0;
- Y0 := 0.5 * (Y + X / Y);
- Inc(N);
- Until (abs(Y - Y0) <= Eps);
- Label7.Caption := (FormatFloat('0.00000000',Y0));
- Label8.Caption := IntToStr(N);
- N3.Enabled := True;
- Result := 'Y = ' + Label7.Caption + ' and ' +'N = ' + Label8.Caption;
- end;
- procedure TForm1.Edit1Change(Sender: TObject);
- begin
- N3.Enabled := False;
- If (Length(Edit1.Text) = 0) or (Length(Edit2.Text) = 0) then
- Button1.Enabled := False
- else
- Button1.Enabled := True;
- Label7.Caption := '';
- Label8.Caption := '';
- end;
- procedure TForm1.Edit2Change(Sender: TObject);
- begin
- If (Length(Edit1.Text) = 0) or (Length(Edit2.Text) = 0) then
- Button1.Enabled := False
- else
- Button1.Enabled := True;
- If (Length(Edit2.Text) <> 0) then
- If (StrToFloat(Edit2.Text) = 0) or (Edit2.Text = '0,') or (Length(Edit1.Text) = 0) then
- Button1.Enabled := False
- else
- Button1.Enabled := True;
- Label7.Caption := '';
- Label8.Caption := '';
- end;
- procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
- var
- N : Integer;
- 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) = 8) Then
- If (Not(Key In [#08, #46])) Then
- Key := #0;
- if (Length(Edit1.Text) = 0) then
- If (Not(Key In ['0'..'9', #08, #46])) Then
- Key := #0;
- N := Ansipos(',', Edit1.Text);
- If (Key = ',') and (N <> 0) then
- If (Not(Key In ['0'..'9', #08, #46])) Then
- Key := #0;
- If (Edit1.Text = '0') and Not(Key in [',', #08, #46]) then
- Key := #0;
- If Key = '.' then
- Key := #0;
- end;
- procedure TForm1.Edit2KeyPress(Sender: TObject; var Key: Char);
- var
- N : Integer;
- begin
- If ((Key = #13) And (Button1.Enabled)) Then
- Button1.Click;
- If (Not(Key In ['0'..'9', #08, #46, ','])) Then
- Key := #0;
- If (Length(Edit2.Text) = 8) Then
- If (Not(Key In [#08, #46])) Then
- Key := #0;
- if (Length(Edit2.Text) = 0) then
- If (Not(Key In ['0'..'9', #08, #46])) Then
- Key := #0;
- N := Ansipos(',', Edit2.Text);
- If (Key = ',') and (n <> 0) then
- If (Not(Key In ['0'..'9', #08, #46])) Then
- Key := #0;
- If (Edit2.Text = '0') and Not(Key in [',', #08, #46]) then
- Key := #0;
- If Key = '.' then
- Key := #0;
- end;
- procedure TForm1.N4Click(Sender: TObject);
- begin
- Application.MessageBox('Программа вычисляет значение квадратного корня с точностью, вводимой пользователем. Также подсчитывает количество итераций, за которое достигается эта точность.', 'Инструкция', 0);
- end;
- procedure TForm1.N5Click(Sender: TObject);
- begin
- Application.MessageBox('Арефин Владислав гр.251004', 'Разрабочик', 0);
- end;
- procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
- begin
- CanClose := MessageBox(Form1.Handle, 'Вы уверены, что хотите выйти?', 'Выход', MB_YESNO + MB_ICONQUESTION)=ID_YES;
- end;
- Function CheckFileData1(Num: String): Boolean;
- Const
- MAX_LIM = 99999999;
- MIN_LIM = 0;
- Var
- NewNum: Real;
- IsCorrect: Boolean;
- Begin
- NewNum := 0;
- IsCorrect := True;
- Try
- NewNum := StrToFloat(Num);
- Except
- MessageBox(Form1.Handle, Pchar('Проверьте корректность данных.'), 'Ошибка', MB_ICONSTOP);
- IsCorrect := False;
- End;
- If(IsCorrect And ((NewNum > MAX_LIM) Or (NewNum < MIN_LIM))) Then
- Begin
- Num := FloatToStr(NewNum);
- MessageBox(Form1.Handle, Pchar('Недопустимое значение!'), 'Ошибка', MB_ICONSTOP);
- IsCorrect := False;
- End;
- CheckFileData1 := IsCorrect;
- End;
- Function CheckFileData2(Num: String): Boolean;
- Const
- MAX_LIM = 99999999;
- MIN_LIM = 0;
- Var
- NewNum: Real;
- IsCorrect: Boolean;
- Begin
- NewNum := 0;
- IsCorrect := True;
- Try
- NewNum := StrToFloat(Num);
- Except
- MessageBox(Form1.Handle, Pchar('Проверьте корректность данных.'), 'Ошибка', MB_ICONSTOP);
- IsCorrect := False;
- End;
- If(IsCorrect And ((NewNum > MAX_LIM) Or (NewNum <= MIN_LIM))) Then
- Begin
- Num := FloatToStr(NewNum);
- MessageBox(Form1.Handle, Pchar('Недопустимое значение!'), 'Ошибка', MB_ICONSTOP);
- IsCorrect := False;
- End;
- CheckFileData2 := 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 TakeDataFromFile1(Number1: String; Var FileOutput: TextFile): String;
- Var
- IsRight: Boolean;
- Begin
- IsRight := True;
- Try
- Readln(FileOutput, Number1);
- IsRight := CheckFileData1(Number1);
- Except
- End;
- If(Not(IsRight)) Then
- TakeDataFromFile1 := ''
- Else
- TakeDataFromFile1 := Number1;
- End;
- Function TakeDataFromFile2(Number2: String; Var FileOutput: TextFile): String;
- Var
- IsRight : Boolean;
- Begin
- IsRight := True;
- Try
- Read(FileOutput, Number2);
- IsRight := CheckFileData2(Number2);
- Except
- End;
- If(Not(IsRight)) Then
- TakeDataFromFile2 := ''
- Else
- TakeDataFromFile2 := Number2;
- End;
- procedure TForm1.N2Click(Sender: TObject);
- Var
- FileInput: TextFile;
- Num : Integer;
- Str1, Str2 : String;
- begin
- Num := 0;
- Path := Open;
- AssignFile(FileInput, Path);
- Reset(FileInput);
- If(IsFileOpen) Then
- Begin
- Str1 := TakeDataFromFile1(IntToStr(Num), FileInput);
- if Str1 <> '' then
- Str2 := TakeDataFromFile2(IntToStr(Num), FileInput);
- End;
- if Not((Str1 = '') or (Str2 = '')) then
- Begin
- Edit1.Text := Str1;
- Edit2.Text := Str2;
- End
- Else
- Begin
- Edit1.Text := #0;
- Edit2.Text := #0;
- End;
- CloseFile(FileInput);
- end;
- procedure TForm1.N3Click(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);
- Label7.Caption := '';
- Label8.Caption := '';
- Edit1.Text := '';
- Edit2.Text := '';
- End;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement