Delphi7.0的计算器代码

 我来答
longchongyang
2008-03-28 · 超过60用户采纳过TA的回答
知道小有建树答主
回答量:304
采纳率:0%
帮助的人:179万
展开全部
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls, Buttons, Menus;

type
TForm1 = class(TForm)
Edit1: TEdit;
Panel1: TPanel;
SpeedButton1: TSpeedButton;
SpeedButton2: TSpeedButton;
SpeedButton3: TSpeedButton;
SpeedButton4: TSpeedButton;
SpeedButton6: TSpeedButton;
SpeedButton7: TSpeedButton;
SpeedButton8: TSpeedButton;
SpeedButton9: TSpeedButton;
SpeedButton10: TSpeedButton;
SpeedButton12: TSpeedButton;
SpeedButton13: TSpeedButton;
SpeedButton14: TSpeedButton;
SpeedButton15: TSpeedButton;
SpeedButton16: TSpeedButton;
SpeedButton17: TSpeedButton;
SpeedButton11: TSpeedButton;
SpeedButton18: TSpeedButton;
SpeedButton19: TSpeedButton;
SpeedButton20: TSpeedButton;
SpeedButton21: TSpeedButton;
SpeedButton5: TSpeedButton;
MainMenu1: TMainMenu;
zhua1: TMenuItem;
N1: TMenuItem;
N2: TMenuItem;
N3: TMenuItem;
SpeedButton22: TSpeedButton;
N4: TMenuItem;
procedure SpeedButton20Click(Sender: TObject);
procedure SpeedButton1Click(Sender: TObject);
procedure SpeedButton2Click(Sender: TObject);
procedure SpeedButton3Click(Sender: TObject);
procedure SpeedButton4Click(Sender: TObject);
procedure SpeedButton6Click(Sender: TObject);
procedure SpeedButton7Click(Sender: TObject);
procedure SpeedButton13Click(Sender: TObject);
procedure SpeedButton12Click(Sender: TObject);
procedure SpeedButton14Click(Sender: TObject);
procedure SpeedButton15Click(Sender: TObject);
procedure SpeedButton17Click(Sender: TObject);
procedure SpeedButton5Click(Sender: TObject);
procedure SpeedButton19Click(Sender: TObject);
procedure SpeedButton11Click(Sender: TObject);
procedure SpeedButton18Click(Sender: TObject);
procedure SpeedButton21Click(Sender: TObject);
procedure SpeedButton16Click(Sender: TObject);
procedure N1Click(Sender: TObject);
procedure SpeedButton22Click(Sender: TObject);
procedure N2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure N4Click(Sender: TObject);
procedure Edit1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure Edit1KeyPress(Sender: TObject; var Key: Char);

private
//将窗体变量放置在这里也可以,这样如果有
//Form2就无法访问

{ Private declarations }
public

{ Public declarations }
end;

var

Form1: TForm1;
flag : Integer =0;
flag1: Integer =0; // 是否复位的标记
flag3: Integer =0; //小数点的标记
Temp1: string = '';
Temp2: real=0; //注意变量的定义

implementation

{$R *.dfm}

procedure TForm1.SpeedButton20Click(Sender: TObject);
begin
if SpeedButton20.Caption = '关闭' then
begin
SpeedButton20.Caption := '打开' ;
SpeedButton20.Font.Color := clGreen
end
else
begin
SpeedButton20.Caption := '关闭';
SpeedButton20.Font.Color := clSilver
end;
end;

procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
Edit1.Font.Color := clBlack;
if flag1=5 then Edit1.Text := '';
if SpeedButton20.Caption = '打开' then
begin
Edit1.Text := Edit1.text+'1';flag1 := 0;
end
end;

procedure TForm1.SpeedButton2Click(Sender: TObject);
begin
Edit1.Font.Color := clBlack;
if flag1 = 5 then Edit1.Text :='';
if SpeedButton20.Caption = '打开' then
begin
Edit1.Text := Edit1.text+'2'; flag1 := 0;
end;
end;

procedure TForm1.SpeedButton3Click(Sender: TObject);
begin
Edit1.Font.Color := clBlack;
if flag1 = 5 then Edit1.Text := '';
if SpeedButton20.Caption='打开' then
begin
Edit1.Text := Edit1.text+'3'; flag1:=0;
end;
end;

