C++ 中缀表达式转后缀表达式(单一位数运算数)
#include
int main() { string infix; cin >> infix;
stack<char> op_stack;
string postfix = "";
for (char c : infix) {
if (isdigit(c)) {
postfix += c;
} else {
while (!op_stack.empty() && op_stack.top() != '(' && ((c == '*' || c == '/') || (op_stack.top() == '+' || op_stack.top() == '-'))) {
postfix += op_stack.top();
op_stack.pop();
}
op_stack.push(c);
}
}
while (!op_stack.empty()) {
postfix += op_stack.top();
op_stack.pop();
}
cout << postfix << endl;
return 0;
}
原文地址: https://www.cveoy.top/t/topic/oiLU 著作权归作者所有。请勿转载和采集!