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