2526: 【普及-】【P1449】后缀表达式

内存限制:128 MB 时间限制:1.000 S
评测方式:文本比较 命题人:
提交:26 解决:12

题目描述

所谓后缀表达式是指这样的一个表达式:式中不再引用括号,运算符号放在两个运算对象之后,所有计算按运算符号出现的顺序,严格地由左而右新进行(不用考虑运算符的优先级)。

如:lns="http://www.w3.org/1998/Math/MathML">3*(5-2)+7 对应的后缀表达式为:lns="http://www.w3.org/1998/Math/MathML">3.5.2.-*7.+@。在该式中,@ 为表达式的结束符号。. 为操作数的结束符号。

输入

输入一行一个字符串 lns="http://www.w3.org/1998/Math/MathML">,表示后缀表达式。

输出

输出一个整数,表示表达式的值。

样例输入 复制

3.5.2.-*7.+@

样例输出 复制

16

提示

数据保证,lns="http://www.w3.org/1998/Math/MathML">150,答案和计算过程中的每一个值的绝对值不超过 lns="http://www.w3.org/1998/Math/MathML">109


#include<bits/stdc++.h>
using namespace std;

int main(){
	stack<int> n;
	int x,y,s=0,ans=0;
    char ch;
    while (cin>>ch) {
    	if (ch>='0'&&ch<='9') {
    		s=s*10+ch-'0';
    		continue;
		}
    	if (ch=='.') {
    		n.push(s),s=0;
		}else if (ch!='@') {
			x=n.top();
			n.pop();
			y=n.top();
			n.pop();
			switch (ch) {
				case '+':n.push(y+x);break;
				case '-':n.push(y-x);break;
				case '*':n.push(y*x);break;
				case '/':n.push(y/x);break;
			}
		}
	}
	ans=n.top();
    cout<<ans<<endl;
	return 0;
}