After discussing Infix to Postfix and Infix to Prefix expression,now I discuss Post fix Evaluation.Postfix evaluation can be done in c,c+...
After discussing Infix to Postfix and Infix to Prefix expression,now I discuss Post fix Evaluation.Postfix evaluation can be done in c,c++,java,c# etc.Now we take an example:-
-->2*5/3-7+8%3*2-1(by using stack algorithm)
First we evaluate infix expression(direct method):-
=10/3-7+8%3*2-1
=3-7+8%3*2-1
=3-7+2*2-1
=3-7+4-1
=-4+4-1
=-1
Then Convert into Postfix notation:-
=(25*3/)-7+(83%)*2-1
=(25*3/)-7+(83%2*)-1
=(25*3/7-)+(83%2+)-1
=(25*3/7-83%2*+)-1
=25*3/7-83%2*+1-
Finally Postfix conversion by using stack:-
Postfix | Stack |
---|---|
2 | 2 |
3 | 2,3 |
5 | 2,3,5 |
* | 2,15 |
4 | 2,15,4 |
- | 2,11 |
3 | 2,11,3 |
% | 2,2 |
+ | 4 |
7 | 4,7 |
- | -3 |
1 | -3,1 |
5 | -3,1,5 |
* | -3*5 |
+ | 2 |
COMMENTS