← Back to STL Containers
Question 523
Infix to Postfix
Infix to Postfix
Write a functionstring 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 operands0-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*2becomes3 4 2 * +-(1+2)*3becomes1 2 + 3 *-8/2-1becomes8 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 — print one after the last line.
Hints4 available
Sign in to use hintsTopics:
STL ContainersStacks
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (cout, fori)
Your Output
Run your code to see the output here...
Test Cases
Run your code to see test case results.