CS210
Tutorial 4
Solutions for Problems 5-8

 

 

  1. Assume that a=7.0, b=4.0, c=3.0 and d=-2.0. Assume also that ~ represents the unary minus operation. Evaluate the postfix expressions:
    We decide to use the underlining technique, an operand stack may also be used.

a .     a b c d + / *

7.0  4.0  3.0  -2.0  + / *
7.0  4.0  1.0  /  *

7.0  4.0  *
28.0

b .    a b c + / d *

7.0  4.0  3.0  +  / -2.0  *
7.0  7.0  / -2.0  *
1.0  -2.0  *
-2.0

c .     a  ~ b  c  + -
7.0  ~ 4.0  3.0  +  -
-7.0  4.0  3.0  +  -
-7.0  7.0  -
-14.0

  1. Repeat exercise 5 tracing the contents of the operator stack used for evaluation.

a .     a b c d + / *

-2.0

 

 

 

 

 

 

3.0

+

1.0

/

 

*

 

4.0

à

4.0

à

4.0

à

 

7.0

3.0 + -2.0 = 1.0

7.0

4.0/1.0=4.0

7.0

7.0*4.0=28.0

28.0

b .    a b c + / d *

 

 

 

 

 

 

 

3.0

+

 

/

 

*

 

4.0

à

7.0

à

-2.0

à

 

7.0

4.0+3.0=7.0

7.0

7.0/7.0=1.0

1.0

1.0*-2.0=-2.0

-2.0

c .     a  ~ b  c  + -

 

 

 

 

 

 

 

 

~

3.0

+

 

-

 

 

à

4.0

à

7.0

à

 

7.0

-(7.0)=-7.0

-7.0

4.0+3.0=7.0

-7.0

-7.0-7.0=-14.0

-14.0

 

7.Convert the postfix expressions in exercise 5 to infix form.

a .     a b c d + / *
(a
(b/(c+d)))

b .    a b c + / d *
(a/(b+c))*
d

c .     a  ~ b  c  + -
(~a) – (b+c)

8.Convert the infix to postfix:

a .     a * b + c – d
a  b  *  c  +  d  -

b .    (a + b) / c + d
a  b  +  c  / d  +

c .     ((a > 3) && ( a < 9)) || ! (a > 0)
a  3  > a  9 < && a  0  >  !  ||

d .    a – (b –(c –(d –e)))
a  b  c  d  e  -  -  -  -

Hosted by www.Geocities.ws

1