Labels

Friday, May 11, 2012

Transforming Postfix Expression To Infix

Postfix to Infix Algorithm
Let’s look out for the algorithm for converting postfix to infix expression:-
-  While there are input symbol left
-  Read the next symbol from input.
-  If the symbol is an operand (i.e. value)
-  Push it onto the stack.
-  Otherwise, the symbol is an operator.
-  If there are fewer than 2 values on the stack
-  (Error) The user has not input sufficient values in the expression.
-  Else, Pop the top 2 values from the stack (operand1 & operand 2).
-  Put the operator, with the values as arguments and form a string (like : operand1 operator operand2).
-  Encapsulate the resulted string with parenthesis. (like: (a+b)  if operand1 =’a’, operand2 =’b’, operator = ‘+’ )
-  Push the resulted string back to stack.
-  If there is only one value in the stack
-  That value in the stack is the desired infix string.
-  If there are more values in the stack
-  (Error) The user input has too many values.