各位大侠:
小弟有一个请教:
怎么用Deiphl做一个计算器的程序?
我这的课本上有一个简单的计算器的实现,需要你自己去扩展
想要的话就留下邮箱
代码如下:
unit Udemo28;
interface
uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;
type TDemo12Form = class(TForm) Button0: TButton; Button1: TButton; Button2: TButton; Button3: TButton; Button4: TButton; Button5: TButton; Button6: TButton; Button7: TButton; Button8: TButton; Button9: TButton; Button10: TButton; Plus: TButton; Minus: TButton; Multiply: TButton; Devide: TButton; Equal: TButton; Clear: TButton; edtCalc: TEdit; procedure FormCreate(Sender: TObject); procedure ZeroToNineClick(Sender: TObject); procedure OperatorClick(Sender: TObject); procedure EqualClick(Sender: TObject); procedure ClearClick(Sender: TObject); private { Private declarations } Temp: real; OpChar: char; IsOperator: boolean; public { Public declarations } end;
var Demo12Form: TDemo12Form;
implementation
{$R *.DFM}
procedure TDemo12Form.FormCreate(Sender: TObject); begin Temp := 0; IsOperator := false; end;
procedure TDemo12Form.ZeroToNineClick(Sender: TObject); begin if IsOperator then begin IsOperator := false; EdtCalc.Text := ''; end; if (EdtCalc.Text='0') and ((Sender as TButton).Caption<>'.') then EdtCalc.Text := ''; EdtCalc.Text := EdtCalc.Text + (Sender as TButton).Caption; end;
procedure TDemo12Form.OperatorClick(Sender: TObject); begin Temp := StrToFloat(EdtCalc.Text); //为变量Temp赋值 case (Sender as TButton).Tag of //取加减乘除运算符 0: OpChar := '+'; 1: OpChar := '-'; 2: OpChar := '*'; 3: OpChar := '/'; end; IsOperator := true; end;
procedure TDemo12Form.EqualClick(Sender: TObject); begin if OpChar<>'=' then try case OpChar of //作加减乘除运算 '+': Temp := Temp + StrToFloat(EdtCalc.Text); '-': Temp := Temp - StrToFloat(EdtCalc.Text); '*': Temp := Temp * StrToFloat(EdtCalc.Text); '/': Temp := Temp / StrToFloat(EdtCalc.Text); end; OpChar := '='; EdtCalc.Text := FloatToStr(Temp); //显示运算结果 except on Exception do if OpChar='/' then if Temp>=0 then EdtCalc.Text := '错误:正无穷大!' else EdtCalc.Text := '错误:负无穷大!'; end; end;
procedure TDemo12Form.ClearClick(Sender: TObject); begin Temp := 0; EdtCalc.Text := '0'; end;
end.
另外,小弟的邮箱是aeolusfly.student@sina.com
望大侠长指教!