#include <iostream> #include<algorithm> #include<vector> #include<map> #include<string> #include<cstring> #include<queue> #include<stack> using namespace std; map<char, int> priority{ {'\0',0}, {'+',1},{'-',1}, {'*',2},{'/',2} }; int main() { char arr[1000]; while (cin >> arr) { stack<char> op; stack<double> sdo; string num = " "; for (int i = 0;; i++) { if (arr[i] >= '0' && arr[i] <= '9') { num.push_back(arr[i]); } else { double number = stod(num); sdo.push(number); num = " "; //输入的为符号;输入优先级高,则直接放入 while (!op.empty() && priority[arr[i]] <= priority[op.top()]) { double lhs = sdo.top(); sdo.pop(); double rhs = sdo.top(); sdo.pop(); double res; if (op.top() == '+') { res = rhs + lhs; } if (op.top() == '-') { res = rhs - lhs; } if (op.top() == '*') { res = rhs * lhs; } if (op.top() == '/') { res = rhs / lhs; } op.pop(); sdo.push(res); } if (arr[i] == '\0') { printf("%d\n", (int)sdo.top()); break; } else { op.push(arr[i]); } } } } }