U117014 后缀表达式求值简单版 题解
weizehao20131117 · · 题解
一、核心考点:栈。
# include <bits/stdc++.h>
using namespace std;
stack<int>st;
char c;
long long a,b;
int num(int a,int b,char c)
{
//四种计算方法
if(c=='+')return a+b;
if(c=='-')return a-b;
if(c=='*')return a*b;
if(c=='/')return a/b;
}
int main()
{
while(cin>>c)
{
//将字符转换成数字
if(c>='0'&&c<='9')
{
c-='0';
st.push(c);
}
else
{
b=st.top();
st.pop();
a=st.top();
st.pop();
st.push(num(a,b,c));
cout<<st.top()<<endl;
}
}
cout<<st.top();
return 0;
}