Presentation is loading. Please wait.

Presentation is loading. Please wait.

Reverse Polish Notation Written by J.J. Shepherd.

Similar presentations


Presentation on theme: "Reverse Polish Notation Written by J.J. Shepherd."— Presentation transcript:

1 Reverse Polish Notation Written by J.J. Shepherd

2 Reverse Polish Notation Most math expressions you have seen were written in “in-fix” notation Example: 3 + 5 However there is another notation called “post- fix” or “Revere Polish Notation” where the operator is at the end Example 3 5 +

3 Big Idea Use a stack to solve reverse polish notation You push onto the stack when there’s a number When there’s an operator (+, -, *, /) then – Pop off two items from the stack – Evaluate the two numbers with that operation – Push the result back to the stack Once it has completed the expression there should only be one item left on the stack

4

5 Reverse Polish Example Here the user has inputted the string 3 2 + Head Tail 3 2 +

6 Reverse Polish Example 3 is a number so push it on the stack Head Tail 3 2 + 3

7 Reverse Polish Example 2 is a number so push it on the stack Head Tail 3 2 + 32

8 Reverse Polish Example + is an operator so pop of two Head Tail 3 2 + 32

9 Reverse Polish Example + is an operator so pop of two Head Tail 3 2 + 32 POP + 2

10 Reverse Polish Example + is an operator so pop of two Head Tail 3 2 + 3 POP 3 + 2

11 Reverse Polish Example Evaluate Head Tail 3 2 + 5

12 Reverse Polish Example Push the value back Head Tail 3 2 + 5 5

13 Reverse Polish Example DONE! Head Tail 3 2 + 5

14

15 Reverse Polish Example Here the user has inputted the string 8 2 – 3 * Head Tail 8 2 – 3 *

16 Reverse Polish Example 8 is a number so push it on the stack Head Tail 8 2 – 3 * 8

17 Reverse Polish Example 2 is a number so push it on the stack Head Tail 8 2 – 3 * 82

18 Reverse Polish Example - is an operator so pop of two Head Tail 8 2 – 3 * 82

19 Reverse Polish Example + is an operator so pop of two Head Tail 8 2 – 3 * 82 POP - 2

20 Reverse Polish Example + is an operator so pop of two Head Tail 8 2 – 3 * 3 POP 8 - 2

21 Reverse Polish Example Evaluate Head Tail 8 2 – 3 * 6

22 Reverse Polish Example Push the value back Head Tail 8 2 – 3 * 6 6

23 Reverse Polish Example 3 is a number so push it onto the stack Head Tail 8 2 – 3 * 63

24 Reverse Polish Example * is an operator so pop of two Head Tail 8 2 – 3 * 62 POP * 3

25 Reverse Polish Example * is an operator so pop of two Head Tail 8 2 – 3 * 6 POP 6 * 3

26 Reverse Polish Example Evaluate Head Tail 8 2 – 3 * 18

27 Reverse Polish Example Push the value back Head Tail 8 2 – 3 * 18

28 Reverse Polish Example DONE! Head Tail 8 2 – 3 * 18


Download ppt "Reverse Polish Notation Written by J.J. Shepherd."

Similar presentations


Ads by Google