L
logpie
Unregistered / Unconfirmed
GUEST, unregistred user!
这是我刚才写的:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
type
Stk=Record
Data:array[1..50] of char;
top:0..50;
end;
type
TPTRTable=array[1..7,1..7] of char;
const
GT : TPTRTable= (
(* +, - * / ( ) # *)
(*+*) ( '>', '>', '<', '<', '<', '>', '>'),
(*-*) ( '>', '>', '<', '<', '<', '>', '>'),
(***) ( '>', '>', '>', '>', '<', '>', '>'),
(*/*) ( '>', '>', '>', '>', '<', '>', '>'),
(*(*) ( '<', '<', '<', '<', '<', '=', 'E'),
(*)*) ( '>', '>', '>', '>', 'E', '>', '>'),
(*#*) ( '<', '<', '<', '<', '<', 'E', '=')
);
var
Form1: TForm1;
OPTR,OPND:Stk;
StrExp,Thetas:String;
procedure Push(var Stack:stk;elem:Char);
function Pop(Var Stack:Stk):char;
function GetTop(Var Stack:Stk):Char;
function Confirm(ptr,ptr2:Char):Char;
function Operate(res,theta,res2:Char)Char;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
var i:integer;
theta,res,res2:Char;
res3char;
begin
Push(OPTR,'#');
Thetas:='+-*/()#';
StrExp:=Edit1.Text ;
i:=1;
while not((StrExp='#') and (GetTop(OPTR)='#'))do
begin
if StrExp in ['0'..'9'] then
begin
Push(OPND,StrExp);
i:=i+1;
end
else
case Confirm(GetTop(OPTR),StrExp) of
'<':begin
Push(OPTR,StrExp);
i:=i+1;
end;
'=':begin
Pop(OPTR);
i:=i+1;
end;
'E':Showmessage('Error');
'>':begin
theta:=Pop(OPTR);
res2:=Pop(OPND);
res:=Pop(OPND);
res3:=Operate(res,theta,res2);
Push(OPND,Char(res3));
end;
end;
end;
Showmessage(GetTop(OPND));
end;
procedure Push(Var Stack:stk;elem:Char);
begin
Stack.top:=Stack.top +1;
Stack.Data[Stack.top]:=elem;
end;
function Pop(Var Stack:Stk):char;
begin
Pop:=Stack.Data[Stack.top];
Stack.top:=Stack.top -1;
end;
function GetTop(Var Stack:Stk):Char;
begin
GetTop:=Stack.Data[Stack.top];
end;
function Confirm(Ptr,Ptr2:Char):Char;
var i,j:integer;
begin
i:=1;
j:=1;
while Ptr<>Thetasdo
i:=i+1;
while Ptr2<>Thetas[j]do
j:=j+1;
Confirm:= GT[i,j];
end;
function Operate(res,theta,res2:Char)Char;
var temp:integer;
begin
case theta of
'+':temp:=Strtoint(res) + Strtoint(res2);
'-':temp:=Strtoint(res) - Strtoint(res2);
'*':temp:=Strtoint(res) * Strtoint(res2);
'/':temp:=Strtoint(res) div Strtoint(res2);
end;
Operate:=Pchar(inttostr(temp));
end;
end.
我的EDIT是2-1-758#
显示结果:2,也就是OPND栈顶的数是表达式的首字母,我是《数据结构》上面算法写的啊
到底错在哪了?
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
type
Stk=Record
Data:array[1..50] of char;
top:0..50;
end;
type
TPTRTable=array[1..7,1..7] of char;
const
GT : TPTRTable= (
(* +, - * / ( ) # *)
(*+*) ( '>', '>', '<', '<', '<', '>', '>'),
(*-*) ( '>', '>', '<', '<', '<', '>', '>'),
(***) ( '>', '>', '>', '>', '<', '>', '>'),
(*/*) ( '>', '>', '>', '>', '<', '>', '>'),
(*(*) ( '<', '<', '<', '<', '<', '=', 'E'),
(*)*) ( '>', '>', '>', '>', 'E', '>', '>'),
(*#*) ( '<', '<', '<', '<', '<', 'E', '=')
);
var
Form1: TForm1;
OPTR,OPND:Stk;
StrExp,Thetas:String;
procedure Push(var Stack:stk;elem:Char);
function Pop(Var Stack:Stk):char;
function GetTop(Var Stack:Stk):Char;
function Confirm(ptr,ptr2:Char):Char;
function Operate(res,theta,res2:Char)Char;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
var i:integer;
theta,res,res2:Char;
res3char;
begin
Push(OPTR,'#');
Thetas:='+-*/()#';
StrExp:=Edit1.Text ;
i:=1;
while not((StrExp='#') and (GetTop(OPTR)='#'))do
begin
if StrExp in ['0'..'9'] then
begin
Push(OPND,StrExp);
i:=i+1;
end
else
case Confirm(GetTop(OPTR),StrExp) of
'<':begin
Push(OPTR,StrExp);
i:=i+1;
end;
'=':begin
Pop(OPTR);
i:=i+1;
end;
'E':Showmessage('Error');
'>':begin
theta:=Pop(OPTR);
res2:=Pop(OPND);
res:=Pop(OPND);
res3:=Operate(res,theta,res2);
Push(OPND,Char(res3));
end;
end;
end;
Showmessage(GetTop(OPND));
end;
procedure Push(Var Stack:stk;elem:Char);
begin
Stack.top:=Stack.top +1;
Stack.Data[Stack.top]:=elem;
end;
function Pop(Var Stack:Stk):char;
begin
Pop:=Stack.Data[Stack.top];
Stack.top:=Stack.top -1;
end;
function GetTop(Var Stack:Stk):Char;
begin
GetTop:=Stack.Data[Stack.top];
end;
function Confirm(Ptr,Ptr2:Char):Char;
var i,j:integer;
begin
i:=1;
j:=1;
while Ptr<>Thetasdo
i:=i+1;
while Ptr2<>Thetas[j]do
j:=j+1;
Confirm:= GT[i,j];
end;
function Operate(res,theta,res2:Char)Char;
var temp:integer;
begin
case theta of
'+':temp:=Strtoint(res) + Strtoint(res2);
'-':temp:=Strtoint(res) - Strtoint(res2);
'*':temp:=Strtoint(res) * Strtoint(res2);
'/':temp:=Strtoint(res) div Strtoint(res2);
end;
Operate:=Pchar(inttostr(temp));
end;
end.
我的EDIT是2-1-758#
显示结果:2,也就是OPND栈顶的数是表达式的首字母,我是《数据结构》上面算法写的啊
到底错在哪了?