2,565 views
0 votes
0 votes
Result for evaluating the following post fix

105+606/*8-

I'm confused whether it is correct postfix expression.

1 Answer

1 votes
1 votes

It is indeed a correct postfix expression. Visualize it as an input sequence which begins with 10 and the input comes in the order:- 

10, 5, +, 60, 6, /, *, 8, -

as soon as 10 is seen it is pushed onto the stack. 

 
 
 
  10

after 10 the next input is 5 which is again pushed onto the stack:

 
 
   5
  10

 The next input is a + operator, whenever a binary operator is seen, the top two values on the stack are popped and the expression formed by the operator and the operand is evaluated: 10+5, the result of this expression( which in this case is 15) will be pushed back onto the stack. Now the stack should look like:

 
 
 
  15

the next inputs 60 and then 6 are similarly pushed onto the stack :

 
   6
  60
  15

The next operator seen is /, which will be evaluated as: 60/6=10 and 10 will be pushed onto the stack;

 
 
  10
  15

Similarly for the * operator: 15*10=150

 
 
 
  150

 The next input is 8 which will be pushed onto stack:

 
 
    8
  150

Now the final input is a subtraction operator: 150-8=142

 
 
 
  142

This value left on the stack is the evaluation of your postfix expression.

I guess this should have cleared your doubt. If the confusion still persists, feel free to ask for clarification.

Related questions

1 votes
1 votes
1 answer
1
2 votes
2 votes
1 answer
4