class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 给定一个后缀表达式,返回它的结果
     * @param str string字符串 
     * @return long长整型
     */
    long long legalExp(string str) {
        // write code here
        long long d=0;
        int i=0;
        int n=str.length();
        stack<long long> st;
        while(i<n){
            long long x,y;
            if(str[i]<='9'&&str[i]>='0'){
                d=str[i]-(long long)'0'+d*10;
            }
            else if(str[i]=='#'){
                st.push(d);
                d=0;
                i++;
                continue;
            }
            else if(str[i]=='*'){
                y=st.top();
                st.pop();
                x=st.top();
                st.pop();
                st.push(x*y);
            }
            else if(str[i]=='/'){
                y=st.top();
                st.pop();
                x=st.top();
                st.pop();
                st.push(x/y);
            }
            else if(str[i]=='+'){
                y=st.top();
                st.pop();
                x=st.top();
                st.pop();
                st.push(x+y);
            }
            else if(str[i]=='-'){
                y=st.top();
                st.pop();
                x=st.top();
                st.pop();
                st.push(x-y);
            }
            i++;
        }
        return st.top();
    }
};