class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值
*
* 给定一个后缀表达式,返回它的结果
* @param str string字符串
* @return long长整型
*/
long long legalExp(string str) {
// write code here
using ll=long long;
stack<ll> st;
ll num=0;
bool readingnum=false;
for(auto ch:str)
{
if(isdigit(ch))
{
num=num*10+ch-'0';
readingnum=true;
}
else if(ch=='#')
{
st.push(num);
num=0;
readingnum=false;
}
else if (ch == '+' || ch == '-' || ch == '*'){
ll num2 = st.top(); st.pop();
ll num1 = st.top(); st.pop();
ll result = 0;
switch(ch)
{
case '+': result = num1 + num2; break;
case '-': result = num1 - num2; break;
case '*': result = num1 * num2; break;
}
st.push(result);
}
}
return st.top();
}
};