Infix to Postfix Conversion CS212 & CS240 DJ Foreman
The algorithms convert infix notation to postfix use a stack of operators evaluate a postfix expression uses stack of operands evaluate an infix expression use (1) and (2) together
Infix to Postfix (algorithm 1) while (more input ) if '(' push on stack with priority 0 if operand - send to output if ')' output operators till '('. Unstack & discard the '('. Discard the ')' if operator while precedence (operator) < precedence (top) unstack and output operators unstack and output any operators remaining on stack Precedence (high to low): ^ * / + - (
Evaluating Postfix (algorithm 2) while (more input) if (operand) push on stack if (operator) apply to top 2 (previous) stack items and replace them (on the stack) with the result of the operation When no input remains, remaining item on the operand stack is value of the expression