求一个DELPHI的简单计算器代码,能运算加减乘除和退格功能就行
展开全部
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Button5: TButton;
Button6: TButton;
Button7: TButton;
Button8: TButton;
Button9: TButton;
Button10: TButton;
Button11: TButton;
Button12: TButton;
Button13: TButton;
Button14: TButton;
Button15: TButton;
Button16: TButton;
Button17: TButton;
BitBtn1: TBitBtn;
Edit1: TEdit;
procedure Button13Click(Sender: TObject);
procedure Button11Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
procedure Button14Click(Sender: TObject);
procedure Button10Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button12Click(Sender: TObject);
procedure Button9Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button7Click(Sender: TObject);
procedure Button8Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button16Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
procedure Button15Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button17Click(Sender: TObject);
private
procedure setedit(n :string);
{ Private declarations }
public
{ Public declarations }
end;
type
Tcalc=Object
private
value1: Double;
value2: Double;
sign: Char;
public
function Add(x,y: Double): Double;
function Sub(x,y: Double): Double;
function Mul(x,y: Double): Double;
function divi(x,y: Double): Double;
end;
var
Form1: TForm1;
calc: Tcalc;
stort: string;
implementation
{$R *.dfm}
{ Tcalc }
function Tcalc.Add(x, y: Double): Double;
begin
Result:=x+y;
end;
function Tcalc.divi(x, y: Double): Double;
begin
if y=0 then
begin
ShowMessage('除数不能为0');
exit;
end
else
Result:=x/y;
end;
function Tcalc.Mul(x, y: Double): Double;
begin
Result:=x*y;
end;
function Tcalc.Sub(x, y: Double): Double;
begin
Result:=x-y;
end;
{ TForm1 }
procedure TForm1.setedit(n: string);
begin
edit1.Text:=stort+n;
stort:=edit1.Text;
calc.value1:=StrToFloat(edit1.Text);
end;
procedure TForm1.Button13Click(Sender: TObject);
begin
setedit('1');
end;
procedure TForm1.Button11Click(Sender: TObject);
begin
setedit('2');
end;
procedure TForm1.Button6Click(Sender: TObject);
begin
setedit('3');
end;
procedure TForm1.Button14Click(Sender: TObject);
begin
setedit('4');
end;
procedure TForm1.Button10Click(Sender: TObject);
begin
setedit('5');
end;
procedure TForm1.Button5Click(Sender: TObject);
begin
setedit('6');
end;
procedure TForm1.Button12Click(Sender: TObject);
begin
setedit('7');
end;
procedure TForm1.Button9Click(Sender: TObject);
begin
setedit('8');
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
setedit('9');
end;
procedure TForm1.Button7Click(Sender: TObject);
begin
setedit('0');
end;
procedure TForm1.Button8Click(Sender: TObject);
begin
setedit('.');
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
Button16Click(nil);
calc.value2:=calc.value1;
calc.sign:='+';
stort:='';
end;
procedure TForm1.Button16Click(Sender: TObject);
begin
if calc.sign<>' ' then
stort:='';
if calc.sign='+' then
setedit(FloatToStr(calc.Add(calc.value2,calc.value1)));
if calc.sign='-' then
setedit(FloatToStr(calc.Sub(calc.value2,calc.value1)));
if calc.sign='*' then
setedit(FloatToStr(calc.Mul(calc.value2,calc.value1)));
if calc.sign='/' then
setedit(FloatToStr(calc.divi(calc.value2,calc.value1)));
calc.sign:=' ';
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
calc.sign:=' ';
end;
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
calc.sign:=' ';
stort:='';
edit1.Text:='';
end;
procedure TForm1.Button15Click(Sender: TObject);
begin
Button16Click(nil);
calc.value2:=calc.value1;
calc.sign:='-';
stort:='';
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Button16Click(nil);
calc.value2:=calc.value1;
calc.sign:='*';
stort:='';
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Button16Click(nil);
calc.value2:=calc.value1;
calc.sign:='/';
stort:='';
end;
procedure TForm1.Button17Click(Sender: TObject);
begin
Edit1.Text:=copy(edit1.Text,1,length(edit1.Text)-1);
stort:='';
setedit(edit1.Text);
end;
end.
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Button5: TButton;
Button6: TButton;
Button7: TButton;
Button8: TButton;
Button9: TButton;
Button10: TButton;
Button11: TButton;
Button12: TButton;
Button13: TButton;
Button14: TButton;
Button15: TButton;
Button16: TButton;
Button17: TButton;
BitBtn1: TBitBtn;
Edit1: TEdit;
procedure Button13Click(Sender: TObject);
procedure Button11Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
procedure Button14Click(Sender: TObject);
procedure Button10Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button12Click(Sender: TObject);
procedure Button9Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button7Click(Sender: TObject);
procedure Button8Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button16Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
procedure Button15Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button17Click(Sender: TObject);
private
procedure setedit(n :string);
{ Private declarations }
public
{ Public declarations }
end;
type
Tcalc=Object
private
value1: Double;
value2: Double;
sign: Char;
public
function Add(x,y: Double): Double;
function Sub(x,y: Double): Double;
function Mul(x,y: Double): Double;
function divi(x,y: Double): Double;
end;
var
Form1: TForm1;
calc: Tcalc;
stort: string;
implementation
{$R *.dfm}
{ Tcalc }
function Tcalc.Add(x, y: Double): Double;
begin
Result:=x+y;
end;
function Tcalc.divi(x, y: Double): Double;
begin
if y=0 then
begin
ShowMessage('除数不能为0');
exit;
end
else
Result:=x/y;
end;
function Tcalc.Mul(x, y: Double): Double;
begin
Result:=x*y;
end;
function Tcalc.Sub(x, y: Double): Double;
begin
Result:=x-y;
end;
{ TForm1 }
procedure TForm1.setedit(n: string);
begin
edit1.Text:=stort+n;
stort:=edit1.Text;
calc.value1:=StrToFloat(edit1.Text);
end;
procedure TForm1.Button13Click(Sender: TObject);
begin
setedit('1');
end;
procedure TForm1.Button11Click(Sender: TObject);
begin
setedit('2');
end;
procedure TForm1.Button6Click(Sender: TObject);
begin
setedit('3');
end;
procedure TForm1.Button14Click(Sender: TObject);
begin
setedit('4');
end;
procedure TForm1.Button10Click(Sender: TObject);
begin
setedit('5');
end;
procedure TForm1.Button5Click(Sender: TObject);
begin
setedit('6');
end;
procedure TForm1.Button12Click(Sender: TObject);
begin
setedit('7');
end;
procedure TForm1.Button9Click(Sender: TObject);
begin
setedit('8');
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
setedit('9');
end;
procedure TForm1.Button7Click(Sender: TObject);
begin
setedit('0');
end;
procedure TForm1.Button8Click(Sender: TObject);
begin
setedit('.');
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
Button16Click(nil);
calc.value2:=calc.value1;
calc.sign:='+';
stort:='';
end;
procedure TForm1.Button16Click(Sender: TObject);
begin
if calc.sign<>' ' then
stort:='';
if calc.sign='+' then
setedit(FloatToStr(calc.Add(calc.value2,calc.value1)));
if calc.sign='-' then
setedit(FloatToStr(calc.Sub(calc.value2,calc.value1)));
if calc.sign='*' then
setedit(FloatToStr(calc.Mul(calc.value2,calc.value1)));
if calc.sign='/' then
setedit(FloatToStr(calc.divi(calc.value2,calc.value1)));
calc.sign:=' ';
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
calc.sign:=' ';
end;
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
calc.sign:=' ';
stort:='';
edit1.Text:='';
end;
procedure TForm1.Button15Click(Sender: TObject);
begin
Button16Click(nil);
calc.value2:=calc.value1;
calc.sign:='-';
stort:='';
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Button16Click(nil);
calc.value2:=calc.value1;
calc.sign:='*';
stort:='';
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Button16Click(nil);
calc.value2:=calc.value1;
calc.sign:='/';
stort:='';
end;
procedure TForm1.Button17Click(Sender: TObject);
begin
Edit1.Text:=copy(edit1.Text,1,length(edit1.Text)-1);
stort:='';
setedit(edit1.Text);
end;
end.
展开全部
试试这个
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Button5: TButton;
Button6: TButton;
Button7: TButton;
Button8: TButton;
Button9: TButton;
Button10: TButton;
Button11: TButton;
Button12: TButton;
Button13: TButton;
Button14: TButton;
Button15: TButton;
Button16: TButton;
Button17: TButton;
procedure Button4Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
procedure Button7Click(Sender: TObject);
procedure Button9Click(Sender: TObject);
procedure Button10Click(Sender: TObject);
procedure Button11Click(Sender: TObject);
procedure Button13Click(Sender: TObject);
procedure Button8Click(Sender: TObject);
procedure Button12Click(Sender: TObject);
procedure Button15Click(Sender: TObject);
procedure Button16Click(Sender: TObject);
procedure Button17Click(Sender: TObject);
procedure Button14Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
procedure SetEditText(info: string);
{ Private declarations }
public
{ Public declarations }
end;
type //自定义一个Tcompute的类
Tcompute = object
private //保护类中成员数据的蚂则安全
temp1: double;
temp2: double;
temp3: double;
sign: Char;
public //把昌滑类中耐物腊的成员函数进行共享
function Add(x: double; y: double): Double;
function Sub(x: double; y: double): Double;
function Mult(x: double; y: double): Double;
function Dived(x: double; y: double): Double;
end;
var
Form1: TForm1;
Info: Tcompute; //声明Info是类Tcompute的对象
oldinfo:string;
implementation
function Tcompute.Add(x: double; y: double): Double; //进行类的定义与实现
begin
Result := x + y;
end;
function Tcompute.Sub(x: double; y: double): Double;
begin
Result := x - y;
end;
function Tcompute.Mult(x: double; y: double): Double;
begin
Result := x * y;
end;
function Tcompute.Dived(x: double; y: double): Double;
begin
if (y = 0) then begin
Form1.Edit1.text:='1111111111';
//ShowMessage('除数不能为0!'); //这里判断除数不能为0
Result := x;
end else
Result := x / y;
end;
{$R *.dfm}
procedure TForm1.Button4Click(Sender: TObject);
begin
oldinfo := '';
SetEditText('');
Info.sign := ' ';
Info.temp1 := 0;
Info.temp2 := 0;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
SetEditText('7');
Info.temp1 := StrTofloat(Edit1.text);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
SetEditText('8');
Info.temp1 := StrTofloat(Edit1.text);
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
SetEditText('9');
Info.temp1 := StrTofloat(Edit1.text);
end;
procedure TForm1.Button5Click(Sender: TObject);
begin
SetEditText('4');
Info.temp1 := StrTofloat(Edit1.text);
end;
procedure TForm1.Button6Click(Sender: TObject);
begin
SetEditText('5');
Info.temp1 := StrTofloat(Edit1.text);
end;
procedure TForm1.Button7Click(Sender: TObject);
begin
SetEditText('6');
Info.temp1 := StrTofloat(Edit1.text);
end;
procedure TForm1.Button9Click(Sender: TObject);
begin
SetEditText('1');
Info.temp1 := StrTofloat(Edit1.text);
end;
procedure TForm1.Button10Click(Sender: TObject);
begin
SetEditText('2');
Info.temp1 := StrTofloat(Edit1.text);
end;
procedure TForm1.Button11Click(Sender: TObject);
begin
SetEditText('3');
Info.temp1 := StrTofloat(Edit1.text);
end;
procedure TForm1.Button13Click(Sender: TObject);
begin
SetEditText('0');
Info.temp1 := StrTofloat(Edit1.text);
end;
procedure TForm1.Button8Click(Sender: TObject);
begin
Button17Click(nil);
Info.temp2 := Info.temp1;
Info.sign := '+';
oldinfo := '';
end;
procedure TForm1.Button12Click(Sender: TObject);
begin
Button17Click(nil);
Info.temp2 := Info.temp1;
Info.sign := '-';
oldinfo := '';
end;
procedure TForm1.Button15Click(Sender: TObject);
begin
Button17Click(nil);
Info.temp2 := Info.temp1;
Info.sign := '*';
oldinfo := '';
end;
procedure TForm1.Button16Click(Sender: TObject);
begin
Button17Click(nil);
Info.temp2 := Info.temp1;
Info.sign := '/';
oldinfo := '';
end;
procedure TForm1.Button17Click(Sender: TObject);
begin
if Info.sign<>' ' then oldinfo := '';
if (Info.sign = '+') then
SetEditText(FloatToStr(Info.Add(Info.temp2, Info.temp1)))
else if (Info.sign = '-') then
SetEditText(FloatToStr(Info.Sub(Info.temp2, Info.temp1)))
else if (Info.sign = '*') then
SetEditText(FloatToStr(Info.Mult(Info.temp2, Info.temp1)))
else if (Info.sign = '/') then
SetEditText(FloatToStr(Info.Dived(Info.temp2, Info.temp1)));
if Edit1.Text<>'' then
Info.temp1 := StrToFloat(Edit1.Text);
Info.sign := ' ';
end;
procedure TForm1.Button14Click(Sender: TObject);
begin
SetEditText('.');
Info.temp1 := StrToFloat(Edit1.Text);
end;
procedure TForm1.SetEditText(info:string);
begin
Edit1.Text := oldinfo + info;
oldinfo := Edit1.Text;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
oldinfo := '';
end;
end.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Button5: TButton;
Button6: TButton;
Button7: TButton;
Button8: TButton;
Button9: TButton;
Button10: TButton;
Button11: TButton;
Button12: TButton;
Button13: TButton;
Button14: TButton;
Button15: TButton;
Button16: TButton;
Button17: TButton;
procedure Button4Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
procedure Button7Click(Sender: TObject);
procedure Button9Click(Sender: TObject);
procedure Button10Click(Sender: TObject);
procedure Button11Click(Sender: TObject);
procedure Button13Click(Sender: TObject);
procedure Button8Click(Sender: TObject);
procedure Button12Click(Sender: TObject);
procedure Button15Click(Sender: TObject);
procedure Button16Click(Sender: TObject);
procedure Button17Click(Sender: TObject);
procedure Button14Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
procedure SetEditText(info: string);
{ Private declarations }
public
{ Public declarations }
end;
type //自定义一个Tcompute的类
Tcompute = object
private //保护类中成员数据的蚂则安全
temp1: double;
temp2: double;
temp3: double;
sign: Char;
public //把昌滑类中耐物腊的成员函数进行共享
function Add(x: double; y: double): Double;
function Sub(x: double; y: double): Double;
function Mult(x: double; y: double): Double;
function Dived(x: double; y: double): Double;
end;
var
Form1: TForm1;
Info: Tcompute; //声明Info是类Tcompute的对象
oldinfo:string;
implementation
function Tcompute.Add(x: double; y: double): Double; //进行类的定义与实现
begin
Result := x + y;
end;
function Tcompute.Sub(x: double; y: double): Double;
begin
Result := x - y;
end;
function Tcompute.Mult(x: double; y: double): Double;
begin
Result := x * y;
end;
function Tcompute.Dived(x: double; y: double): Double;
begin
if (y = 0) then begin
Form1.Edit1.text:='1111111111';
//ShowMessage('除数不能为0!'); //这里判断除数不能为0
Result := x;
end else
Result := x / y;
end;
{$R *.dfm}
procedure TForm1.Button4Click(Sender: TObject);
begin
oldinfo := '';
SetEditText('');
Info.sign := ' ';
Info.temp1 := 0;
Info.temp2 := 0;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
SetEditText('7');
Info.temp1 := StrTofloat(Edit1.text);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
SetEditText('8');
Info.temp1 := StrTofloat(Edit1.text);
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
SetEditText('9');
Info.temp1 := StrTofloat(Edit1.text);
end;
procedure TForm1.Button5Click(Sender: TObject);
begin
SetEditText('4');
Info.temp1 := StrTofloat(Edit1.text);
end;
procedure TForm1.Button6Click(Sender: TObject);
begin
SetEditText('5');
Info.temp1 := StrTofloat(Edit1.text);
end;
procedure TForm1.Button7Click(Sender: TObject);
begin
SetEditText('6');
Info.temp1 := StrTofloat(Edit1.text);
end;
procedure TForm1.Button9Click(Sender: TObject);
begin
SetEditText('1');
Info.temp1 := StrTofloat(Edit1.text);
end;
procedure TForm1.Button10Click(Sender: TObject);
begin
SetEditText('2');
Info.temp1 := StrTofloat(Edit1.text);
end;
procedure TForm1.Button11Click(Sender: TObject);
begin
SetEditText('3');
Info.temp1 := StrTofloat(Edit1.text);
end;
procedure TForm1.Button13Click(Sender: TObject);
begin
SetEditText('0');
Info.temp1 := StrTofloat(Edit1.text);
end;
procedure TForm1.Button8Click(Sender: TObject);
begin
Button17Click(nil);
Info.temp2 := Info.temp1;
Info.sign := '+';
oldinfo := '';
end;
procedure TForm1.Button12Click(Sender: TObject);
begin
Button17Click(nil);
Info.temp2 := Info.temp1;
Info.sign := '-';
oldinfo := '';
end;
procedure TForm1.Button15Click(Sender: TObject);
begin
Button17Click(nil);
Info.temp2 := Info.temp1;
Info.sign := '*';
oldinfo := '';
end;
procedure TForm1.Button16Click(Sender: TObject);
begin
Button17Click(nil);
Info.temp2 := Info.temp1;
Info.sign := '/';
oldinfo := '';
end;
procedure TForm1.Button17Click(Sender: TObject);
begin
if Info.sign<>' ' then oldinfo := '';
if (Info.sign = '+') then
SetEditText(FloatToStr(Info.Add(Info.temp2, Info.temp1)))
else if (Info.sign = '-') then
SetEditText(FloatToStr(Info.Sub(Info.temp2, Info.temp1)))
else if (Info.sign = '*') then
SetEditText(FloatToStr(Info.Mult(Info.temp2, Info.temp1)))
else if (Info.sign = '/') then
SetEditText(FloatToStr(Info.Dived(Info.temp2, Info.temp1)));
if Edit1.Text<>'' then
Info.temp1 := StrToFloat(Edit1.Text);
Info.sign := ' ';
end;
procedure TForm1.Button14Click(Sender: TObject);
begin
SetEditText('.');
Info.temp1 := StrToFloat(Edit1.Text);
end;
procedure TForm1.SetEditText(info:string);
begin
Edit1.Text := oldinfo + info;
oldinfo := Edit1.Text;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
oldinfo := '';
end;
end.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
真的不好意思 不会啊
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询