|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 kamalo 于 2016-12-10 09:24 编辑
大神莫笑我
简单计算器
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Buttons, StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
SpeedButton1: TSpeedButton; //计算器上有16个按钮
SpeedButton2: TSpeedButton;
SpeedButton3: TSpeedButton;
SpeedButton4: TSpeedButton;
SpeedButton5: TSpeedButton;
SpeedButton6: TSpeedButton;
SpeedButton7: TSpeedButton;
SpeedButton8: TSpeedButton;
SpeedButton9: TSpeedButton;
SpeedButton10: TSpeedButton;
SpeedButton11: TSpeedButton;
SpeedButton12: TSpeedButton;
SpeedButton13: TSpeedButton;
SpeedButton14: TSpeedButton;
SpeedButton15: TSpeedButton;
SpeedButton16: TSpeedButton;
procedure FormCreate(Sender: TObject);
procedure SpeedButton1Click(Sender: TObject);
procedure SpeedButton2Click(Sender: TObject);
procedure SpeedButton3Click(Sender: TObject);
procedure SpeedButton4Click(Sender: TObject);
procedure SpeedButton7Click(Sender: TObject);
procedure SpeedButton8Click(Sender: TObject);
procedure SpeedButton9Click(Sender: TObject);
procedure SpeedButton13Click(Sender: TObject);
procedure SpeedButton14Click(Sender: TObject);
procedure SpeedButton15Click(Sender: TObject);
procedure SpeedButton5Click(Sender: TObject);
procedure SpeedButton6Click(Sender: TObject);
procedure SpeedButton10Click(Sender: TObject);
procedure SpeedButton11Click(Sender: TObject);
procedure SpeedButton12Click(Sender: TObject);
procedure SpeedButton16Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
temp1:string; //存储第一次Edit1中的字符串
temp2:real; //计算结果为实数
flag:integer; //用于加减乘除在case中的条件
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
Edit1.Text:=''; //清空Edit1
end;
procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
Edit1.Text:= Edit1.Text + '1'; //可以多次向Edit1中填入1
end;
procedure TForm1.SpeedButton2Click(Sender: TObject);
begin
Edit1.Text:= Edit1.Text + '2'; //可以多次向Edit1中填入2
end;
procedure TForm1.SpeedButton3Click(Sender: TObject);
begin
Edit1.Text:= Edit1.Text + '3';
end;
procedure TForm1.SpeedButton4Click(Sender: TObject);
begin
Edit1.Text:= Edit1.Text + '0';
end;
procedure TForm1.SpeedButton7Click(Sender: TObject);
begin
Edit1.Text:= Edit1.Text + '4';
end;
procedure TForm1.SpeedButton8Click(Sender: TObject);
begin
Edit1.Text:= Edit1.Text + '5';
end;
procedure TForm1.SpeedButton9Click(Sender: TObject);
begin
Edit1.Text:= Edit1.Text + '6';
end;
procedure TForm1.SpeedButton13Click(Sender: TObject);
begin
Edit1.Text:= Edit1.Text + '7';
end;
procedure TForm1.SpeedButton14Click(Sender: TObject);
begin
Edit1.Text:= Edit1.Text + '8';
end;
procedure TForm1.SpeedButton15Click(Sender: TObject);
begin
Edit1.Text:= Edit1.Text + '9';
end;
procedure TForm1.SpeedButton5Click(Sender: TObject);
begin
flag:= 1; //加法在case条件中设为1
temp1:=Edit1.Text; //将第一次输入的字符串存储在变量temp1中
Edit1.Text:='';
end;
procedure TForm1.SpeedButton6Click(Sender: TObject);
begin
flag:= 2; //减法在case条件中设为2
temp1:=Edit1.Text;
Edit1.Text:='';
end;
procedure TForm1.SpeedButton10Click(Sender: TObject);
begin
flag:= 3;
temp1:=Edit1.Text;
Edit1.Text:='';
end;
procedure TForm1.SpeedButton11Click(Sender: TObject);
begin
flag:= 4;
temp1:=Edit1.Text;
Edit1.Text:='';
end;
procedure TForm1.SpeedButton12Click(Sender: TObject);
begin
case flag of
1: temp2:=strtofloat(Edit1.Text) + strtofloat(temp1); //分别计算加减乘除,结果赋值给实数变量temp2
2: temp2:=strtofloat(temp1) - strtofloat(Edit1.Text);
3: temp2:=strtofloat(Edit1.Text) * strtofloat(temp1);
4: temp2:=strtofloat(temp1) / strtofloat(Edit1.Text);
end;
edit1.Text:=floattostr(temp2); //将计算所得结果实数变量temp2变成字符串赋值给Edit1
end;
procedure TForm1.SpeedButton16Click(Sender: TObject);
begin
Edit1.Text:=''; //实现复位按钮功能
end;
end. |
|