看到别的版中大多都有关于计算器的源码,所以我就写一个给我们版。
没做过多的测试,如有不正确的地方还请大家指教。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Button5: TButton;
Button6: TButton;
Button7: TButton;
Button8: TButton;
Button9: TButton;
Button10: TButton;
Button11: TButton;
Button12: TButton;
Button13: TButton;
Button14: TButton;
Button15: TButton;
Button16: TButton;
Edit1: TEdit;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
type
TMyysf=(Add,Minus,Plus,Di); //枚举类型
type
TMyComputer=class(Tobject) //生明一个类
private //私有变量
//当xj为真时,edit1.text续写,否则重写
xj:boolean;
Myysf:TMyysf;
x1,x2:single;
property MyXj:boolean read xj write xj;
public //公有变量
constructor Create;
//运算过程
procedure Compute(Sender:TObject);
//为变量1赋值,并记录运算符
procedure ysf(Sender:Tobject);
//输入数字
procedure srsz(Sender:Tobject);
//键盘输入
procedure KeyPress(Sender:Tobject;var Key:Char);
//小数点的输入
procedure Point(Sender:Tobject);
end;
var
Form1: TForm1;
mycomputer:Tmycomputer; //定义一个对象
implementation
{$R *.dfm}
constructor TMyComputer.Create;
begin
self.xj:=True;
end;
procedure TMyComputer.KeyPress(Sender:Tobject;var Key:Char);
var
i:Integer;
begin
for i:=0 to Form1.ControlCount-1 do
begin
if (Form1.Controls[i] as TButton).Caption=Key then
begin
//模拟键盘按下的事件。
SendMessage((Form1.Controls[i] as TButton).Handle, BM_SETSTATE, 1, 0);
Sleep(100);
SendMessage((Form1.Controls[i] as TButton).Handle, BM_SETSTATE, 0, 0);
(Form1.Controls[i] as TButton).OnClick((Form1.Controls[i] as TButton));
exit;
end;
end;
end;
//各运算符的过程
procedure TMyComputer.ysf(Sender:Tobject);
begin
if Sender=form1.button10 then
self.Myysf:=add
else if Sender=form1.button11 then
self.Myysf:=minus
else if Sender=form1.button12 then
self.Myysf:=plus
else if Sender=form1.button13 then
self.Myysf:=di;
self.x1:=strtofloat(form1.Edit1.Text);
self.MyXj:=False;
end;
//等号事件
procedure TMyComputer.Compute(Sender:TObject);
begin
self.x2:=strtofloat(form1.Edit1.Text);
if Myysf=add then
form1.Edit1.Text:=floattostr(x1+x2)
else if myysf=minus then
form1.Edit1.Text:=floattostr(x1-x2)
else if myysf=plus then
form1.Edit1.Text:=floattostr(x1*x2)
else if myysf=di then
begin
if x2=0 then
begin
messagebox(form1.Handle,'除数不能为0','错误',0+16);
exit;
end
else
form1.Edit1.Text:=floattostr(x1/x2);
end;
self.MyXj:=false;
end;
//输入数字
procedure TMyComputer.srsz(Sender:Tobject);
begin
if self.MyXj then
if form1.Edit1.Text<>'0' then
form1.Edit1.Text:=form1.Edit1.Text+(Sender as TButton).Caption
else
form1.Edit1.Text:=(Sender as TButton).Caption
else
form1.Edit1.Text:=(Sender as TButton).Caption;
self.MyXj:=True;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
i:integer;
begin
mycomputer:=TMyComputer.Create;
for i:=0 to self.ControlCount-1 do
begin
if (self.Controls[i] is TButton) then
begin
if (((form1.Controls[i] as TButton).Caption>='0') and
((form1.Controls[i] as TButton).Caption<='9')) then
begin
//单击事件
(self.Controls[i] as TButton).OnClick:=mycomputer.srsz;
end
else if ((form1.Controls[i] as TButton).Caption>='=') then
begin
(self.Controls[i] as TButton).OnClick:=mycomputer.Compute;
end
else if (self.Controls[i] as TButton).Caption='.' then
begin
(self.Controls[i] as TButton).OnClick:=mycomputer.Point;
end
else
begin
(self.Controls[i] as TButton).OnClick:=mycomputer.ysf;
end;
//按键事件
(form1.Controls[i] as TButton).OnKeyPress:=MyComputer.KeyPress;
end;
end;
end;
//.
procedure TMyComputer.Point(Sender: TObject);
var
f:single;
begin
//判断是转换成float。
if trystrtofloat(form1.edit1.Text+'.',f) then
mycomputer.srsz(Sender);
end;
end.