Міністерство освіти і науки України
Хмельницький національний університет
Кафедра системного програмування
Лабораторна робота №4
з програмування
на тему:
«Ознайомлення з інструментальним середовищем програмування Delphi. Робота з простими візуальними об’єктами»
Лабораторна робота №3
Тема: Ознайомлення з інструментальним середовищем програмування Delphi. Робота з простими візуальними об’єктами.
Мета:ознайомитись з інструментальним середовищем програмування Delphi та його простими візуальними об’єктами.
Завдання
Розробити проект, що табулює задану функцію на відрізку АВ з кроком h=(B-A)/(n-1). На головній формі проекту вводяться початкові дані (початок та кінець відрізка АВ, крок табулювання h, кількість ітерацій n, а також значення додаткових змінних) та розміщуються кнопки: виклик форм відображення результатів та кнопка завершення проекту в цілому. На другій формі розмістити графік функції, при цьому заборонити закриття цієї форми стандартними засобами. На тертій формі розмістити результати табулювання функції у вигляді таблиці. Таблиця організована за допомогою компонента TStringGrid. При цьому в першій колонці вказується номер ітерації, в другій колонці – аргумент функції, в третій – результат обчислення, в четверту виводиться умова, відповідно до якої проведено обчислення функції.
Забезпечити введення початкових даних по замовчуванню. При введенні заданих величин або зміні їхнього значення автоматично повинно обчислюватись значення незаданої величини і виводитись у відповідне поле. При введенні некоректних даних в форму видати повідомлення  про помилку і без виведення результатів.
         (x-z)/x,                             0<=x<1.5
y =   (ln x2)/(lg x3) + M,           1.5<=x<2.5
        [cos(x/s) + sin(s/x) ]*z,    x>2.5
Unit1.pas
unit Unit1; 
{$mode objfpc}{$H+}
interface
uses
  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs, StdCtrls,
 unit2,unit3;
type
  { TForm1 }
  TForm1 = class(TForm)
    Button1: TButton;    Button2: TButton;    Button3: TButton;
    Edit1: TEdit;    Edit2: TEdit;    Edit3: TEdit;    Edit4: TEdit;
    Edit5: TEdit;    Edit6: TEdit;    Edit7: TEdit;
    GroupBox1: TGroupBox;    GroupBox2: TGroupBox;
    Label1: TLabel;    Label2: TLabel;    Label3: TLabel;    Label4: TLabel;
    Label5: TLabel;    Label6: TLabel;    Label7: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Edit1KeyPress(Sender: TObject; var Key: char);
    procedure Edit2KeyPress(Sender: TObject; var Key: char);
    procedure Edit3Change(Sender: TObject);
    procedure Edit3KeyPress(Sender: TObject; var Key: char);
    procedure Edit5KeyPress(Sender: TObject; var Key: char);
    procedure Edit6KeyPress(Sender: TObject; var Key: char);
    procedure Edit7KeyPress(Sender: TObject; var Key: char);
  private
    { private declarations }
  public
    { public declarations }
  end; 
var  Form1: TForm1;  A,B,Z,M,S:real; N,H:integer;
implementation
{ TForm1 }
procedure TForm1.Button1Click(Sender: TObject);
begin
 if MessageDlg('Табуляція','Дійсно закрити?',mtWarning,mbYesNo,0)=mrYes then Form1.Close;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
  Z:=StrToFloat(Edit5.Text);  M:=StrToFloat(Edit6.Text);   S:=StrToFloat(Edit7.Text);
  A:=StrToFloat(Edit1.Text);  B:=StrToFloat(Edit2.Text);   H:=StrToInt(Edit3.Text);
  if H<>0 then N:=trunc(abs(A-B)/H)+1;
  if (S=0) then ShowMessage('Змінна S не може дорівнювати 0!') else
  if (H=0) then ShowMessage('Крок H не може дорівнювати 0!') else  Form2.Show;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
   Z:=StrToFloat(Edit5.Text);  M:=StrToFloat(Edit6.Text);   S:=StrToFloat(Edit7.Text);
  A:=StrToFloat(Edit1.Text);  B:=StrToFloat(Edit2.Text);   H:=StrToInt(Edit3.Text);
  if H<>0 then N:=trunc(abs(A-B)/H)+1;
  if (S=0) then ShowMessage('Змінна S не може дорівнювати 0!') else
  if (H=0) then ShowMessage('Крок H не може дорівнювати 0!') else  Form3.Show;
end;
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: char);
begin
  case Key of  '0'..'9',#8: ;
  ',': if (pos(',',Edit1.Text)<>0) or (Edit1.Text='') then Key:=#0;
   else Key:=#0;  end;
end;
procedure TForm1.Edit2KeyPress(Sender: TObject; var Key: char);
begin
   case Key of  '0'..'9',#8: ;
  ',': if (pos(',',Edit2.Text)<>0) or (Edit2.Text='') then Key:=#0;
   else Key:=#0;  end;
