Friday, May 7, 2010

cs301 solution

7 2↑ 4 3 2↑*+18 3/-
7 2↑ 4 3 2↑*+18 3/-

Input op1 op2 value Stack
7 7
2 2
7
↑ 7 2 49 49
4 7 2 49 4
49
3 7 2 49 3
4
49
2 7 2 49 2
3
4
49
↑ 3 2 9 9
4
49
* 4 9 36 36
45
+ 49 36 85 85
18 49 36 85 18
85
3 49 36 85 3
18
85
/ 18 3 6 6
85
- 85 6 79 79
79






Q.2
Convert the following infix expression into postfix expression using stack, show your evaluation in the following tabular form.




a) 4↑ 2 + (5 * 4) ↑ 2 – 18 / 3 5 marks
Symbol Postfix Stack
4 4
↑ 4 ↑
2 42 ↑
+ 42↑ +
( 42↑ +(
5 42↑5 +(
* 42↑5 +(*
4 42↑54 +(*
) 42↑54* +
↑ 42↑54* +↑
2 42↑54*2
- 42↑54*2↑+ -
18 42↑54*2↑+18 -
/ 42↑54*2↑+18 -/
3 42↑54*2↑+183 -/
42↑54*2↑+183/-

b) 7+(5-2)+(2 *1)-(5/2) 5 marks
Symbol Postfix Stack
7 7
+ 7 +
( 7 +(
5 75 +(
- 75 +(-
2 752 +(-
) 752- +(-
+ 752-+ +
( 752-+ +(
2 752-+2 +(
* 752-+2 +(*
1 752-+21 +(*
) 752-+21* +
- 752-+21*+ -
( 752-+21*+ -(
5 752-+21*+5 -(/
/ 752-+21*+5 -(/
2 752-+21*+52 -(/
) 752-+21*+52/ -

752-+21*+52/-


..............


Q.2
Convert the following infix expression into postfix expression using stack, show your
evaluation in the following tabular form.
a) 4 2 + (5 * 4) 2 – 18 / 3
b) 7+(5-2)+(2 *1)-(5/2)
Symbol Postfix Stack
7 7
+ 7 +
( 7 +(
5 7 5 +(
- 7 5 +(-
2 7 5 2 +
) 7 5 2 -
+ 7 5 2 - + +
( 7 5 2 - + +(
2 7 5 2 - + 2 +(
* 7 5 2 - + 2 +(*
Symbol Postfix Stack
4 4
4
2 4 2
+ 4 2 +
( 4 2 +(
5 4 2 5 +(
* 4 2 5 +(*
4 4 2 5 4 +(*
) 4 2 5 4 * +
4 2 5 4 * +
2 4 2 5 4 * 2 +
- 4 2 5 4 * 2 + -
18 4 2 5 4 * 2 + 18 -
/ 4 2 5 4 * 2 + 18 - /
3 4 2 5 4 * 2 + 18 3 / -
4 2 5 4 * 2 + 18 3 / -
1 7 5 2 - + 2 1 +(*
) 7 5 2 - + 2 1
- 7 5 2 - + 2 1 * + -
( 7 5 2 - + 2 1 * + -(
5 7 5 2 - + 2 1 * + 5 -(
/ 7 5 2 - + 2 1 * + 5 -(/
2 7 5 2 - + 2 1 * + 5 2 -(/
) 7 5 2 - + 2 1 * + 5 2 / -
7 5 2 - + 2 1 * + 5 2 / -

0 comments:

Post a Comment