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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | #define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #include<stack> #include<vector> #include<typeinfo> using namespace std; int t; stack<double> si; stack<char> sc; inline bool execute() { if (sc.empty()) return false; double x2 = si.top(); si.pop(); double x1 = si.top(); si.pop(); char symbol = sc.top(); sc.pop(); switch (symbol) { case '+':si.push(x1 + x2); break; case '-':si.push(x1 - x2); break; case '*':si.push(x1 * x2); break; case '/':si.push(x1 / x2); break; } return true; } int main() { scanf("%d", &t); do { char c = getc(stdin); if (c == -1)break; if (c == '\n') { while (execute()); if (si.empty())continue; double v = si.top(); while (!si.empty())si.pop(); while (!sc.empty())sc.pop(); printf("%.2lf\n", v); continue; } if (c <= '9' && c >= '0') { ungetc(c, stdin); double v; scanf("%lf", &v); if (sc.empty())si.push(v); else if (sc.top() == '-') { si.push(-v); sc.pop(); sc.push('+'); } else if (sc.top() == '/') { si.push(1.0 / v); sc.pop(); sc.push('*'); } else si.push(v); } else if (c == '+' || c == '-' || c == '*' || c == '/') { while (!sc.empty() && (c == '+' || c == '-') && (sc.top() == '*' || sc.top() == '/')) { execute(); } sc.push(c); } } while (1); } |
http://bjutacm.openjudge.cn/lianxi/1018/