var a:array[1..100]of char;
top:integer;
s:set of char;
ch:char; b:boolean;
begin
s:=[‘(‘,‘[‘];
b:=true;
while not eoln do
begin
read(ch);
if ch in s then
begin //进栈开始
inc(top);
a[top]:=ch;
end //进栈结束
else if top=0 then b:=false //判断
else
case a[top] of
‘(‘:if ch=‘)‘ then dec(top) else b:=false;
‘[‘:if ch=‘]‘ then dec(top) else b:=false;
end;
end;
if b then writeln(‘OK‘) else writeln(‘Wrong‘); //结果输出
end.
【例题】表达式求值
若Exp=a×b+(c-d/e)×f 则
前缀式为:+×ab ×-c /def
中缀式为:3×1+(4-6/2)×9=12
后缀式为:31×462/-9×+=12
因为表达式是递归定义的,则它的转换也是递归的。
后缀式的运算规则为:
运算符在式中出现的顺序恰为表达式的运算顺序;
每个运算符和在它之前出现且紧靠它的两个操作数构成一个最小表达式;
给出后缀表示法能否计算出结果?
给出中缀表示法能否计算出结果?
【重点】学会后缀表达式、中缀表达式、前缀表达式(这个不太用得到)相互的转换方法。
参考程序:
//【本参考程序由gzh提供】
Var
s,ans:string;
Function switch(s:string):string;
var
i,j,len:longint;
s1,s2:string;
top:longint;
flog:boolean;
begin
len:=length(s);
if len=1 then exit(s);
top:=0; flog:=false;
for i:=1 to len do
begin
if s[i]=‘(‘ then inc(top);
if s[i]=‘)‘ then dec(top);
if(top=0)and(i<len)then flog:=true;
end;
if not flog then
begin
delete(s,1,1); delete(s,len-1,1);
exit(switch(s));
end;
for i:=1 to len do
begin
if s[i]=‘(‘ then inc(top);
if s[i]=‘)‘ then dec(top);
if(top=0)and(s[i]=‘+‘)then
begin
s1:=‘‘; s2:=‘‘;
for j:=1 to i-1 do s1:=s1+s[j];
for j:=i+1 to len do s2:=s2+s[j];
s1:=switch(s1); s2:=switch(s2);
exit(s1+s2+‘+‘);
end;
if(top=0)and(s[i]=‘-‘)then
begin
s1:=‘‘; s2:=‘‘;
for j:=1 to i-1 do s1:=s1+s[j];
for j:=i+1 to len do s2:=s2+s[j];
s1:=switch(s1); s2:=switch(s2);
exit(s1+s2+‘-‘);
end;
end;
for i:=1 to len do
begin
if s[i]=‘(‘ then inc(top);
if s[i]=‘)‘ then dec(top);
if(top=0)and(s[i]=‘*‘)then
begin
s1:=‘‘; s2:=‘‘;
for j:=1 to i-1 do s1:=s1+s[j];
for j:=i+1 to len do s2:=s2+s[j];
s1:=switch(s1); s2:=switch(s2);
exit(s1+s2+‘*‘);
end;
if(top=0)and(s[i]=‘/‘)then
begin
s1:=‘‘; s2:=‘‘;
for j:=1 to i-1 do s1:=s1+s[j];
for j:=i+1 to len do s2:=s2+s[j];
s1:=switch(s1); s2:=switch(s2);
exit(s1+s2+‘/‘);
end;
end;
end;
Begin
readln(s);
ans:=switch(s);
writeln(ans);
End.