Dear Friends
Here is the algorithm to change a Postfix Expression to Infix Expression -
You want a program to convert something like
( 3 ( 4 2 + ) 5 * )
to
( ( 3 * ( 4 + 2 ) ) * 5 )
Suppose you have a list of tokens ('(', numbers, operators, ')')
as the programs input. The algorithm is:
1.scan the postfix expression and for each character do the following
if [token is '('] then
push '(' on the stack
else if [token is number] then
push number on the stack
else if [token is operator] then
while
pop off two elements a,b
if top element on stack is '(' then
pop off the '('
push back on "(a operator b)"
break while
else
push back on "(a operator b)"
end while
else if [token is ')'] then
discard
2. After you reach the end of the postfix expressionb read the stack and append to the infix expression
For example click here
4 comments:
Thank for algorithm sir..
in this alg. we must give input along with parenthesis..
any other way without giving parenthesis and checking priorities and insert parenthesis in to the string....
Laxman - this example has a parenthesis does not mean it is always neccessary.
check the algorithm for a postfix expression without parenthesis and it will evaluate correctly.
let me know if you understood what i said.
oh....
i think i got it..
i'll see it after writing program.
sir........
i didn't find the condition in while..
Post a Comment