(a) Consider the following arithmetic expression P, written in postfix notation:
P: 12, 7, 3, - , /, 2, 1, 5 , + , * , +
Translate P into infix expression.
P: 12,[7-3],/,2,1,5,+,*,+
=[12/(7-3)],2,1,5,+,*,+
=[12/(7-3)],2,[1+5],*,+
=[12/(7-3)],[2*(1+5)],+
=12/(7-3)+2*(1+5)
(b) Evaluate the infix expression.
P: 12 , 7 , 3 , - , / , 2 , 1 , 5 , + , * , + , )
Symbol |
Stack |
12 |
12 |
7 |
12,7 |
3 |
12,7,3 |
- |
12,4 |
/ |
3 |
2 |
3,2 |
1 |
3,2,1 |
5 |
3,2,1,5 |
+ |
3,2,6 |
* |
3,12 |
+ |
15 |
) |
15 |