Infix to postfix converter
Convert an infix expression to postfix (Reverse Polish) or prefix notation the way ISC Computer Science marks it, with the stack after every symbol - or evaluate a postfix expression step by step.
Answer
- Prefix
- *+PQ-RS
- Postfix
- PQ+RS-*
Working
- Reverse the expression, swapping ( and ): (S-R)*(Q+P)
- Convert that with a stack, as for postfix, but keep an operator of equal precedence on the stack (only ^ pops an equal ^):
| Symbol scanned | Stack | Output | Action |
|---|---|---|---|
| ( | ( | Push '(' | |
| S | ( | S | Operand: add it to the output |
| - | (- | S | Push '-' |
| R | (- | SR | Operand: add it to the output |
| ) | (empty) | SR- | Pop - to the output, then remove the '(' |
| * | * | SR- | Push '*' |
| ( | *( | SR- | Push '(' |
| Q | *( | SR-Q | Operand: add it to the output |
| + | *(+ | SR-Q | Push '+' |
| P | *(+ | SR-QP | Operand: add it to the output |
| ) | * | SR-QP+ | Pop + to the output, then remove the '(' |
| end | (empty) | SR-QP+* | End: pop * to the output |
- Reverse the output SR-QP+* to get the prefix form: *+PQ-RS
Practise on real ISC questions
- Convert the following infix notation to postfix form. ( A / B + C ) / ( D * ( E − F ) 2024 Specimen
- Convert the following infix notation to postfix notation: $A * (B + C / D) - E / F$ 2022 Specimen
- Convert the following infix expression to postfix form: $P * Q / R + (S + T)$ 2017
- Convert the following infix notation to postfix form: $A + ( B - C * ( D / E ) * F )$ 2018
- Convert the following infix notation to prefix form. $(P + Q / R) * (S + T / U) / V$ 2026 Improvement
- Convert the following infix notation to prefix notation. $(A - B) / C * (D + E)$ 2023
- Convert the following infix notation to postfix form. $(P + Q * R - S) / T * U$ 2024
- For the given Binary Tree, which traversal order will arrange the elements in ascending order? 2025 Practice
- Convert the following infix notation to prefix form: $(X + Y) / (Z * W / V)$ 2020
- Convert the following infix notation to postfix form. (AB^C) + (DE) where B^C = $B^C$ 2025 Improvement
More Computer Science tools
- Boolean algebra solver Simplify any expression: the laws step by step, truth table, K-map and the logic circuit.
- Binary tree and BST tool Build a binary search tree, rebuild a tree from two traversals, and get every traversal and fact.