procedure TForm1.SpeedButton4Click(Sender: TObject);
begin
Edit1.Font.Color := clBlack;
if flag1 = 5 then Edit1.Text := '';
if SpeedButton20.Caption='打开' then
begin
Edit1.Text := Edit1.text+'4'; flag1 := 0;
end;
end;

procedure TForm1.SpeedButton6Click(Sender: TObject);
begin
Edit1.Font.Color := clBlack;
if flag1 = 5 then Edit1.Text := '';
if SpeedButton20.Caption='打开' then
begin
Edit1.Text:=Edit1.text+'5'; flag1:=0;
end;
end;

procedure TForm1.SpeedButton7Click(Sender: TObject);
begin
Edit1.Font.Color := clBlack;
if flag1 = 5 then Edit1.Text := '';
if SpeedButton20.Caption = '打开' then
begin
Edit1.Text := Edit1.text+'6'; flag1:=0;
end;
end;

procedure TForm1.SpeedButton13Click(Sender: TObject);
begin
Edit1.Font.Color := clBlack;
if flag1 = 5 then Edit1.Text := '';
if SpeedButton20.Caption = '打开' then
begin
Edit1.Text := Edit1.text+'7'; flag1 := 0;
end;
end;

procedure TForm1.SpeedButton12Click(Sender: TObject);
begin
Edit1.Font.Color := clBlack;
if flag1 = 5 then Edit1.Text := '';
if SpeedButton20.Caption='打开' then
begin
Edit1.Text:=Edit1.text+'8'; flag1:=0;
end;
end;

procedure TForm1.SpeedButton14Click(Sender: TObject);
begin
Edit1.Font.Color := clBlack; //为每个Button恢复颜色 目的是区分数据是结果还是参加数
if flag1=5 then Edit1.Text := '';
if SpeedButton20.Caption='打开' then
begin
Edit1.Text := Edit1.text+'9'; flag1 := 0;
end;
end;

procedure TForm1.SpeedButton15Click(Sender: TObject);
begin
Edit1.Font.Color := clBlack;
if flag1 = 5 then Edit1.Text := '';
if SpeedButton20.Caption = '打开' then
begin
Edit1.Text := Edit1.text+'0'; flag1 := 0;
end;
end;

procedure TForm1.SpeedButton17Click(Sender: TObject);
begin
Edit1.Clear;
flag := 0;
flag1 := 5; // 是否复位的标记
flag3 :=0; //小数点的标记
Temp1 := '';
Temp2 :=0; //注意变量的定义

end;

procedure TForm1.SpeedButton5Click(Sender: TObject);
begin
Edit1.Font.Color := Clgreen; //设置输入颜色
if (edit1.Text<>'+') and (edit1.Text<>'-')and (edit1.Text<>'*') and (edit1.Text<>'/')
and(edit1.text<>'')
then
begin
try //仅仅作为实验,记住使用的格式
case flag of //切记case 不支持字符型
1 : Temp2 := StrTofloat(Temp1)+StrTofloat(Edit1.text) ;
2 : Temp2 := StrTofloat(Temp1)-StrTofloat(Edit1.text) ;
3 : Temp2 := StrTofloat(Temp1)*StrTofloat(Edit1.text) ;
4 : begin

if StrTofloat(edit1.text)<>0 then Temp2 := StrTofloat(Temp1)/StrTofloat(Edit1.text)
else
begin
Edit1.Font.Color:=clLime;
showmessage('错误!除数不能够为0!');
Edit1.Font.Color:=clblack;
end;

end;
else
Temp2:=StrToFloat(Edit1.text);
end;

except
ShowMessage('错误!您输入的数据过大');
Form1.close;
end;
Temp1 := FloatToStr(Temp2);
if edit1.Font.Color=clblack then
begin
Temp1:=FloatToStr(Temp2);
Edit1.Text := Temp1;
flag1 := 5;
flag := 0;
flag3 := 0;
end ;
if edit1.Font.Color=clgreen then
begin
flag := 1;
flag1 := 5;
Edit1.Text := '+';
flag3 := 0;
end;
end;
end;

