#include<cstdio> #include<string> #include<map> #include<stack> using namespace std; int main() { char arr[300]; map<char, int> priority = {//记录输入的优先级 {'$', 0}, {'+', 1}, {'-', 1}, {'*', 2}, {'/', 2}, }; while (fgets(arr, sizeof(arr), stdin) != NULL) { string ear = arr; ear.pop_back(); if (ear == "0") { break; } ear.push_back('$');//给表达式末尾加上$ stack<double> numStack; stack<char> opearStack; string nums; for (int i = 0; i < ear.size(); ++i) {//开始遍历表达式 if (ear[i] >= '0' && ear[i] <= '9') { nums.push_back(ear[i]);//把数字压入数字栈 } else if (ear[i] == ' ') { if (nums != " ") { numStack.push(stod(nums)); nums = " "; } } else { if (ear[i] == '$') { if (nums != " ") { numStack.push(stod(nums)); nums = " "; } } while (!opearStack.empty() && priority[ear[i]] <= priority[opearStack.top()]) { char oper = opearStack.top();//操作符 opearStack.pop(); double rhs = numStack.top();//运算数1 numStack.pop(); double lhs = numStack.top();//运算数2 numStack.pop(); switch (oper) { case '+' : numStack.push(lhs + rhs); break; case '-' : numStack.push(lhs - rhs); break; case '*' : numStack.push(lhs * rhs); break; case '/' : numStack.push(lhs / rhs); break; } } opearStack.push(ear[i]); } } printf("%.2lf\n", numStack.top()); } }