初学者请教大虾个问题(50分)

  • 主题发起人 主题发起人 吖诚
  • 开始时间 开始时间

吖诚

Unregistered / Unconfirmed
GUEST, unregistred user!
这是个计算器的小程序,现在有两个问题:第一、这个程序要怎样认定计算的先后顺序?
第二、当输入一次小数点后怎么实现不会再次接受输入在edit1上?
本人刚开始学delphi,论坛注册时间不长,分数虽少,还请大家见谅。如果哪位大虾知道,还请赐教,感激不尽~!

var
Form1: TForm1;
A:Extended;
B:boolean;
C:boolean;
D:boolean;
E:boolean;
implementation

{$R *.dfm}


procedure TForm1.Button4Click(Sender: TObject);
begin
Edit1.text:=Edit1.Text+'0';
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
Edit1.text:=Edit1.Text+'1';
end;

procedure TForm1.Button7Click(Sender: TObject);
begin
Edit1.text:=Edit1.Text+'2';
end;

procedure TForm1.Button10Click(Sender: TObject);
begin
Edit1.text:=Edit1.Text+'3';
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
Edit1.text:=Edit1.Text+'4';
end;

procedure TForm1.Button9Click(Sender: TObject);
begin
Edit1.text:=Edit1.Text+'6';
end;

procedure TForm1.Button6Click(Sender: TObject);
begin
Edit1.text:=Edit1.Text+'5';
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
Edit1.text:=Edit1.Text+'7';
end;

procedure TForm1.Button5Click(Sender: TObject);
begin
Edit1.text:=Edit1.Text+'8';
end;

procedure TForm1.Button8Click(Sender: TObject);
begin
Edit1.text:=Edit1.Text+'9';
end;

procedure TForm1.Button15Click(Sender: TObject);
begin
Edit1.text:=Edit1.Text+'.';
end;

procedure TForm1.Button13Click(Sender: TObject);
begin
A:=StrTofloat(Edit1.text);
Edit1.Text :='';
B:=true;
c:=false;
D:=false;
E:=false;
end;

procedure TForm1.Button14Click(Sender: TObject);
begin
Edit1.Text:='';
end;

procedure TForm1.Button16Click(Sender: TObject);

begin
if B=true then
Edit1.Text:=floatTostr(StrTofloat(Edit1.Text ) + A)
else if C=true then
Edit1.Text:=floatTostr(A - StrTofloat(Edit1.Text ))
else if D=true then
Edit1.Text:=floatTostr(StrTofloat(Edit1.Text ) * A)
else if E=true then
Edit1.Text:=floatTostr(A / StrTofloat(Edit1.Text ));




end;

procedure TForm1.Button17Click(Sender: TObject);
begin
A:=StrTofloat(Edit1.text);
Edit1.Text :='';
B:=false;
c:=true;
D:=false;
E:=false;

end;

procedure TForm1.Button12Click(Sender: TObject);
begin
A:=StrTofloat(Edit1.text);
Edit1.Text :='';
B:=false;
c:=false;
D:=true;
E:=false;
end;

procedure TForm1.Button11Click(Sender: TObject);
begin
A:=StrToInt(Edit1.text);
Edit1.Text :='';
B:=false;
c:=false;
D:=false;
E:=true;
end;

end.
 
第一:调用如下程序计算 先uses ComObj;
procedure TForm1.BitBtn1Click(Sender: TObject);
var
sc : OleVariant;
begin
sc := CreateOleObject('MSScriptControl.ScriptControl.1');
sc.Language :='JavaScript';
ShowMessage(VarToStr(sc.Eval(edit1.Text)));
end;

第二 要控制edit1的输入值,需做四则运算语法分析, 比较麻烦
 
这是个普通的计算器程序,如果那样的思维就会写的很复杂,我觉得用前序式可以让计算机认定+.-.*./的先后次序的,但是我对那语法不太熟悉,嘿嘿,请见谅,这样子程序是可以运算的,就是不分计算顺序。和小数点,可以不断重复接受输入。有没有最简单的方法来解决这两个问题,这样我这样的初学者也容易懂些。
 
procedure TForm1.Button15Click(Sender: TObject);
begin
try
floattostr(edit1.text+'.');
Edit1.text:=Edit1.Text+'.';
except
end;
end;
 
try
StrTofloat(edit1.text+'.');
Edit1.text:=Edit1.Text+'.';
except
end;
这样式可行的,但是我这弹出个CPU的错误,是不是我定义了什么东西不对或者是我有什么东西没定义?
 
编译时出现错误,运行exe就不出现了,
 
谢谢2005帮我解决了小数点的问题,那对于让计算器分出计算的顺序谁 还有比较简单的方法可以实现吗?太难了我也不懂,写了白写,汗~!
 
刚哪位兄弟呼叫了我,不好意思没有找到你的名字回你,有空加msn;聊天。嘿嘿
 
哪位大侠知道怎么用节点方式实现该程序的连式运算阿(可以自动识别+、*、/- )给我些提示吧。。。。。。。。另外小数点我觉得这样更容易些 :
if strpos(PChar(edit1.Text),'.') = nil then
edit1.Text :=edit1.Text +'.'
 
这也是一个计算器的代码,共同学习.
unit Unit1;

interface

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

