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

Postfix
AB+C*DE/-
Prefix
-*+ABC/DE

Stack table

Scan left to right. An operand goes straight to the output. An operator first pops every operator of higher or equal precedence (^ is right to left, so an equal ^ stays), then is pushed. ( is pushed; ) pops up to its (. At the end the stack is emptied.

Symbol scannedStackPostfix expressionAction
((Push '('
A(AOperand: add it to the output
+(+APush '+'
B(+ABOperand: add it to the output
)(empty)AB+Pop + to the output, then remove the '('
**AB+Push '*'
C*AB+COperand: add it to the output
--AB+C*Pop * (higher or equal precedence), then push '-'
D-AB+C*DOperand: add it to the output
/-/AB+C*DPush '/'
E-/AB+C*DEOperand: add it to the output
end(empty)AB+C*DE/-End: pop /, - to the output

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 ›