#include<iostream> #include<string> #include<stack> using namespace std; stack<char> op; //运算符 stack<double> d; //操作数 double getnum(string str, int& t) { //将多个字符转化为数字,t为引用类型 double ans = 0; while (isdigit(str[t])) { ans = ans * 10 + str[t] - '0'; t++; } return ans; } int priority(char c) { if (c == '#') return 0; else if (c == '$') return 1; else if (c == '+' || c == '-') return 2; else return 3; } double calculate(double x, double y, char op) { double res = 0; if (op == '+') res = x + y; else if (op == '-') res = x - y; else if (op == '*') res = x * y; else if (op == '/') res = x / y; return res; } int main() { string str; while (getline(cin, str)) { if (str == "0") break; op.push('#'); //#为最低优先级 str += '$'; //处理结束的标志 int t = 0; while (t < str.size()) { if (str[t] == ' ') t++; //跳过空格 else if (isdigit(str[t])) d.push(getnum(str, t)); //数字入栈,可能有多位 else { if (priority(op.top()) < priority( str[t])) { //优先级比运算符栈顶大,则入栈 op.push(str[t]); t++; } else { double y = d.top(); //取操作数 d.pop(); double x = d.top(); d.pop(); d.push(calculate(x, y, op.top())); //后进先出 op.pop(); //弹出运算符 } } } printf("%.2f\n", d.top()); } return 0; }