主题:[原创]计算器
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons;
type
TForm1 = class(TForm)
Edit1: TEdit;
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
BitBtn3: TBitBtn;
BitBtn4: TBitBtn;
BitBtn5: TBitBtn;
BitBtn6: TBitBtn;
BitBtn7: TBitBtn;
BitBtn8: TBitBtn;
BitBtn9: TBitBtn;
BitBtn10: TBitBtn;
BitBtn11: TBitBtn;
BitBtn12: TBitBtn;
BitBtn13: TBitBtn;
BitBtn14: TBitBtn;
BitBtn15: TBitBtn;
BitBtn16: TBitBtn;
BitBtn17: TBitBtn;
procedure FormCreate(Sender: TObject);
procedure BitBtn11Click(Sender: TObject);
procedure BitBtn12Click(Sender: TObject);
procedure BitBtn13Click(Sender: TObject);
procedure zerotonine(Sender: TObject);
procedure Edit1KeyPress(Sender: TObject; var Key: Char);
procedure BitBtn14Click(Sender: TObject);
procedure BitBtn17Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
temp : integer; //累加结果临时保存变量。
first : boolean; //是否是第一次操作我们的运算符
equ : boolean;
op : char;
sub : boolean;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
temp := 0;
first := true;
equ := false;
end;
procedure TForm1.BitBtn11Click(Sender: TObject); //add
begin
op := '+';
if first = true then
begin
temp := strtoint(edit1.Text);
first := false;
edit1.Text :='';
end
else
begin
temp := temp + strtoint(edit1.Text);
edit1.Text := '';
end;
end;
procedure TForm1.BitBtn12Click(Sender: TObject); //clear
begin
edit1.Text := '';
temp := 0;
first := true;
end;
procedure TForm1.BitBtn13Click(Sender: TObject); //equal
begin
case op of
'+' :
begin
temp := temp +strtoint(edit1.Text);
edit1.Text := inttostr(temp);
first := true;
end;
'-' :
begin
temp := temp - strtoint(edit1.Text);
edit1.Text := inttostr(temp);
end;
end;
first := true;
end;
procedure TForm1.zerotonine(Sender: TObject); //num
begin
edit1.Text := edit1.Text+(sender as Tbutton).Caption;
end;
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if (key > '9') or (key < '0') and (key <> #8) then // 判断用户输入的是不是数字
if (key = '-') and (edit1.Text = '') then //判断是否输入了符号
key := '-'
else
key := #0;
end;
procedure TForm1.BitBtn14Click(Sender: TObject); //sub
begin
op := '-';
if first = true then //判断是否第一次执行‘-’运算
begin
temp := strtoint(edit1.Text); //第一次执行时,因为还没输入第二个操作数,所以只需简单赋值就行了
first := false;
edit1.Text :='';
end
else
begin
temp := temp - strtoint(edit1.Text);
edit1.Text := '';
end;
end;
procedure TForm1.BitBtn17Click(Sender: TObject); // 判断是否是符号
begin
if edit1.Text = '' then //符号只有在数字前面才算正确。
edit1.Text := '-';
end;
end.
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons;
type
TForm1 = class(TForm)
Edit1: TEdit;
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
BitBtn3: TBitBtn;
BitBtn4: TBitBtn;
BitBtn5: TBitBtn;
BitBtn6: TBitBtn;
BitBtn7: TBitBtn;
BitBtn8: TBitBtn;
BitBtn9: TBitBtn;
BitBtn10: TBitBtn;
BitBtn11: TBitBtn;
BitBtn12: TBitBtn;
BitBtn13: TBitBtn;
BitBtn14: TBitBtn;
BitBtn15: TBitBtn;
BitBtn16: TBitBtn;
BitBtn17: TBitBtn;
procedure FormCreate(Sender: TObject);
procedure BitBtn11Click(Sender: TObject);
procedure BitBtn12Click(Sender: TObject);
procedure BitBtn13Click(Sender: TObject);
procedure zerotonine(Sender: TObject);
procedure Edit1KeyPress(Sender: TObject; var Key: Char);
procedure BitBtn14Click(Sender: TObject);
procedure BitBtn17Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
temp : integer; //累加结果临时保存变量。
first : boolean; //是否是第一次操作我们的运算符
equ : boolean;
op : char;
sub : boolean;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
temp := 0;
first := true;
equ := false;
end;
procedure TForm1.BitBtn11Click(Sender: TObject); //add
begin
op := '+';
if first = true then
begin
temp := strtoint(edit1.Text);
first := false;
edit1.Text :='';
end
else
begin
temp := temp + strtoint(edit1.Text);
edit1.Text := '';
end;
end;
procedure TForm1.BitBtn12Click(Sender: TObject); //clear
begin
edit1.Text := '';
temp := 0;
first := true;
end;
procedure TForm1.BitBtn13Click(Sender: TObject); //equal
begin
case op of
'+' :
begin
temp := temp +strtoint(edit1.Text);
edit1.Text := inttostr(temp);
first := true;
end;
'-' :
begin
temp := temp - strtoint(edit1.Text);
edit1.Text := inttostr(temp);
end;
end;
first := true;
end;
procedure TForm1.zerotonine(Sender: TObject); //num
begin
edit1.Text := edit1.Text+(sender as Tbutton).Caption;
end;
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if (key > '9') or (key < '0') and (key <> #8) then // 判断用户输入的是不是数字
if (key = '-') and (edit1.Text = '') then //判断是否输入了符号
key := '-'
else
key := #0;
end;
procedure TForm1.BitBtn14Click(Sender: TObject); //sub
begin
op := '-';
if first = true then //判断是否第一次执行‘-’运算
begin
temp := strtoint(edit1.Text); //第一次执行时,因为还没输入第二个操作数,所以只需简单赋值就行了
first := false;
edit1.Text :='';
end
else
begin
temp := temp - strtoint(edit1.Text);
edit1.Text := '';
end;
end;
procedure TForm1.BitBtn17Click(Sender: TObject); // 判断是否是符号
begin
if edit1.Text = '' then //符号只有在数字前面才算正确。
edit1.Text := '-';
end;
end.