表达式中数字均为非负整数
stack<int> num;
stack<char> op;
void eval()
{
int b = num.top();
num.pop();
int a = num.top();
num.pop();
char c = op.top();
op.pop();
if (c == '+')
{
num.push(a + b);
}
else if (c == '-')
{
num.push(a - b);
}
else if (c == '*')
{
num.push(a * b);
}
else
{
num.push(a / b);
}
}
int main()
{
std::ios::sync_with_stdio(false), std::cin.tie(0), std::cout.tie(0);
unordered_map<char, int> pr = {{'+', 1}, {'-', 1}, {'*', 2}, {'/', 2}};//运算符优先级
string str;
cin >> str;
for (int i = 0; i < str.length(); i++)
{
if (str[i] >= '0' && str[i] <= '9')
{
int x = 0;
while (str[i] >= '0' && str[i] <= '9')
{
x = 10 * x + str[i++] - '0';
}
num.push(x);
i--;
}
else if (str[i] == ')')
{
while (op.top() != '(')
{
eval();
}
op.pop();
}
else
{
while (op.size() && str[i] != '(' && pr[str[i]] <= pr[op.top()])
{
eval();
}
op.push(str[i]);
}
}
while (op.size())
{
eval();
}
cout << num.top() << endl;
return 0;
}