class Solution {
public:
typedef long long ll;
int calculate(string s) {
s += "+";//在末尾加一个+号运算符
if (s.empty()) return 0;
stack<int> vals;
stack<char> ops;
int val = 0;
for (auto c : s) {
if (c == ' ') {//如果为空格直接跳过
continue;
} else if (c >= '0' && c <= '9') {//如果是数字计算数字大小
val *= 10;
val += c - '0';
} else {//如果是运算符我们先把数字压入栈
vals.push(val);
val = 0;//如果运算符栈顶是乘除的我们要先计算乘除的
if (!ops.empty() && (ops.top() == '*' || ops.top() == '/')) {
cout<<ops.top()<<endl;
int v1 = vals.top();vals.pop();
int v2 = vals.top();vals.pop();
char op = ops.top();ops.pop();
int v = (op == '*') ? (v2 * v1) : (v2 / v1);
vals.push(v);
}
ops.push(c);
}
}
vals.push(val);
int res = 0;
while (!ops.empty()) {
int sign = ops.top() == '-' ? -1 : 1;
ops.pop();
int v = vals.top();
vals.pop();
res += sign * v;
}
if (!vals.empty()) res += vals.top();
return res;
}
};