#include <bits/stdc++.h> #include <cstddef> #include <cstdio> #include <stack> #include <string> using namespace std; int main() { char buf[1001]; while (fgets(buf, 1001, stdin) != NULL) { string expr = buf; expr.pop_back(); map<char, int> pirority{ {'$', 0}, {'+', 1}, {'-', 1}, {'*', 2}, {'/', 2}, }; //去除换行符号 /* 整数和运算符之间用一个空格分隔。没有非法表达式。当一行中只有0时输入结束,相应的结果不要输出。 输入: 1 + 2 4 + 2 * 5 - 7 / 11 0 输出: 3.00 13.36 */ if (expr == "0") break; expr.push_back('$');//补充一个虚拟的终止符号 string num;//数字 stack<double> numstack; stack<char> operstack; for (unsigned i = 0; i < expr.size(); i++) { if (expr[i] >= '0' && expr[i] <= '9') { num.push_back(expr[i]);//压入数字 } else if (expr[i] == ' ') { if (num != " ") { numstack.push(stod(num));//stod():string->double num = " "; } } else { if (expr[i] == '$') { if (num != " ") { numstack.push(stod(num)); num = " "; } } while (!operstack.empty() && pirority[operstack.top()] >= pirority[expr[i]]) { char oper = operstack.top(); operstack.pop(); double right = numstack.top(); numstack.pop(); double left = numstack.top(); numstack.pop(); switch (oper) { case '+': numstack.push(right + left); break; case '-': numstack.push(left - right); break; case '*': numstack.push(right * left); break; case '/': numstack.push(left /right ); break; } } operstack.push(expr[i]); } } printf("%.2lf\n",numstack.top()); } } // 64 位输出请用 printf("%lld")