题目描述
所谓后缀表达式是指这样的一个表达式:式中不再引用括号,运算符号放在两个运算对象之后,所有计算按运算符号出现的顺序,严格地由左而右新进行(不用考虑运算符的优先级)。
如:3*(5–2)+7对应的后缀表达式为:3.5.2.-*7.+@。’@’为表达式的结束符号。‘.’为操作数的结束符号。
输入输出格式
输入格式:
输入:后缀表达式
输出格式:
输出:表达式的值
输入输出样例
输入样例#1:
3.5.2.-*7.+@
输出样例#1:
16
说明
字符串长度,1000内。
很简单
‘.’为读入
‘+’将前面a[ans]+a[ans-1]
‘-‘a[ans-1]-a[ans]
‘*’a[ans]*a[ans-1]
‘/’a[ans-1] div a[ans]
代码如下:
var x,ans:longint;c:char;a:array[1..600]of longint;
begin
x:=0;
repeat
read(c);
case c of
'0'..'9':
x:=x*10+ord(c)-48;
'+':
begin
dec(ans);
a[ans]:=a[ans]+a[ans+1];
end;
'-':
begin
dec(ans);
a[ans]:=a[ans]-a[ans+1];
end;
'*':
begin
dec(ans);
a[ans]:=a[ans]*a[ans+1];
end;
'/':
begin
dec(ans);
a[ans]:=a[ans] div a[ans+1];
end;
'.':
begin
inc(ans);
a[ans]:=x;
x:=0;
end;
end;
until c='@';
writeln(a[1]);
end.