Infix to Postfix
hardQuestion 523Write a function string toPostfix(const string& expr) that converts an infix arithmetic expression to postfix notation using a stack of operators.
The input contains no spaces and only these characters: single-digit operands 0-9, the operators +, -, *, /, and parentheses ( ). The expression is always valid.
Precedence rules:
- * and / have higher precedence (2) than + and - (1)
- all operators are left-associative
- parentheses override precedence
The output tokens (each a single character) are separated by single spaces with no trailing space. Examples:
- 3+4*2 becomes 3 4 2 * +
- (1+2)*3 becomes 1 2 + 3 *
- 8/2-1 becomes 8 2 / 1 -
The tests print the returned string on its own line.
Do NOT write a main function.
Expected output
3 4 2 * + 1 2 + 3 * 8 2 / 1 - 2 3 4 + * 5 -
= newline. The output ends with a newline, so print one after the last line.
Your output
Run your code to see the output here...