lgd0722 发表于 2016-5-22 12:05:59

delphi简易计算器

初学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.

三千繁华 发表于 2016-5-22 13:42:04

请问画图小工具怎么做、求代码和图

lgd0722 发表于 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;

梦长生 发表于 2021-6-17 20:47:50

没有压缩包吗
页: [1]
查看完整版本: delphi简易计算器