关于计算器的问题(没有答案,告别大富翁了,郁闷)(50分)

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

吖诚

Unregistered / Unconfirmed
GUEST, unregistred user!
type
PSimpleNode=^TSimpleNode;
TSimpleNode=record
Val:double;
Op:char;
Next:PSimpleNode;
end;
Top= PSimpleNode;
TForm1 = class(TForm)

GroupBox1: TGroupBox;
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;
Edit1: TEdit;
procedure Button10Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
procedure Button7Click(Sender: TObject);
procedure Button8Click(Sender: TObject);
procedure Button9Click(Sender: TObject);
procedure Button11Click(Sender: TObject);
procedure Button17Click(Sender: TObject);
procedure Button16Click(Sender: TObject);
procedure Button15Click(Sender: TObject);
procedure Button14Click(Sender: TObject);
procedure Button13Click(Sender: TObject);
procedure Button12Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
Top,Cur,Next:PSimpleNode;
end;

var
Form1: TForm1;
A:Extended;

implementation

{$R *.dfm}

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

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

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

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

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

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

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

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

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

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

procedure TForm1.Button11Click(Sender: TObject);
begin
if strpos(Pchar(edit1.Text),'.')=nil then
Edit1.Text:=edit1.Text+'.';
end;

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

procedure TForm1.Button16Click(Sender: TObject);
begin
Next:=New(PsimpleNode);
Cur:=Top;
Cur^.Val:=StrTofloat(Edit1.Text);
Cur^.Op:='+';
Edit1.Text:='';
Cur^.Next:=Next;
Cur:=Next;
end;

procedure TForm1.Button15Click(Sender: TObject);
begin
Next:=New(PsimpleNode);
Cur:=Top;
Cur^.Val:=StrTofloat(Edit1.Text);
Cur^.Op:='-';
Edit1.Text:='';
New(Next);
Cur^.Next:=Next;
Cur:=Next;

end;

procedure TForm1.Button14Click(Sender: TObject);
begin
Next:=New(PsimpleNode);
Cur:=Top;
Cur^.Val:=StrTofloat(Edit1.Text);
Cur^.Op:='*';
Edit1.Text:='';
New(Next);
Cur^.Next:=Next;
Cur:=Next;

end;

procedure TForm1.Button13Click(Sender: TObject);
begin
Next:=New(PsimpleNode);
Cur:=Top;
Cur^.Val:=StrTofloat(Edit1.Text);
Cur^.Op:='/';
Edit1.Text:='';
New(Next);
Cur^.Next:=Next;
Cur:=Next;
end;

procedure TForm1.Button12Click(Sender: TObject);
begin
While Cur^.Op='+' do // Next<>nil
Edit1.Text:=floatTostr(StrTofloat(Edit1.Text)+Cur^.Val);


end;

end.
请问如何才可以实现连算(+。-。*。/)按先后次序。该程序错在那里???谢谢~!
 
判断里面的有无(*,-,)字符,看比如你把表达式可以重新格式化一下,把所有的*,/都加上(),最后根据()的层数进行一层一层的计算分解,就可以了。剩下的只剩下层层的剥离()的算法了。
 
晕死我了,都学了20多天delphi了,连个这样的计算器都写不好。这程序有错误,好像Top也没建立好。都想几天了,现在看到都头大
 
学得时间不长嘛,还好,转学JAVA吧
 
你错了,我学了2年的DELPHI做不包含连加都做不好很正常,因为连加本来算法就复杂,和你的DELPH水平无关。
我说的是,你为什么确定字符的话,使用组件的tag和hint属性好些,其实也没有什么了。
你需要把算法的内容存成字符进行算法处理就完了。
 
unit Unit1;

interface

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

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 Button8Click(Sender: TObject);
procedure Button7Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
procedure Button12Click(Sender: TObject);
procedure Button11Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button10Click(Sender: TObject);
procedure Button9Click(Sender: TObject);
procedure Button16Click(Sender: TObject);
procedure Button15Click(Sender: TObject);
procedure Button14Click(Sender: TObject);
procedure Button13Click(Sender: TObject);
procedure Button17Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
s: string;
implementation

