求值后缀表达式 3 5*6 7+11/ -
求值后缀表达式 3 5*6 7+11/ -
program hzbds_1010;
type stack=record
data:array[1..100] of real;
top:0..100;
end;
var s:stack;
ch:char;
i:integer;
x:real;
a:array[1..10] of char;
function pop(var s:stack):real;{enter}
begin
pop:=s.data[s.top];
s.top:=s.top-1;
end;
procedure push(var s:stack;x:real);{out}
begin
s.top:=s.top+1;
s.data[s.top]:=x;
end;
{main}
begin
i:=0;
repeat
i:=i+1;
read(a[i]);
until a[i]='@';
s.top:=0;
i:=1;
ch:=a[i];
while ch'@'do
begin
case ch of
'0'..'9':begin
x:=0;
while ch' 'do
begin
x:=x*10+ord(ch)-ord('0');
i:=i+1;
ch:=a[i];
end;
end;
'+':x:=pop(s)+pop(s);
'-':begin x:=pop(s);x:=pop(s)-x; end;
'*':x:=pop(s)*pop(s);
'/':begin x:=pop(s);x:=pop(s)/x; end;
end;
push(s,x);
i:=i+1;ch:=a[i];
end;
writeln(trunc(pop(s)));
end.
用pascal 运行一下注意结尾用@来结束输入