//土尔逊Torson 编写于2023/5/03 #define _CRT_SECURE_NO_WARNINGS #include <cstdio> #include <stack> #include <string> #include <stdlib.h> #include <iostream> #include <map> using namespace std; int main() { string tmp05701; map<char, int> priority = { { '$',0 }, { '+',1 },{ '-',1 }, { '*',2 },{ '/',2 } }; while (getline(cin, tmp05701)) { stack<char> opr05701; stack<double> num05701; string process = tmp05701; string num; //printf("%s %d\n", tmp05701.c_str(),tmp05701.size()); //process.pop_back(); if (process == "0") { break; } process.push_back('$'); for (unsigned i = 0; i < process.size(); ++i) { if (process[i] >= '0' && process[i] <= '9') { num.push_back(process[i]); } else { // + - * / $ if (num != "") { num05701.push(stod(num));//stod --> string to double num = ""; } if (process[i] == '$') { if (num != "") { num05701.push(stod(num));//stod --> string to double num = ""; } } while (!opr05701.empty() && priority[opr05701.top()] >= priority[process[i]]) { // 新来的运算符的优先级不高于栈顶的优先级 char oper = opr05701.top(); opr05701.pop(); double rhs = num05701.top(); num05701.pop(); double lhs = num05701.top(); num05701.pop(); switch (oper) { case '+': num05701.push(lhs + rhs); break; case '-': num05701.push(lhs - rhs); break; case '*': num05701.push(lhs * rhs); break; case '/': num05701.push(lhs / rhs); break; } } // 所有比expr[i]优先级更高的运算符都计算过了 opr05701.push(process[i]); } } printf("%.0lf\n", num05701.top()); } system("pause"); return EXIT_SUCCESS; } // 64 位输出请用 printf("%lld")