[Tutor] Using a Stack to evaluate postfix....?
Sean 'Shaleh' Perry
shalehperry@attbi.com
Wed Jun 18 01:09:02 2003
On Tuesday 17 June 2003 20:50, evros wrote:
> what happen if we have an example with a negative number ex:
> ((2*3)-10)= -4 in postfix will be liek that 23*10- is that correct?
> if i try to use stack now to evaluate i will have :push 2 then push 3,then
> perfom the operation* so we have 6 push 6 then push 10 then pop the operand
> off the stack. my question now are we gona have 10-6 or 6-10 . which one is
> going first? thanks for teh help
let's rewrite this in postfix:
2 3 * 10 -
so if you tilt your head, this is also the stack (-:
after the multiplication you have
6 10 -
which yields -4.
Basically, imagine the numbers as say apples. Pop one off the stack and it
goes in your left hand, then pop the second one into your right hand. The
operator then tells you what to do with the apples. Postfix is always read
left -> right so there is no confusion.
10 6 - is different from 6 10 -.