Prashnikaप्रश्निका

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.

What to do

Operators: + - * / % and ^ (power). Brackets ( ) [ ] { } work. Operands can be letters, names or numbers.

Try: (A + B) * C - D / EA + B * C ^ D ^ Eprefix of (P + Q) * (R - S)evaluate 5 6 2 + * 12 4 / -evaluate AB+C*

Answer

Prefix
*+PQ-RS
Postfix
PQ+RS-*

Working

  1. Reverse the expression, swapping ( and ): (S-R)*(Q+P)
  2. Convert that with a stack, as for postfix, but keep an operator of equal precedence on the stack (only ^ pops an equal ^):
Symbol scannedStackOutputAction
((Push '('
S(SOperand: add it to the output
-(-SPush '-'
R(-SROperand: add it to the output
)(empty)SR-Pop - to the output, then remove the '('
**SR-Push '*'
(*(SR-Push '('
Q*(SR-QOperand: add it to the output
+*(+SR-QPush '+'
P*(+SR-QPOperand: add it to the output
)*SR-QP+Pop + to the output, then remove the '('
end(empty)SR-QP+*End: pop * to the output
  1. Reverse the output SR-QP+* to get the prefix form: *+PQ-RS

Practise on real ISC questions

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.

All study tools ›