鱼C论坛

 找回密码
 立即注册
查看: 3156|回复: 3

[原创] delphi简易计算器

[复制链接]
发表于 2016-5-22 12:05:59 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
初学delphi,编写的简易计算器程序。
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, jpeg, ExtCtrls, ComCtrls;

type
  TForm1 = class(TForm)
    btn1: TButton;
    btn2: TButton;
    btn3: TButton;
    btn4: TButton;
    btn5: TButton;
    btn6: TButton;
    btn7: TButton;
    btn8: TButton;
    btn9: TButton;
    btn10: TButton;
    btn11: TButton;
    btn12: TButton;
    btn13: TButton;
    btn14: TButton;
    btn15: TButton;
    btn16: TButton;
    btn17: TButton;
    btn18: TButton;
    btn19: TButton;
    btn20: TButton;
    btn21: TButton;
    btn22: TButton;
    btn23: TButton;
    btn24: TButton;
    redt1: TRichEdit;
    procedure btn21Click(Sender: TObject);
    procedure btn22Click(Sender: TObject);
    procedure btn23Click(Sender: TObject);
    procedure btn9Click(Sender: TObject);
    procedure btn10Click(Sender: TObject);
    procedure btn11Click(Sender: TObject);
    procedure btn12Click(Sender: TObject);
    procedure btn13Click(Sender: TObject);
    procedure btn14Click(Sender: TObject);
    procedure btn15Click(Sender: TObject);
    procedure btn16Click(Sender: TObject);
    procedure btn17Click(Sender: TObject);
    procedure btn18Click(Sender: TObject);
    procedure btn19Click(Sender: TObject);
    procedure btn20Click(Sender: TObject);
    procedure btn1Click(Sender: TObject);
    procedure btn24Click(Sender: TObject);
    procedure btn2Click(Sender: TObject);
    procedure btn3Click(Sender: TObject);
    procedure btn4Click(Sender: TObject);
    procedure btn5Click(Sender: TObject);
    procedure btn6Click(Sender: TObject);
    procedure btn8Click(Sender: TObject);
    procedure btn7Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  num1: Real = 0;
  flag: Integer = 0;
  int1: Integer = 0;

implementation

{$R *.dfm}

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

procedure TForm1.btn22Click(Sender: TObject);
begin
  redt1.Text:= '';
end;

procedure TForm1.btn23Click(Sender: TObject);
begin
  redt1.Text:= FloatToStr( StrToFloat(redt1.Text) / 100 );
end;

procedure TForm1.btn9Click(Sender: TObject);
begin
  redt1.Text:= redt1.Text + '9';
end;

procedure TForm1.btn10Click(Sender: TObject);
begin
  redt1.Text:= redt1.Text + '8';
end;

procedure TForm1.btn11Click(Sender: TObject);
begin
  redt1.Text:= redt1.Text + '7';
end;

procedure TForm1.btn12Click(Sender: TObject);
begin
  redt1.Text:= redt1.Text + '6';
end;

procedure TForm1.btn13Click(Sender: TObject);
begin
  redt1.Text:= redt1.Text + '5';
end;

procedure TForm1.btn14Click(Sender: TObject);
begin
  redt1.Text:= redt1.Text + '4';
end;

procedure TForm1.btn15Click(Sender: TObject);
begin
  redt1.Text:= redt1.Text + '3';
end;

procedure TForm1.btn16Click(Sender: TObject);
begin
  redt1.Text:= redt1.Text + '2';
end;

procedure TForm1.btn17Click(Sender: TObject);
begin
  redt1.Text:= redt1.Text + '1';
end;

procedure TForm1.btn18Click(Sender: TObject);
begin
  redt1.Text:= redt1.Text + '0';
end;

procedure TForm1.btn19Click(Sender: TObject);
begin
  redt1.Text:= redt1.Text + '00';
end;

procedure TForm1.btn20Click(Sender: TObject);
begin
  redt1.Text:= redt1.Text + '.';
end;

procedure TForm1.btn1Click(Sender: TObject);
begin
  num1:= StrToFloat(redt1.Text);
  flag:= 1;
  redt1.Text:= '';
end;

procedure TForm1.btn24Click(Sender: TObject);
begin
  if(flag = 1) then
  begin
    redt1.Text:= FloatToStr(num1 + StrToFloat(redt1.Text));
    flag:= 0;
  end;
  if (flag = 2) then
  begin
    redt1.Text:= FloatToStr(num1 - StrToFloat(redt1.Text));
    flag:= 0;
  end;
  if (flag = 3) then
  begin
    redt1.Text:= FloatToStr(num1 * StrToFloat(redt1.Text));
    flag:= 0;
  end;
  if (flag = 4) then
  begin
    redt1.Text:= FloatToStr(num1 / StrToFloat(redt1.Text));
    flag:= 0;
  end;
  if (flag = 5) then
  begin
    redt1.Text:= IntToStr(int1 and StrToInt(redt1.Text));
    flag:= 0;
  end;
  if (flag = 6) then
  begin
    redt1.Text:= IntToStr(int1 or StrToInt(redt1.Text));
    flag:= 0;
  end;
  if (flag = 7) then
  begin
    redt1.Text:= IntToStr(int1 xor StrToInt(redt1.Text));
    flag:= 0;
  end;
  num1:= 0;
  int1:= 0;
end;

procedure TForm1.btn2Click(Sender: TObject);
begin
  num1:= StrToFloat(redt1.Text);
  flag:= 2;
  redt1.Text:= '';
end;

procedure TForm1.btn3Click(Sender: TObject);
begin
  num1:= StrToFloat(redt1.Text);
  flag:= 3;
  redt1.Text:= '';
end;

procedure TForm1.btn4Click(Sender: TObject);
begin
  num1:= StrToFloat(redt1.Text);
  flag:= 4;
  redt1.Text:= '';
end;

procedure TForm1.btn5Click(Sender: TObject);
begin
  int1:= StrToInt(redt1.Text);
  flag:= 5;
  redt1.Text:= '';
end;

procedure TForm1.btn6Click(Sender: TObject);
begin
  int1:= StrToInt(redt1.Text);
  flag:= 6;
  redt1.Text:= '';
end;

procedure TForm1.btn8Click(Sender: TObject);
begin
  int1:= StrToInt(redt1.Text);
  flag:= 7;
  redt1.Text:= '';
end;

procedure TForm1.btn7Click(Sender: TObject);
begin
  redt1.Text:= IntToStr(not StrToInt(redt1.Text))
end;

end.

简易计算器

简易计算器
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2016-5-22 13:42:04 | 显示全部楼层
请问画图小工具怎么做、求代码和图
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-5-22 15:24:30 | 显示全部楼层
中间if可以改为case。
procedure TForm1.btn24Click(Sender: TObject);
begin
  case flag of
    1: redt1.Text:= FloatToStr(num1 + StrToFloat(redt1.Text));
    2: redt1.Text:= FloatToStr(num1 - StrToFloat(redt1.Text));
    3: redt1.Text:= FloatToStr(num1 * StrToFloat(redt1.Text));
    4: redt1.Text:= FloatToStr(num1 / StrToFloat(redt1.Text));
    5: redt1.Text:= IntToStr(int1 and StrToInt(redt1.Text));
    6: redt1.Text:= IntToStr(int1 or StrToInt(redt1.Text));
    7: redt1.Text:= IntToStr(int1 xor StrToInt(redt1.Text));
  else
  end;
  flag:= 0;
  num1:= 0;
  int1:= 0;
end;
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-6-17 20:47:50 | 显示全部楼层
没有压缩包吗
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-12-31 02:49

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表