我写了一个例子,是采用堆栈将中缀变成后缀的
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
QStack=record
Datas:array[0..50] of string;
Top:integer;
end;
PQStack=^QStack;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
procedure InitiateQStack(qstype
QStack);
Function PushQStack(qstype
QStack;s:string):Boolean;
Function PopQStack(qstype
QStack):string;
Function GetTopStack(qstype
QStack):string;
Function NotEmptyStack(qstype
QStack):Boolean;
Function Postfix(qstype
QStack;str:string):Boolean;
Function Priority(x1,x2:string):string;
end;
var
Form1: TForm1;
strArray:array[1..100] of string;
implementation
{$R *.dfm}
{ TForm1 }
function TForm1.GetTopStack(qstype: PQStack): string;
begin
if qstype.Top<0 then result:=''
else
begin
result:=qstype.datas[qstype.top];
end;
end;
procedure TForm1.InitiateQStack(qstype: PQStack);
begin
qstype.Top:=-1;
end;
function TForm1.NotEmptyStack(qstype: PQStack): Boolean;
begin
if qstype.Top<0 then result:=false
else result:=true;
end;
function TForm1.PopQStack(qstype: PQStack): string;
begin
if qstype.Top<0 then result:=''
else
begin
result:=qstype.datas[qstype.top];
qstype.Top:=qstype.Top-1;
end;
end;
function TForm1.Postfix(qstype: PQStack; str: string): Boolean;
var
x1,x2:string;
j,i:integer;
save:string;
begin
j:=1;
i:=1;
new(qstype);
qstype.Datas[0]:='#';
qstype.Top:=0;
x2:=str[j];
x1:=GetTopStack(qstype);
while ((qstype.Top>-1) and (j<(length(str)+1))) do
begin
if ((x2<>'+') and (x2<>'-') and (x2<>'*') and (x2<>'/') and (x2<>'(') and (x2<>')') and (x2<>'#')) then
begin
save:=save+x2;
inc(j);
x2:=str[j];
// showmessage('<> '+'x1: '+x1+' x2: '+x2+' i: '+inttostr(i)+' '+' strarray
: '+strarray+' Save: '+save);
end
else if Priority(x1,x2)='<' then
begin
strArray:=save;
save:='';
inc(i);
PushQStack(qstype,x2);
x1:=GetTopStack(qstype);
inc(j);
x2:=str[j];
// showmessage('< '+'x1: '+x1+' x2: '+x2+' i: '+inttostr(i)+' '+' strarray: '+strarray+' Save: '+save);
end
else if Priority(x1,x2)='>' then
begin
strArray:=save;
save:='';
inc(i);
strArray:=GetTopStack(qstype);
inc(i);
PopQStack(qstype);
x1:=GetTopStack(qstype);
// showmessage('> '+'x1: '+x1+' x2: '+x2+' i: '+inttostr(i)+' '+' strarray: '+strarray+' Save: '+save);
end
else if ((Priority(x1,x2)='=') and (x1='(') and (x2=')')) then
begin
PopQstack(qstype);
x1:=GetTopStack(qstype);
inc(j);
x2:=str[j];
end
else if ((Priority(x1,x2)='=') and (x1='#') and (x2='#')) then
begin
result:=true;
exit;
end
else if Priority(x1,x2)='' then
begin
result:=false;
break;
end;
end;
dispose(qstype);
end;
function TForm1.Priority(x1, x2: string): string;
begin
if (((x1='(') and (x2=')')) or ((x1='#') and (x2='#'))) then result:='='
else if (((x1=')') and (x2='(')) or ((x1='#') and (x2=')'))) then result:=''
else if (((x1='+') or (x1='-') or (x1='*') or (x1='/') or (x1=')')) and ((x2='+') or (x2='-') or (x2=')') or (x2='#'))) then result:='>'
else if ((x1='*') or (x1='/') or (x1=')')) and ((x2='*') or (x2='/')) then result:='>'
else result:='<';
end;
function TForm1.PushQStack(qstype: PQStack; s: string): Boolean;
begin
if (qstype.Top>49) then result:=false
else
begin
inc(qstype.Top);
qstype.Datas[qstype.Top]:=s;
result:=true;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
s:string;
sl:string;
pQStack;
i:integer;
begin
for i:=1 to 100 do
begin
strarray:='';
end;
s:=edit1.Text;
s:=s+'#';
Postfix(p,s);
for i:=1 to 100 do
begin
sl:=sl+strarray;
end;
form1.Caption:=sl;
end;
end.