Polish Notation - Example

Example

This uses the same expression as before and the algorithm above.


Token Action Stack Notes
1 Operand 1 Push onto stack.
1 Operand 1 1 Push onto stack.
+ Operator 2 Pop the two operands (1, 1), calculate (1 + 1 = 2) and push onto stack.
2 Operand 2 2 Push onto stack.
+ Operator 4 Pop the two operands (2, 2), calculate (2 + 2 = 4) and push onto stack.
3 Operand 3 4 Push onto stack.
1 Operand 1 3 4 Push onto stack.
1 Operand 1 1 3 4 Push onto stack.
+ Operator 2 3 4 Pop the two operands (1, 1), calculate (1 + 1 = 2) and push onto stack.
7 Operand 7 2 3 4 Push onto stack.
Operator 5 3 4 Pop the two operands (7, 2), calculate (7 − 2 = 5) and push onto stack.
15 Operand 15 5 3 4 Push onto stack.
/ Operator 3 3 4 Pop the two operands (15, 5), calculate (15/5 = 3) and push onto stack.
* Operator 9 4 Pop the two operands (3, 3), calculate (3 * 3 = 9) and push onto stack.
Operator 5 Pop the two operands (9, 4), calculate (9 − 4 = 5) and push onto stack.

The result is at the top of the stack.

Read more about this topic:  Polish Notation

Famous quotes containing the word example:

    Our intellect is not the most subtle, the most powerful, the most appropriate, instrument for revealing the truth. It is life that, little by little, example by example, permits us to see that what is most important to our heart, or to our mind, is learned not by reasoning but through other agencies. Then it is that the intellect, observing their superiority, abdicates its control to them upon reasoned grounds and agrees to become their collaborator and lackey.
    Marcel Proust (1871–1922)