public
{ Public declarations }
end;
var
Form1: TForm1;
a,b,c: real;
d: char; // объявить переменные
implementation
{$R *.dfm}
procedure TForm1.Button17Click(Sender: TObject); // кнопка "сброс"
begin
Label1.Caption:= ''; a:= 0; b:= 0; c:= 0;
end;
procedure TForm1.FormActivate(Sender: TObject); // при открытии окна
// программы
begin
a:= 0; b:= 0; c:= 0;
end;
procedure TForm1.Button13Click(Sender: TObject); // кнопка "/"
begin
a:= StrToFloat(Label1.Caption);
Label1.Caption:= '/';
d:= '/';
end;
procedure TForm1.Button14Click(Sender: TObject); // кнопка "*"
begin
a:= StrToFloat(Label1.Caption);
Label1.Caption:= '*';
d:= '*';
end;
procedure TForm1.Button15Click(Sender: TObject); // кнопка "-"
begin
a:= StrToFloat(Label1.Caption);
Label1.Caption:= '-';
d:= '-';
end;
procedure TForm1.Button16Click(Sender: TObject); // кнопка "+"
begin
a:= StrToFloat(Label1.Caption);
Label1.Caption:= '+';
d:= '+';
end;
procedure TForm1.Button12Click(Sender: TObject); // кнопка "="
begin
b:= StrToFloat(Label1.Caption);
if d = '/' then c:= a/b;
if d = '-' then c:= a-b;
if d = '*' then c:= a*b;
if d = '+' then c:= a+b;
Label1.Caption:= FloatToStr(c);
end;
procedure TForm1.Button10Click(Sender: TObject); // кнопка "0"
begin
if (Label1.Caption = '/') or
(Label1.Caption = '*') or
(Label1.Caption = '+') or
(Label1.Caption = '-') then Label1.Caption:= '0'
else Label1.Caption:= Label1.Caption + '0';
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if (Label1.Caption = '/') or // кнопка "1"
(Label1.Caption = '*') or
(Label1.Caption = '+') or
(Label1.Caption = '-') then Label1.Caption:= '1'
else Label1.Caption:= Label1.Caption + '1';
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
if (Label1.Caption = '/') or // кнопка "2"
(Label1.Caption = '*') or
(Label1.Caption = '+') or
(Label1.Caption = '-') then Label1.Caption:= '2'
else Label1.Caption:= Label1.Caption + '2';
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
if (Label1.Caption = '/') or // кнопка "3"
(Label1.Caption = '*') or
(Label1.Caption = '+') or
(Label1.Caption = '-') then Label1.Caption:= '3'
else Label1.Caption:= Label1.Caption + '3';
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
if (Label1.Caption = '/') or // кнопка "4"
(Label1.Caption = '*') or
(Label1.Caption = '+') or
(Label1.Caption = '-') then Label1.Caption:= '4'
else Label1.Caption:= Label1.Caption + '4';
end;
procedure TForm1.Button5Click(Sender: TObject);
begin
if (Label1.Caption = '/') or // кнопка "5"
(Label1.Caption = '*') or
(Label1.Caption = '+') or
(Label1.Caption = '-') then Label1.Caption:= '5'
else Label1.Caption:= Label1.Caption + '5';
end;
procedure TForm1.Button6Click(Sender: TObject);
begin
if (Label1.Caption = '/') or // кнопка "6"
(Label1.Caption = '*') or
(Label1.Caption = '+') or
(Label1.Caption = '-') then Label1.Caption:= '6'
else Label1.Caption:= Label1.Caption + '6';
end;
procedure TForm1.Button7Click(Sender: TObject);
begin
if (Label1.Caption = '/') or // кнопка "7"
(Label1.Caption = '*') or
(Label1.Caption = '+') or
(Label1.Caption = '-') then Label1.Caption:= '7'
else Label1.Caption:= Label1.Caption + '7';
end;
procedure TForm1.Button8Click(Sender: TObject);
begin
if (Label1.Caption = '/') or // кнопка "8"
(Label1.Caption = '*') or
(Label1.Caption = '+') or
(Label1.Caption = '-') then Label1.Caption:= '8'
else Label1.Caption:= Label1.Caption + '8';
end;
procedure TForm1.Button9Click(Sender: TObject);
begin
if (Label1.Caption = '/') or // кнопка "9"
(Label1.Caption = '*') or
(Label1.Caption = '+') or
(Label1.Caption = '-') then Label1.Caption:= '9'
else Label1.Caption:= Label1.Caption + '9';
end;
procedure TForm1.Button11Click(Sender: TObject);
begin
if (Label1.Caption = '/') or // кнопка ","
(Label1.Caption = '*') or
(Label1.Caption = '+') or
(Label1.Caption = '-') then Label1.Caption:= ','
else Label1.Caption:= Label1.Caption + ',';
end;
procedure TForm1.FormKeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin // нажатие клавиши*
if (Key>47) and (Key<58) then begin
if (Label1.Caption = '/') or
(Label1.Caption = '*') or
(Label1.Caption = '+') or
(Label1.Caption = '-') then Label1.Caption:= IntToStr(Key-48)
else Label1.Caption:= Label1.Caption + IntToStr(Key-48);
end;
if (Key>95) and (Key<106) then begin
if (Label1.Caption = '/') or
(Label1.Caption = '*') or
(Label1.Caption = '+') or
(Label1.Caption = '-') then Label1.Caption:= IntToStr(Key-96)
else Label1.Caption:= Label1.Caption + IntToStr(Key-96);
end;
if Key = 111 then Button13Click(sender); // "/"
if Key = 106 then Button14Click(sender); // "*"
if Key = 109 then Button15Click(sender); // "-"
if Key = 107 then Button16Click(sender); // "+"
if Key = 187 then Button12Click(sender); // "="
end;
end.
*) – нажатие клавиш будет обрабатываться,
если свойство формы KeyPreview установить в True.
Если Вы решите расширить возможности калькулятора, в Паскале возведение в степень (В = Аx) выполняется так: B := exp (x * ln (A)).
2.2. Простейшая экспертная система