procedure TForm1.SpeedButton19Click(Sender: TObject);
begin
Edit1.Font.Color := Clgreen;
if (edit1.Text<>'+') and (edit1.Text<>'-')and (edit1.Text<>'*') and (edit1.Text<>'/')and(edit1.text<>'')
then //当前里面的字符为 + -* / 不能够进行运算
begin
try
case flag of //切记case 不支持字符型
1 : Temp2 := StrTofloat(Temp1)+StrTofloat(Edit1.text) ;
2 : Temp2 := StrTofloat(Temp1)-StrTofloat(Edit1.text) ;
3 : Temp2 := StrTofloat(Temp1)*StrTofloat(Edit1.text) ;
4 :
begin

if StrTofloat(edit1.text)<>0 then Temp2 := StrTofloat(Temp1)/StrTofloat(Edit1.text)
else
begin
Edit1.Font.Color:=clLime;
showmessage('错误!除数不能够为0!');
Edit1.Font.Color:=clblack;
end;

end;
else
Temp2:=StrToFloat(Edit1.text);
end;
except
ShowMessage('错误!您输入的数据过大');
Form1.close;
end;
Temp1 := FloatToStr(Temp2);
if edit1.Font.Color=clblack then
begin
Temp1:=FloatToStr(Temp2);
Edit1.Text := Temp1;
flag1 := 5;
flag := 0;
flag3 := 0;
end ;
if edit1.Font.Color=clgreen then
begin
flag := 2;
flag1 := 5;
Edit1.Text := '-';
flag3 := 0;
end;
end;
end;

procedure TForm1.SpeedButton11Click(Sender: TObject);
begin
Edit1.Font.Color:=Clgreen;
if (edit1.Text<>'+') and (edit1.Text<>'-')and (edit1.Text<>'*') and (edit1.Text<>'/')and(edit1.text<>'') then
begin
try
case flag of //切记case 不支持字符型
1 : Temp2 :=StrTofloat(Temp1)+StrTofloat(Edit1.text) ;
2 : Temp2 := StrTofloat(Temp1)-StrTofloat(Edit1.text) ;
3 : Temp2 := StrTofloat(Temp1)*StrTofloat(Edit1.text) ;
4 : begin

if StrTofloat(edit1.text)<>0 then Temp2:=StrTofloat(Temp1)/StrTofloat(Edit1.text)
else
begin
edit1.Font.Color:=clLime;
showmessage('错误!除数不能够为0!');
edit1.Font.Color:=clblack;
end;
end;
else
Temp2 := StrToFloat(Edit1.text);
end;
except
ShowMessage('错误!您输入的数据过大');
Form1.close;
end;
Temp1 := FloatToStr(Temp2);
if edit1.Font.Color=clblack then
begin
Temp1:=FloatToStr(Temp2);
Edit1.Text := Temp1;
flag1 := 5;
flag := 0;
flag3 := 0;
end ;
if edit1.Font.Color=clgreen then
begin
flag := 3;
flag1 := 5;
Edit1.Text := '*';
flag3 := 0;
end;
end;
end;

procedure TForm1.SpeedButton18Click(Sender: TObject);
begin
Edit1.Font.Color := Clgreen;
if (edit1.Text<>'+') and (edit1.Text<>'-')and (edit1.Text<>'*') and (edit1.Text<>'/')
and (edit1.text<>'') then
begin
try
case flag of //切记case 不支持字符型
1 : Temp2 := StrTofloat(Temp1)+StrTofloat(Edit1.text) ;
2 : Temp2 := StrTofloat(Temp1)-StrTofloat(Edit1.text) ;
3 : Temp2 := StrTofloat(Temp1)*StrTofloat(Edit1.text) ;
4 : begin

if StrTofloat(edit1.text)<>0 then Temp2 := StrTofloat(Temp1)/StrTofloat(Edit1.text)
else
begin
edit1.Font.Color:=clLime;
showmessage('错误!除数不能够为0!');
edit1.Font.Color:=clblack;
end;

end;
else
Temp2 := StrToFloat(Edit1.text);
end;
except
ShowMessage('错误!您输入的数据过大');
Form1.close;
end;
Temp1 := FloatToStr(Temp2);
if edit1.Font.Color=clblack then
begin
Temp1:=FloatToStr(Temp2);
Edit1.Text := Temp1;
flag1 := 5;
flag := 0;
flag3 := 0;
end ;
if edit1.Font.Color=clgreen then
begin
flag := 4;
flag1 := 5;
Edit1.Text := '/';
flag3 := 0;
end;
end;
end;