{$R *.dfm}

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

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

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

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

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

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

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

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

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

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

procedure TForm1.Button11Click(Sender: TObject);
begin
if strpos(Pchar(edit1.Text),'.')=nil then
Edit1.Text:=edit1.Text+'.';
end;

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

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

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

end;

procedure TForm1.Button14Click(Sender: TObject);
begin
s:=s+Edit1.Text+'*';
Edit1.Text:='';

end;

procedure TForm1.Button13Click(Sender: TObject);
begin
s:=s+Edit1.Text+'/';
Edit1.Text:='';
end;

procedure TForm1.Button12Click(Sender: TObject);
var
sc : OleVariant;
begin
s:=s+edit1.Text;
sc := CreateOleObject('MSScriptControl.ScriptControl.1');
sc.Language :='JavaScript';
edit1.Text:= VarToStr(sc.Eval(s));
end;

end.
delphi7测试通过
 
unit Unit10;

interface

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

type
PSimpleNode=^TSimpleNode;
TSimpleNode=record
Val:double;
Op:char;
Next:PSimpleNode;
end;
//Top= PSimpleNode;
TForm1 = class(TForm)

GroupBox1: TGroupBox;
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;
Edit1: TEdit;
procedure Button10Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
procedure Button7Click(Sender: TObject);
procedure Button8Click(Sender: TObject);
procedure Button9Click(Sender: TObject);
procedure Button11Click(Sender: TObject);
procedure Button17Click(Sender: TObject);
procedure Button16Click(Sender: TObject);
procedure Button12Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button15Click(Sender: TObject);
private
{ Private declarations }
Top,Cur,Next:PSimpleNode;
public
{ Public declarations }
end;

var
Form1: TForm1;
//A:Extended;

implementation

{$R *.dfm}

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

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

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

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

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

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

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

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

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

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

procedure TForm1.Button11Click(Sender: TObject);
begin
if strpos(Pchar(edit1.Text),'.')=nil then
Edit1.Text:=edit1.Text+'.';
end;

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

procedure TForm1.Button16Click(Sender: TObject);
begin
Cur^.Val:= StrTofloat(Edit1.Text);
Cur^.op:='+';
Edit1.Text:='';
Next:=new(PSimpleNode);
Cur^.Next :=Next;
Cur:=Next;
Next:=nil

end;
procedure TForm1.Button12Click(Sender: TObject);
var
answer:Double;
begin
answer:=0;
Cur^.Val :=strtofloat(edit1.Text );
Cur^.Op :='=';
Cur^.Next :=nil;
{execute operation }
Cur := Top ;
if Top^.Op ='+' then
answer :=answer+Top^.Val;
//else if Top^.Op ='-' then
//answer :=Top^.Val;

While Cur^.Next<>nil do
begin
Next:=Cur^.Next;
if (Next^.op='+') or (Next^.op='=') then
answer :=answer+Next^.Val //StrTofloat
else if (Next^.op='-') or (Next^.op='=') then
answer :=answer-Next^.Val
//StrTofloat
Cur:=Next;
end;
edit1.Text :=floattostr(answer);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
Top:=new(PsimpleNode);
Cur:=Top;
end;

procedure TForm1.Button15Click(Sender: TObject);
begin
Cur^.Val:= StrTofloat(Edit1.Text);
Cur^.op:='-';
Edit1.Text:='';
Next:=new(PSimpleNode);
Cur^.Next :=Next;
Cur:=Next;
Next:=nil
end;

end.
按此方法可以实现连加,但还是不可混合运算,更别说什么先后次序了,做郁闷了都
 
我工作五年了

写程序主要用的就是加减运算

连乘除我都很少用

为楼主的精神感到汗颜
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
2K
import
I
S
回复
0
查看
928
SUNSTONE的Delphi笔记
S
后退
顶部