end;
procedure TForm1.Edit3Change(Sender: TObject);
begin
  if Edit1.Text<>'' then A:=StrToFloat(Edit1.Text);
  if Edit2.Text<>'' then B:=StrToFloat(Edit2.Text);
  if Edit3.Text<>'' then H:=StrToInt(Edit3.Text);
  if (Edit1.Text<>'') and (Edit2.Text<>'') and (Edit3.Text<>'') and (H<>0) then begin N:=trunc(abs(A-B)/H)+1; Edit4.Text:=IntToStr(N); end;
  if (Edit1.Text<>'') and (Edit2.Text<>'') and (Edit3.Text<>'') and (Edit4.Text<>'') and (Edit5.Text<>'') and (Edit6.Text<>'') and (Edit7.Text<>'')
  then begin Button2.Enabled:=True; Button3.Enabled:=True; end else begin Button2.Enabled:=False; Button3.Enabled:=False; end;
end;
procedure TForm1.Edit3KeyPress(Sender: TObject; var Key: char);
begin
   case Key of  '0'..'9',#8: ;
   else Key:=#0;  end;
end;
procedure TForm1.Edit5KeyPress(Sender: TObject; var Key: char);
begin
    case Key of  '0'..'9',#8: ;
  ',': if (pos(',',Edit5.Text)<>0) or (Edit5.Text='') or (Edit5.Text='-') then Key:=#0;
  '-': if Edit5.Text<>'' then Key:=#0;
   else Key:=#0;  end;
end;
procedure TForm1.Edit6KeyPress(Sender: TObject; var Key: char);
begin
     case Key of  '0'..'9',#8: ;
  ',': if (pos(',',Edit6.Text)<>0) or (Edit6.Text='') or (Edit6.Text='-') then Key:=#0;
  '-': if Edit6.Text<>'' then Key:=#0;
   else Key:=#0;  end;
end;
procedure TForm1.Edit7KeyPress(Sender: TObject; var Key: char);
begin
     case Key of  '0'..'9',#8: ;
  ',': if (pos(',',Edit7.Text)<>0) or (Edit7.Text='') or (Edit7.Text='-') then Key:=#0;
  '-': if Edit7.Text<>'' then Key:=#0;
   else Key:=#0;  end;
end;
initialization
  {$I unit1.lrs}
end.
Unit2.pas
unit Unit2; 
{$mode objfpc}{$H+}
interface
uses
  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls;
type
  { TForm2 }
  TForm2 = class(TForm)
    Button1: TButton;    Button2: TButton;
    Panel1: TPanel;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end; 
var  Form2: TForm2; x0,y0:integer;
implementation
 uses Unit1;
{ TForm2 }
function F(x:real):real;
var ft:real;
begin  if x=0 then F:=0;
  if (x>0) and (x<1.5) then F:=(x-z)/x;
  if (x>=1.5) and (x<=2.5) then F:=ln(x*x)/(ln(x*x*x)/ln(10))+M;
  if (x>2.5) then F:=(cos(x/s)+sin(s/x))*z;
end;
procedure TForm2.Button1Click(Sender: TObject);
begin   Form2.Hide;
end;
procedure TForm2.Button2Click(Sender: TObject);
var i:integer; x,y:real;
begin
  x0:=(Panel1.Width-Panel1.Left) div 2; y0:=(Panel1.Height-Panel1.Top) div 2;
  Panel1.Canvas.Line(Panel1.Left,y0,Panel1.Left+Panel1.Width,y0);
  Panel1.Canvas.Line(x0,Panel1.Top,x0,Panel1.Top+Panel1.Height);
  if A>B then begin y:=A; a:=B; b:=y; end; x:=a;
  while x<=b do begin Panel1.Canvas.Line(x0+round((x)*10),y0-round(F(x)*10),x0+round((x+1)*10),y0-round(F(x+1)*10)); x:=x+1; end;
end;
initialization
  {$I unit2.lrs}
end.
Unit3.pas
unit Unit3; 
{$mode objfpc}{$H+}
interface
uses
  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs, StdCtrls, Grids,
  Unit2;
type
  { TForm3 }
  TForm3 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    SG: TStringGrid;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end; 
var  Form3: TForm3; 
implementation
 uses Unit1;
{ TForm3 }
procedure TForm3.Button1Click(Sender: TObject);
var i,j:integer;
begin  Form3.Hide;
  for i:=0 to SG.ColCount-1 do for j:=0 to SG.RowCount-1 do SG.Cells[i,j]:='';
end;
procedure TForm3.Button2Click(Sender: TObject);
var i:integer; x:real;
begin
  SG.RowCount:=N+1; if A<B then x:=A else x:=B;
  for i:=1 to N do begin
  SG.Cells[0,i]:=IntToStr(i);
  SG.Cells[1,i]:=FloatToStr(x);
  SG.Cells[2,i]:=FloatToStr(F(x));
  if (x>0) and (x<1.5) then SG.Cells[3,i]:='(x-z)/x';
  if (x>=1.5) and (x<=2.5) then SG.Cells[3,i]:='ln(x*x)/(ln(x*x*x)/ln(10))+M';
  if (x>2.5) then SG.Cells[3,i]:='(cos(x/s)+sin(s/x))*z';
  x:=x+h;
  end;
end;
initialization
  {$I unit3.lrs}
end.
Висновок.  Виконавши лабораторну роботу, я освоїв роботу програм з багатьма формами, а також закріпив навички роботи з простими компонентами середовища Delphi, такими як TLabel, TEdit, TButton, опрацював способи побудови графіків за допомогою властивості Canvas, засвоїв навички створення найпростіших обробників подій з можливістю перевірки вхідних даних та виведення інформаційних повідомлень, а також навчився працювати з таблицями компоненту StringGrid.
/
/
/