procedure TForm1.SpeedButton21Click(Sender: TObject);
begin

if messagedlg('是否退出?',mtconfirmation,[Mbyes,Mbno],0) = mryes then
close;

end;

procedure TForm1.SpeedButton16Click(Sender: TObject);

begin
if flag1<>5 then
begin
try
case flag of //切记case 不支持字符型
1 : Temp2 := StrTofloat(Temp1)+StrTofloat(Edit1.text) ;
2 : Temp2 := StrTofloat(Temp1)-StrTofloat(Edit1.text) ;
3 : Temp2 := StrTofloat(Temp1)*StrTofloat(Edit1.text) ;
4 : begin

if StrTofloat(edit1.text)<>0 then Temp2:=StrTofloat(Temp1)/StrTofloat(Edit1.text)
else
begin
Edit1.Font.Color:=ClLime;
showmessage('错误!除数不能够为0!');

end;
end;
else

Temp2:=StrToFloat(Edit1.Text); //如果直接按=号,那么显示第一个数
//即把第一个数赋给第二个数
end;
except
ShowMessage('错误!您输入的数据过大');
Form1.close;
end;
flag:=0;
if Edit1.Font.Color=ClLime then Edit1.Font.Color := clBlack
else Edit1.Font.Color := clred ;

Edit1.Text:=FloatToStr(Temp2);
end;
flag1 := 5;
flag3 := 0;
end;

procedure TForm1.N1Click(Sender: TObject);
begin
SpeedButton20.Caption := '打开';
N3.Caption := '状态/on'

end;

procedure TForm1.SpeedButton22Click(Sender: TObject);
begin
Edit1.Font.Color := ClBlack;
if flag1 = 5 then //出现结果指点点小数点 会置0,避免这个情况先清空
// 然后设置 flag1:=0; 这样数字可以跟着后面输入
begin
Edit1.Text := '';
flag1 := 0;
end;

if (SpeedButton20.Caption='打开') and ( flag3=0) then
begin
if Edit1.Text = '' then
Edit1.Text := '0'+'.'+Edit1.text //如果直接点小数点的BUTTON 则在前面加0
else
Edit1.Text := Edit1.text+'.';

end;
Inc(flag3,1); //统计小数、点的个数
// 注意可以用inc(flag3,1)每次 加1 inc(flag3,1)每次加5
//Dec (flag,1)是减减1

end;

procedure TForm1.N2Click(Sender: TObject);
begin
SpeedButton20.Caption := '关闭' ;
N3.Caption := '状态/off';
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
N3.Caption := '状态/on';
flag1:=5;
end;

procedure TForm1.N4Click(Sender: TObject);
begin
close;
end;

procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if key = VK_RETURN then SpeedButton16.Click; // VK_RETURN为ENTER的虚拟值
//还有其他方法可以实现
end;

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
case key of
'1' : SpeedButton1.Click;
'2' : SpeedButton2.Click;
'3' : SpeedButton3.Click;
'4' : SpeedButton4.Click;
'5' : SpeedButton6.Click;
'6' : SpeedButton7.Click;
'7' : SpeedButton13.Click;
'8' : SpeedButton12.Click;
'9' : SpeedButton14.Click;
'0' : SpeedButton15.Click;
'.' : SpeedButton22.Click;
'+' : SpeedButton5.Click;
'-' : SpeedButton19.Click;
'*' : SpeedButton11.Click;
'/' : SpeedButton18.Click;
end;

end;

end.

参考资料: http://bbs.delphi7.com.cn/viewthread.php?tid=344&extra=page%3D1

AiPPT
2024-09-19 广告
随着AI技术的飞速发展,如今市面上涌现了许多实用易操作的AI生成工具1、简介:AiPPT: 这款AI工具智能理解用户输入的主题,提供“AI智能生成”和“导入本地大纲”的选项,生成的PPT内容丰富多样,可自由编辑和添加元素,图表类型包括柱状图... 点击进入详情页
本回答由AiPPT提供
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式