求值后缀表达式 3 5*6 7+11/ -

目中人 1年前 已收到1个回答 举报

331023177 幼苗

共回答了17个问题采纳率:82.4% 举报

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 运行一下注意结尾用@来结束输入

1年前

4
可能相似的问题
Copyright © 2024 YULUCN.COM - 雨露学习互助 - 17 q. 0.235 s. - webmaster@yulucn.com