1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| unordered_map<char, int> pri; pri['#'] = 0; pri['$'] = 1; pri['+'] = pri['-'] = 2; pri['*'] = pri['/'] = 3;
stack<char> op; stack<int> num; string str; str += '$'; op.push('#'); int idx = 0;
while (idx < str.size()) { if (str[idx] == ' ') idx++; else if (isdigit(str[idx])) num.push(getnum(str, idx)); else { if (pri[op.top()]) < pri[str[idx]]) { op.push(str[idx]); idx++; } else { double y = num.top(); num.pop(); double x = num.top(); num.pop(); num.push(cal(x, y, op.top())); op.pop(); } } }
|