后缀表达式
后缀表达式,个位数的,用Pascal语言编写
- 提问者网友:疯孩纸
- 2021-04-29 10:39
- 五星知识达人网友:玩家
- 2021-04-29 12:12
var
s:string;
stack:array[1..1001] of integer;
top:integer;
function pop:integer;
begin
pop:=stack[top];
dec(top);
end;
procedure push(x:integer);
begin
inc(top);
stack[top]:=x;
end;
procedure init;
begin
readln(s);
top:=0;
end;
procedure main;
var
top:integer;
i:integer;
x,y,u,v:integer;
begin
for i:=1 to length(s) do
begin
if s[i] in ['0'..'9'] then push(ord(s[i])-ord('0'));
if s[i]='+' then
begin
push(pop+pop);
end;
if s[i]='-' then
begin
x:=pop;
y:=pop;
push(y-x);
end;
if s[i]='*' then
begin
push(pop*pop);
end;
if s[i]='/' then
begin
u:=pop;
v:=pop;
if u<>0 then
push(trunc(v/u))
else begin write('error'); halt; end;
end;
end;
end;
procedure print;
begin
writeln(pop);
end;
begin
init;
main;
print;
end.