经典。
#include<bits/stdc++.h>
using namespace std;
stack<char> ops;
stack<int> nums;
bool is_num(char c)
{
if(c >= '0' && c <= '9') return true;
return false;
}
void cal()
{
int b = nums.top(); nums.pop();
int a = nums.top(); nums.pop();
char c = ops.top(); ops.pop();
int res;
if(c == '+') res = a + b;
else if(c == '-') res = a - b;
else if(c == '*') res = a * b;
else if(c == '/') res = a / b;
nums.push(res);
}
int main()
{
string s;
cin >> s; s = '(' + s + ')';
for(int i = 0; i < s.size(); i++)
{
char c = s[i];
if(is_num(c))
{
int j = i, num = 0;
while(is_num(s[j])) num = num * 10 + (s[j] - '0'), j++;
i = j - 1; nums.push(num);
}
else
{
if(c == '(') ops.push(c);
else if(c == '+' || c == '-')
{
while(ops.top() != '(') cal();
ops.push(c);
}
else if(c == '*' || c == '/')
{
while(ops.top() == '*' || ops.top() == '/') cal();
ops.push(c);
}
else if(c == ')')
{
while(ops.top() != '(') cal();
ops.pop();
}
else cout << "NO" << endl;
}
}
cout << nums.top() << endl;
return 0;
}