http://tjuacm.chaosheng.top/problem.php?id=1253
#include <iostream> #include <cstdio> #include <cstring> #include <unordered_map> #include <stack> using namespace std; string expr; stack<double> num; stack<char> op; bool isNumber(char a){ return (a != '+' && a != '-' && a != '*' && a != '/'); } void eval(){ double x = num.top(); num.pop(); double y = num.top(); num.pop(); char c = op.top(); op.pop(); double ans; if(c == '+') ans = x + y; else if(c == '-') ans = y - x; else if(c == '*') ans = x * y; else if(c == '/') ans = y / (x * 1.0); num.push(ans); } int main(){ unordered_map<char, int> pr{{'+', 1}, {'-', 1}, {'*', 2}, {'/', 2}}; int t; cin >> t; while(t--){ cin >> expr; int n = expr.size(); for(int i = 0; i < n; i++){ if(isdigit(expr[i])){ double tmp = 0, j = i; while(j < n && isdigit(expr[j])){ tmp = tmp * 10 + expr[j++] - '0'; } num.push(tmp); i = j - 1; }else{ while (op.size() && pr[op.top()] >= pr[expr[i]]) eval(); op.push(expr[i]); } } while(!op.empty()){ eval(); } cout << num.top() << endl; } return 0; }