#include using namespace std; // 判断字符是否是操作符 bool isOp(string c) { return c == "+" || c == "-" || c == "*" || c == "/"; } string prefixToPostfix(string s) { stack stk; for (int i = s.size() - 1; i >= 0; i--) { string c = string(1, s[i]); if (isOp(c)) { // 操作符 string a = stk.top(); stk.pop(); string b = stk.top(); stk.pop(); stk.push(a + b + c); } else stk.push(c); // 字母a~z } return stk.top(); } int main() { // * + 12 23 * 3 + 45 6 string prefix = "*+ab*c+de"; string postfix = prefixToPostfix(prefix); cout << "前缀表达式: " << prefix << endl; cout << "后缀表达式: " << postfix << endl; return 0; } /* Prefix Expression: *+ab*c+de Postfix Expression: ab+cde+** Infix Expression: ((a+b)*(c*(d+e))) */