unit Unit1; // модуль главной формы
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
jpeg, ExtCtrls, ComCtrls, ImgList, StdCtrls, Menus;
type
TForm1 = class(TForm)
TreeView1: TTreeView;
Edit1: TEdit;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
SaveDialog1: TSaveDialog;
Button5: TButton;
OpenDialog1: TOpenDialog;
MainMenu1: TMainMenu;
N1: TMenuItem;
N2: TMenuItem;
N3: TMenuItem;
N4: TMenuItem;
N5: TMenuItem;
N6: TMenuItem;
procedure FormCreate(Sender: TObject);
procedure TreeView1GetSelectedIndex(Sender: TObject; Node: TTreeNode);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure N2Click(Sender: TObject);
procedure N3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
MyNode: TTreeNode; // - объявить переменную
implementation
uses Unit2;
{$R *.DFM}
procedure TForm1.FormCreate(Sender: TObject);
begin
MyNode:= nil;
end;
procedure TForm1.TreeView1GetSelectedIndex(Sender: TObject;
Node: TTreeNode);
begin
MyNode:= Node;
end;
procedure TForm1.Button1Click(Sender: TObject);
// кнопка - узел
begin
if TreeView1.Items. Count = 0
then TreeView1.Items. Add(nil, Edit1.text)
else TreeView1.Items. Add(MyNode, Edit1.text);
end;
procedure TForm1.Button2Click(Sender: TObject);
// кнопка - потомок
begin
if TreeView1.Items. Count <> 0
then TreeView1.Items. AddChild(MyNode, Edit1.text)
end;
procedure TForm1.Button3Click(Sender: TObject);
// кнопка - удалить
begin
TreeView1.Items. Delete(MyNode);
end;
procedure TForm1.Button4Click(Sender: TObject);
// кнопка / сохранить
// дерево решений и
begin // соотв. команда в меню
if SaveDialog1.Execute then
TreeView1.SaveToFile(SaveDialog1.FileName);
end;
procedure TForm1.Button5Click(Sender: TObject);
// дерево решений /
// загрузить дерево решений
begin // и соотв. кнопка
if OpenDialog1.Execute then
TreeView1.LoadFromFile(OpenDialog1.FileName);
end;
procedure TForm1.N2Click(Sender: TObject);
// режим работы / пользователь
begin
Edit1.Visible:= False; N6.Enabled:= False;
Button1.Visible:= False; Button2.Visible:= False;
Button3.Visible:= False; Button4.Visible:= False;
end;
procedure TForm1.N3Click(Sender: TObject);
// режим работы / Эксперт
begin
Form2.Visible:= True;
end;
end.

unit Unit2; // модуль формы для задания пароля
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm2 = class(TForm)
Edit1: TEdit;
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormActivate(Sender: TObject);
private
{ Private declarations }
|
Из за большого объема этот материал размещен на нескольких страницах:
1 2 3 4 5 6 7 8 |