type
TForm1 = class(TForm)
Panel1: TPanel;
Panel2: TPanel;
SpeedButton7: TSpeedButton;
SpeedButton8: TSpeedButton;
SpeedButton9: TSpeedButton;
SpeedButton13: TSpeedButton;
SpeedButton4: TSpeedButton;
SpeedButton5: TSpeedButton;
SpeedButton6: TSpeedButton;
SpeedButton12: TSpeedButton;
SpeedButton1: TSpeedButton;
SpeedButton2: TSpeedButton;
SpeedButton3: TSpeedButton;
SpeedButton11: TSpeedButton;
SpeedButton14: TSpeedButton;
SpeedButton15: TSpeedButton;
SpeedButton10: TSpeedButton;
StaticText1: TStaticText;
SpeedButton0: TSpeedButton;
SpeedButton16: TSpeedButton;
SpeedButton17: TSpeedButton;
GroupBox1: TGroupBox;
procedure SpeedButton1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure SpeedButton11Click(Sender: TObject);
procedure SpeedButton15Click(Sender: TObject);
procedure SpeedButton16Click(Sender: TObject);
procedure SpeedButton17Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
restart: Boolean;
isfirst: Boolean;
fir_num,sec_num: String;
sign: integer;
result: real;
save: String;

implementation

{$R *.dfm}

function count(sign: integer):real;
begin
case sign of
//-----------------------------------------------------------
1: result:=strtofloat(fir_num)+strtofloat(sec_num)
//为加号时
2: result:=strtofloat(fir_num)-strtofloat(sec_num)
//为减号时
3: result:=strtofloat(fir_num)*strtofloat(sec_num)
//为乘号时
4: begin
try
result:=strtofloat(fir_num)/strtofloat(sec_num)
//为除号时
except
ShowMessage('错误!');
form1.close;
end
//除数为0时,做出异常处理
end;
end;
end;
//---------------------------------------------

procedure TForm1.SpeedButton1Click(Sender: TObject);
var
i: integer;
begin
if restart then //如果是重新开始输入,则清除原来的操作数,并设置isfirst为True
begin
isfirst:=True;
fir_num:='';
sec_num:='';
restart:=False;
end;
if isfirst then //如果是第一个操作数
begin
if (sender as TSpeedButton).Caption='.' then //如果输入的是小数点
begin
if (strlen(pChar(fir_num))<=0) then //如果第一个操作数并未输入
fir_num:='0.'
else
for i:= 1 to strlen(pChar(fir_num)) do
if fir_num='.' then exit;
//如果第一个中已含有小数点而又输入小数点,则退出
end;
if (strlen(pChar(fir_num))>0) and (fir_num[1]='0') then //如果最高位为0
begin
if ((sender as TSpeedButton).Caption='.') then
fir_num:='0.'
else
begin
if strlen(pChar(fir_num))>1 then //如果是小数,则继续输入
fir_num:=fir_num+(sender as TSpeedButton).Caption
else
fir_num:=(sender as TSpeedButton).Caption;
//如果不是小数,则去掉最高位的0
end;
end
else
fir_num:=fir_num+(sender as TSpeedButton).Caption;
StaticText1.Caption:=fir_num;
end
else
begin
if (sender as TSpeedButton).Caption='.' then //如果第二个操作数并未输入
begin
if (strlen(pChar(sec_num))<=0) then
sec_num:='0.'
else
for i:= 1 to strlen(pChar(sec_num)) do
if sec_num='.' then exit;
//如果第二个中已含有小数点而又输入小数点,则退出
end;
if (strlen(pChar(sec_num))>0) and (sec_num[1]='0') then //如果最高位为0
begin
if ((sender as TSpeedButton).Caption='.') then
sec_num:='0.'
else
begin
if strlen(pChar(sec_num))>1 then //如果是小数,则继续输入
sec_num:=sec_num+(sender as TSpeedButton).Caption
else
sec_num:=(sender as TSpeedButton).Caption;
//如果不是小数,则去掉最高位的0
end;
end
else
sec_num:=sec_num+(sender as TSpeedButton).Caption;
StaticText1.Caption:=sec_num;
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
StaticText1.Caption:='0.'
//设置StaticText1初始显示为0.
restart:=False;
Isfirst:=True;
fir_num:='';
sec_num:='';
end;

procedure TForm1.SpeedButton11Click(Sender: TObject);
begin
if (fir_num<>'') and (sec_num<>'') then
//如果两各操作数都不为空
begin
result:=count(sign)
//调用函数,返回计算结果
fir_num:=floattostr(result);
sec_num:='';
StaticText1.Caption:=floattostr(result);
end;
sign:=(sender as TSpeedButton).Tag;
isfirst:=False;
restart:=False;
end;

procedure TForm1.SpeedButton15Click(Sender: TObject);
begin
if (sec_num<>'') then
//如果第二个操作数不为空则返回结果
begin
result:=count(sign);
fir_num:='';
fir_num:=fir_num+floattostr(result);
StaticText1.Caption:=floattostr(result);
sec_num:='';
end;
restart:=true;
end;

procedure TForm1.SpeedButton16Click(Sender: TObject);
begin
restart:=True;
fir_num:='';
sec_num:='';
self.StaticText1.Caption:='0.';
end;

procedure TForm1.SpeedButton17Click(Sender: TObject);
begin
Close;
end;

end.
 
我想在计算器上的计算符都建立一个节点,等到用等于号确认的时候能自动按计算连式的顺序先计算乘。除。可行性是可以的。可我就是没找到好的方法,去克服些程序时候的困难
 
可以考虑一下把符号放进一个数组中,点了=之后在按顺序取出来
 
多人接受答案了。
 
后退
顶部