C++
#include <cctype>
#include <iostream>
#include <stack>
#include <unordered_map>
using namespace std;
unordered_map<char, int> isp{
{'(', 1}, {'*', 5}, {'/', 5}, {'+', 3}, {'-', 3}, {')', 6}
};
unordered_map<char, int> icp{
{'(', 6}, {'*', 4}, {'/', 4}, {'+', 2}, {'-', 2}, {')', 1}
};
stack<int> nums;
stack<char> ops;
string toRPN(const string& expr) {
string rpn;
bool parenthesis = false;
int i = 0;
while (i < expr.length()) {
if (isdigit(expr[i])) {
while (i < expr.length() && isdigit(expr[i])) {
rpn += expr[i++];
}
rpn += 'n';
} else {
char c = expr[i++];
while (!ops.empty() && isp[ops.top()] >= icp[c]) {
if (isp[ops.top()] > icp[c]) {
rpn += ops.top();
ops.pop();
} else {
ops.pop();
parenthesis = true;
}
if (parenthesis) {
break;
}
}
if (parenthesis) {
parenthesis = false;
continue;
}
ops.push(c);
}
}
while (!ops.empty()) {
rpn += ops.top();
ops.pop();
}
return rpn;
}
int calcRPN(const string& rpn) {
int i = 0;
while (i < rpn.length()) {
if (isdigit(rpn[i])) {
int num = 0;
while (rpn[i] != 'n') {
num = 10 * num + rpn[i++] - '0';
}
nums.push(num);
++i;
} else {
char c = rpn[i++];
int rhs = nums.top(); nums.pop();
int lhs = nums.top(); nums.pop();
if (c == '+') {
nums.push(lhs + rhs);
} else if (c == '-') {
nums.push(lhs - rhs);
} else if (c == '*') {
nums.push(lhs * rhs);
} else {
nums.push(lhs / rhs);
}
}
}
return nums.top();
}
int main() {
string expr;
cin >> expr;
string rpn = toRPN(expr);
cout << calcRPN(rpn) << endl;
return 0;
}