Q BgQuestion:

      
Pupil
Karma Points: 50
Respect (83%):
posted by  KOJ on 11/5/2009 9:18:59 AM  |  status: Closed  |  Earned Karma: 50

help me to evaluate the following postfix experssions !!

Course Textbook Chapter Problem Needs by
Software Design C++ Programming: From Problem Analysis to Program Design (3rd) by Malik 19 3 11/5/2009 at 12:00:00 PM
Question Details:
Evaluate the following postfix experssions:
a)
         8   2   +   3   ×   16   4   /   -   =
solution :
        ( 8 + 2 ) * 3 - ( 16 / 4 )
so, The answer is = 26
but I don't know how to solve this experssions !!
c)
         70   14   4   5   15   3   /   ×   -   -   /   6   +   =
The answer is:  8
d)
         3   5   6   ×   +   13   -   18   2   /   +   =
The answer is:  29
could anyone please show me the steps on how the calculation is done
thanx
Bonus Point Alert! Earn +15 additional karma points for helping this platinum member.

AAnswers:

Answer Question Ask for clarification
Oracle
Karma Points: 18,820
posted by Marth on 11/5/2009 9:42:27 AM  |  status: Live
Asker's Rating: Helpful   
Response Details:
The basic idea is to use a stack.  In practice, this is actually the Reverse Polish Notation used in some scientific calculators - so if you are calculating by hand, you don't need to draw it out.

a)
         8   2   +   3   ×   16   4   /   -   =

8 Push 8 Stack: 8
2 Push 2 Stack: 8 2
+ pop twice from stack, add, and push result onto stack: Pop 2 8 Push (8+2) = 10 Stack: 10
3 Push 3 Stack 10 3
x pop twice from stack, multiply, and push result onto stack: Pop 3 10 Push (10*3) = 30 Stack: 30
16 Push 16 Stack: 30 16
4 Push 4 Stack: 30 16 4
/ pop twice from stack, divide, and push result onto stack: Pop 4 16 Push (16/4) = 4 Stack: 30 4
- pop twice from stack, subtract, and push result onto stack: Pop 4 30 Push (30 - 4) = 26 Stack: 26

And your answer is stack.peek(), or 26.



Thank you for rating my response. Feel free to PM me if you have further questions.

Java Genius

"Lois Lane is falling, accelerating at an initial rate of 32ft per second per second. Superman swoops down to save her by reaching out two arms of steel. Ms. Lane, who is now traveling at approximately 120 miles per hour, hits them, and is immediately sliced into three equal pieces." - Sheldon
Oracle
Karma Points: 31,807
posted by rapunzel on 11/5/2009 1:36:59 PM  |  status: Live
Asker's Rating: None Provided    Moderator's Rating: Helpful
rachel 's's comment:
"thanx"
Response Details:
please rate - thanks
think of it this way -each operator, operates on the previous 2 operands - decompose from there
a)
         8   2   +   3   ×   16   4   /   -   =
starting left to right get operator +, previous 2 operands are 8 & 2 so we have (8+2)  this is now considered an operand so we have
         (8   + 2)     3   ×   16   4   /   -   =
starting left to right 1st operator is x, the previous opeands are (8+2) and 3 so we now have
((8+2)x3) as the first operand
now have
((8+2)x3) 16 4 / - =
starting on the left 1st operator is / last 2 operands are 16 and 4, combine thes to make the operand (16/4)
now have
((8+2)x3) (16/4) -=
starting on the left first operator is -  so now have
((8+2)x3)- (16/4) =
which is in highly parenthesied infix notation which evaluates to  30-4=26
solution :
        ( 8 + 2 ) * 3 - ( 16 / 4 )
so, The answer is = 26
but I don't know how to solve this experssions !!
c)
         70   14   4   5   15   3   /   ×   -   -   /   6   +   =
same as above    1st step gives us
      70   14   4   5   (15   / 3)     ×   -   -   /   6   +   =
second step
    70   14   4   (5 ×  (15   / 3)  )      -   -   /   6   +   =
next
    70   14   (4     -  (5 ×  (15   / 3)  ) )    -   /   6   +   =
next
    70   (14  -  (4     -  (5 ×  (15   / 3)  ) )  )    /   6   +   =
 (70  /  (14  -  (4     -  (5 ×  (15   / 3)  ) )  ) )     6   +   =
(70  /  (14  -  (4     -  (5 ×  (15   / 3)  ) )  ) )    +  6     =
If I did the math right this evaluates to  16 not 8, the infix is correct
The answer is:  8
d)
         3   5   6   ×   +   13   -   18   2   /   +   =
repeating as above
3   (5  ×  6)     +   13   -   18   2   /   +   =
(3  + (5  ×  6)  )      13   -   18   2   /   +   =
((3  + (5  ×  6)  )   -   13 )     18   2   /   +   =
((3  + (5  ×  6)  )   -   13 )     (18 /    2 )   +   =
((3  + (5  ×  6)  )   -   13 )   +  (18 /    2 )      =
The answer is:  29
Note to all members 1. Please 1 question per post 2. Show respect to your fellow members by rating all answers. 3. When rating remember that the first answer is not necessarily the best answer. 4. When answering questions, explain what you are doing, so that the asker will learn, don't just give a meaningless number. 5. Your answers should be your work. Don't copy from another member, this is Karma abuse and possible disciplinary actions against you. and GOOD LUCK to you all!!
Answer Question Ask for clarificarion

Join Cramster's Community

Cramster.com brings together students, educators and subject enthusiasts in an online study community. With around-the-clock expert help and a community of over 100,000 knowledgeable members, you can find the help you need, whenever you need it. Join for free today » How Cramster is different from tutoring »