The greatest mistake you can make in life is to be continually fearing you will make one

Monday, 8 February 2010

Problems based on Expression Evaluation

Problem 1:
Give postfix form of the following expression
  1. A*(B+(C+D)*(E+F)/G)*H 
  2. A+((B+C)*(D+E)*F)/G
  3. A*(B+D)/E-F-(G+H/K)
  4. ((A-B)*(D/E))/(F*G*H) 
Problem 2:
Evaluate the following postfix expression using a stack and show the contents of stack after each step
  1. 50 40 +18 14-4*+
  2. 100 40 8+20 10 -+*
  3. 5 6 9 +80 5*-/
  4. 120 45+20 10-15+*
  5. 20 45+20 10-15+* 
Problem 3:
Simplify the following infix expressions using operator precedence rules
  1.  8-2*3-1
  2. 12/(6-2)
  3. (4+3)*(8-5)
Solution:
(1) A=8-2*3-1
      * has higher precedence than -. so 2*3 is executed first
      A=8-6-1.both are -.so evaluate from left to right
      A=2-1=1
       A=1

(2)B=12/(6-2)
    operation in parentheses are done first.so 6-2=4
    B=12/4=3
      B=3
(3)C=(4+3)*(8-5)
     operation in parentheses done first
      C=7*(8-5)
      C=7*3
      C=21

Problem 4
Are the following two infix expressions equivalent?
  1. A+B*C
  2. (A+B)*C 
Solution:
      The two infix expressions are different.The multiplication is performed first in the first expression while the addition is performed first in the second expression.

3 comments: