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, Vcl.ExtDlgs, Clipbrd;
- type
- TMainForm = class(TForm)
- ConditionLabel: TLabel;
- MainMenu: TMainMenu;
- FileMenuItem: TMenuItem;
- OpenMenuItem: TMenuItem;
- SaveMenuItem: TMenuItem;
- SaveAsMenuItem: TMenuItem;
- ManualMenuItem: TMenuItem;
- AboutDeveloperMenuItem: TMenuItem;
- Label1: TLabel;
- Label2: TLabel;
- Label3: TLabel;
- Label4: TLabel;
- Edit1: TEdit;
- Edit2: TEdit;
- Edit3: TEdit;
- CalculateButton: TButton;
- Label5: TLabel;
- AnswerLabel: TLabel;
- SaveDialog: TSaveDialog;
- Label6: TLabel;
- Label7: TLabel;
- Label8: TLabel;
- Edit4: TEdit;
- Edit5: TEdit;
- Edit6: TEdit;
- Label9: TLabel;
- Edit7: TEdit;
- procedure OpenMenuItemClick(Sender: TObject);
- procedure ManualMenuItemClick(Sender: TObject);
- procedure AboutDeveloperMenuItemClick(Sender: TObject);
- procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
- procedure CalculateButtonClick(Sender: TObject);
- procedure EditDblClick(Sender: TObject);
- procedure EditChange(Sender: TObject);
- procedure EditKeyPress(Sender: TObject; var Key: Char);
- procedure EditKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
- procedure CopyButtonClick(Sender: TObject);
- procedure PasteButtonClick(Sender: TObject);
- procedure CutButtonClick(Sender: TObject);
- procedure CopyPastePopupMenuPopup(Sender: TObject);
- procedure SaveAsMenuItemClick(Sender: TObject);
- procedure SaveMenuItemClick(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- var
- MainForm: TMainForm;
- const
- MAX = 1000;
- MIN = -1000;
- kNULL = #0;
- kBACKSPACE = #8;
- kMINUS = #45;
- kDOWN = 40;
- kUP = 38;
- kENTER = 13;
- kINSERT = 45;
- implementation
- {$R *.dfm}
- procedure TMainForm.AboutDeveloperMenuItemClick(Sender: TObject);
- begin
- MessageBox(Handle, 'Разработчик: Наривончик Александр Михайлович, гр. 351004', 'О разработчике', MB_OK Or MB_ICONINFORMATION);
- end;
- Function CalculateValue(X: Real): Real;
- Var
- A, B, C, D, E: Integer;
- Begin
- With MainForm Do
- Begin
- A := StrToInt(Edit1.Text);
- B := StrToInt(Edit2.Text);
- C := StrToInt(Edit3.Text);
- D := StrToInt(Edit4.Text);
- E := StrToInt(Edit5.Text);
- End;
- CalculateValue := ( A * X * X + B * X + C ) / ( D * X + E );
- End;
- Function CalculateResult():Real;
- Var
- A, B, C, D, E, X1, X2: Integer;
- K, M, N, T1, T2, Ymax: Real;
- Begin
- With MainForm Do
- Begin
- A := StrToInt(Edit1.Text);
- B := StrToInt(Edit2.Text);
- C := StrToInt(Edit3.Text);
- D := StrToInt(Edit4.Text);
- E := StrToInt(Edit5.Text);
- X1 := StrToInt(Edit6.Text);
- X2 := StrToInt(Edit7.Text);
- End;
- If X2 < X1 Then
- Begin
- X1 := X1 + X2;
- X2 := X1 - X2;
- X1 := X1 - X2;
- End;
- K := A * D;
- if K = 0 then
- Begin
- if (D = 0) And (A <> 0) then
- Begin
- T1 := -B / (2 * A);
- Ymax := CalculateValue(X1);
- If CalculateValue(X2) > Ymax Then
- Ymax := CalculateValue(X2);
- If ( T1 > X1 ) And ( T1 < X2 ) Then
- If CalculateValue(T1) > Ymax Then
- Ymax := CalculateValue(T1);
- End
- Else
- If (D <> 0) then
- Begin
- Ymax := CalculateValue(X1);
- If CalculateValue(X2) > Ymax Then
- Ymax := CalculateValue(X2);
- End;
- End
- Else
- Begin
- M := 2 * A * E;
- N := E * B - D * C;
- Ymax := CalculateValue(X1);
- If CalculateValue(X2) > Ymax Then
- Ymax := CalculateValue(X2);
- If ( M * M - 4 * K * N ) >= 0 Then
- Begin
- T1 := ( -M - Sqrt( M * M - 4 * K * N ) ) / ( 2 * K );
- T2 := ( -M + Sqrt( M * M - 4 * K * N ) ) / ( 2 * K );
- If ( T1 > X1 ) And ( T1 < X2 ) Then
- Begin
- If CalculateValue(T1) > Ymax Then
- Begin
- Ymax := CalculateValue(T1);
- End
- End;
- If( T2 > X1 ) And ( T2 < X2 )Then
- Begin
- If CalculateValue(T2) > Ymax Then
- Begin
- Ymax := CalculateValue(T2);
- End
- End;
- End;
- End;
- CalculateResult := Int(Ymax * 100)/100;
- End;
- Function CheckEdit (Edit: TEdit): Boolean;
- Begin
- With Edit Do
- Begin
- If (Length(Text) = 0) Or (Text = '-') Then
- CheckEdit := False
- Else
- CheckEdit := True;
- End;
- End;
- Procedure EditButton1Enable();
- var
- IsEditsCorrect: Boolean;
- I: Byte;
- Begin
- With MainForm Do
- Begin
- I := 0;
- Repeat
- Inc(I);
- IsEditsCorrect := CheckEdit(TEdit(FindComponent('Edit' + IntToStr(I))));
- Until (Not IsEditsCorrect) Or (I = 7);
- If IsEditsCorrect Then
- CalculateButton.Enabled := True
- Else
- CalculateButton.Enabled := False;
- End;
- End;
- procedure TMainForm.CalculateButtonClick(Sender: TObject);
- var
- Answer: Real;
- D, E, X1, X2: Integer;
- begin
- D := StrToInt(Edit4.Text);
- E := StrToInt(Edit5.Text);
- X1 := StrToInt(Edit6.Text);
- X2 := StrToInt(Edit7.Text);
- If (D*X1 + E = 0) Or (D * X2 + E = 0) Then
- MessageBox(Handle, 'Ошибка в задании функции (деление на 0)!', 'Ошибка', MB_OK Or MB_ICONERROR)
- Else
- Begin
- Answer := CalculateResult;
- AnswerLabel.Caption := FloatToStr(Answer);
- SaveAsMenuItem.Enabled := True;
- SaveMenuItem.Enabled := True;
- End;
- end;
- procedure TMainForm.CopyButtonClick(Sender: TObject);
- begin
- TEdit(ActiveControl).CopyToClipboard;
- end;
- procedure TMainForm.CopyPastePopupMenuPopup(Sender: TObject);
- var
- Num, Code: Integer;
- begin
- Val(Clipboard.AsText, Num, Code);
- If Clipboard.HasFormat(CF_TEXT) And ((Code = 0) And (Num < MAX+1) And (Num > MIN-1)) Then
- PasteButton.Enabled := True
- Else
- PasteButton.Enabled := False;
- end;
- procedure TMainForm.PasteButtonClick(Sender: TObject);
- var
- Num, Code: Integer;
- begin
- with TEdit(ActiveControl)do
- Begin
- PasteFromClipboard;
- Val(Text, Num, Code);
- If (Code <> 0) Or (Num < MIN) Or (Num > MAX) Then
- Begin
- Text := '';
- Beep;
- End;
- End;
- end;
- Procedure SaveAnswer ();
- var
- IsFileCorrect: Boolean;
- FileOut: TextFile;
- Path: String;
- Begin
- With MainForm Do
- Begin
- IsFileCorrect := True;
- Path := SaveDialog.FileName;
- AssignFile(FileOut, Path);
- Try
- Rewrite(FileOut);
- Except
- IsFileCorrect := False;
- MessageBox(Handle, 'Не удалось сохранить ответ в файл!', 'Ошибка', MB_OK Or MB_ICONERROR);
- End;
- If IsFileCorrect then
- Begin
- Writeln(FileOut, 'Наибольшее значение функции на промежутке равно: ', AnswerLabel.Caption);
- CloseFile(FileOut);
- MessageBox(Handle, 'Сохранено успешно!', 'Сохранение', MB_OK Or MB_ICONINFORMATION);
- End;
- End;
- End;
- procedure TMainForm.SaveAsMenuItemClick(Sender: TObject);
- begin
- If SaveDialog.Execute Then
- SaveAnswer();
- end;
- procedure TMainForm.SaveMenuItemClick(Sender: TObject);
- begin
- If(SaveDialog.FileName = 'Answer') Then
- Begin
- If SaveDialog.Execute Then
- SaveAnswer();
- End
- Else
- SaveAnswer();
- end;
- procedure TMainForm.CutButtonClick(Sender: TObject);
- begin
- TEdit(ActiveControl).CutToClipboard;
- end;
- ////////////////////// EDIT /////////////////////////////////////////////////
- procedure TMainForm.EditChange(Sender: TObject);
- var
- S:String;
- begin
- with Sender As TEdit do
- Begin
- if (Length(Text) > 1) then
- Begin
- if ((Text[1] = kMINUS ) Or (Text[1] = '0')) And (Text[2] = '0') then
- Begin
- S := Text;
- Delete(S, 2, 1);
- Text := S;
- End
- Else
- if (Text[1] = '0') And (Text[2] <> '0') then
- Begin
- S := Text;
- Delete(S, 1, 1);
- Text := S;
- End;
- End;
- EditButton1Enable;
- if AnswerLabel.Caption <> '...' then
- Begin
- AnswerLabel.Caption := '...';
- SaveAsMenuItem.Enabled := False;
- SaveMenuItem.Enabled := False;
- End;
- End;
- end;
- procedure TMainForm.EditDblClick(Sender: TObject);
- begin
- with Sender As TEdit do
- Text := '';
- end;
- procedure TMainForm.EditKeyDown(Sender: TObject; var Key: Word;
- Shift: TShiftState);
- Begin
- with MainForm, Sender As TEdit do
- case key of
- kENTER, kDOWN:
- SelectNext(ActiveControl, True, True);
- kUP:
- SelectNext(ActiveControl, False, True);
- kINSERT:
- Key := 0;
- end;
- End;
- procedure TMainForm.EditKeyPress(Sender: TObject; var Key: Char);
- var
- Num, Code: Integer;
- S: String;
- begin
- with Sender As TEdit Do
- case key of
- '0'..'9', kMINUS:
- Begin
- S := Text;
- Insert(Key, S, SelStart+1);
- Val (S, Num, Code);
- If (Code <> 0) And (S <> '-') Then
- Begin
- Key := kNULL;
- Beep;
- End
- Else
- If (Num > MAX) Or (Num < MIN) Then
- Begin
- Key := kNULL;
- Beep;
- End
- End;
- kBACKSPACE:;
- Else
- Key := kNULL;
- end;
- end;
- ////////////////////////////////////////////////////////////////////////////////
- procedure TMainForm.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
- begin
- CanClose := MessageBox(Handle, 'Вы действительно хотите выйти?', 'Вы уверены?', MB_YESNO Or MB_ICONQUESTION) = IDYES;
- end;
- procedure TMainForm.ManualMenuItemClick(Sender: TObject);
- begin
- MessageBox(Handle, '1. Последовательно введите все коэффициенты и задайте отрезок соответствующими значениями (целые числа от -1000 до 1000) в соответствующие поля.' + #13#10 + '2. Нажмите кнопку "Вычислить".' + #13#10 + '3. Получите результат!'+ #13#10 + '4. В случае ввода из файла убедитесь, что файл содержит все 7 чисел, записанных в отдельных строках', 'Инструкция', MB_OK Or MB_ICONINFORMATION);
- end;
- Function ReadNumFromFile(Var FileIn: TextFile; Var Num: Integer): Boolean;
- Var
- IsFileCorrect: Boolean;
- NumStr: String;
- Code: Integer;
- Begin
- Readln(FileIn, NumStr);
- Val(NumStr, Num, Code);
- If Code = 0 then
- IsFileCorrect := True
- Else
- Begin
- MessageBox(MainForm.Handle, 'Неверный формат данных в файле!', 'Ошибка', MB_OK Or MB_ICONERROR);
- IsFileCorrect := False;
- End;
- If (IsFileCorrect) And ((Num < MIN) Or (Num > MAX)) then
- Begin
- MessageBox(MainForm.Handle, 'Неверный формат данных в файле!', 'Ошибка', MB_OK Or MB_ICONERROR);
- IsFileCorrect := False;
- End;
- ReadNumFromFile := IsFileCorrect;
- End;
- procedure TMainForm.OpenMenuItemClick(Sender: TObject);
- var
- FileIn: TextFile;
- Path: String;
- A, B, C, D, E, X1, X2: Integer;
- IsFileCorrect: Boolean;
- begin
- If OpenDialog.Execute Then
- Begin
- IsFileCorrect := True;
- Path := OpenDialog.FileName;
- AssignFile(FileIn, Path);
- Try
- Reset(FileIn);
- Except
- IsFileCorrect := False;
- MessageBox(Handle, 'Не удалось открыть файл!', 'Ошибка', MB_OK Or MB_ICONERROR);
- End;
- If (IsFileCorrect) Then
- Begin
- IsFileCorrect := ReadNumFromFile(FileIn, A);
- If (IsFileCorrect) Then
- IsFileCorrect := ReadNumFromFile(FileIn, B);
- If (IsFileCorrect) Then
- IsFileCorrect := ReadNumFromFile(FileIn, C);
- If (IsFileCorrect) Then
- IsFileCorrect := ReadNumFromFile(FileIn, D);
- If (IsFileCorrect) Then
- IsFileCorrect := ReadNumFromFile(FileIn, E);
- If (IsFileCorrect) Then
- IsFileCorrect := ReadNumFromFile(FileIn, X1);
- If (IsFileCorrect) Then
- IsFileCorrect := ReadNumFromFile(FileIn, X2);
- If (IsFileCorrect) And Not EoF(FileIn) then
- Begin
- IsFileCorrect := False;
- MessageBox(MainForm.Handle, 'Неверный формат данных в файле!', 'Ошибка', MB_OK Or MB_ICONERROR);
- End;
- CloseFile(FileIn);
- If (IsFileCorrect) then
- Begin
- Edit1.Text := IntToStr(A);
- Edit2.Text := IntToStr(B);
- Edit3.Text := IntToStr(C);
- Edit4.Text := IntToStr(D);
- Edit5.Text := IntToStr(E);
- Edit6.Text := IntToStr(X1);
- Edit7.Text := IntToStr(X2);
- End;
- End;
- End;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement