Presentation is loading. Please wait.

Presentation is loading. Please wait.

Reverse Polish Notation (RPN) & Stacks CSC 1401: Introduction to Programming with Java Week 14 – Lecture 2 Wanda M. Kunkle.

Similar presentations


Presentation on theme: "Reverse Polish Notation (RPN) & Stacks CSC 1401: Introduction to Programming with Java Week 14 – Lecture 2 Wanda M. Kunkle."— Presentation transcript:

1 Reverse Polish Notation (RPN) & Stacks CSC 1401: Introduction to Programming with Java Week 14 – Lecture 2 Wanda M. Kunkle

2 Reverse Polish Notation (RPN) RPN, also known as postfix notation, was invented by Australian philosopher and computer scientist Charles Hamblin in the mid-1950s. RPN, also known as postfix notation, was invented by Australian philosopher and computer scientist Charles Hamblin in the mid-1950s. For a good explanation of RPN, let’s pay a visit to the Wikipedia Web site: For a good explanation of RPN, let’s pay a visit to the Wikipedia Web site: http://en.wikipedia.org/wiki/Reverse_polish_n otation http://en.wikipedia.org/wiki/Reverse_polish_n otation http://en.wikipedia.org/wiki/Reverse_polish_n otation http://en.wikipedia.org/wiki/Reverse_polish_n otation

3 RPN Example #1 Consider the following expression in algebraic (infix) notation: 1 + 4 * 3 = ? Is the answer 13 or 15? Consider the following expression in algebraic (infix) notation: 1 + 4 * 3 = ? Is the answer 13 or 15?

4 RPN Example #1 Consider the following expression in algebraic (infix) notation: 1 + 4 * 3 = ? Is the answer 13 or 15? It’s 13 because of order of operations (Please Excuse My Dear Aunt Sally). Consider the following expression in algebraic (infix) notation: 1 + 4 * 3 = ? Is the answer 13 or 15? It’s 13 because of order of operations (Please Excuse My Dear Aunt Sally).

5 RPN moves the operator to the end of the expression to avoid confusion (and to eliminate the need for parentheses) RPN moves the operator to the end of the expression to avoid confusion (and to eliminate the need for parentheses) 4 5 + 6 * 4 5 + 6 * What is the result? What is the result? 4 5 + is 4 + 5 = 9 4 5 + is 4 + 5 = 9 9 6 * is 9 * 6 = 54 9 6 * is 9 * 6 = 54 4 5 + 6 * is therefore the same as (4 + 5) * 6 4 5 + 6 * is therefore the same as (4 + 5) * 6 Steps: Steps: 1. 4 5 + 6 * 2. 9 6 * 3. 54 RPN Example #1

6 RPN Example #2 (4 + 5) / (6 + 7) (4 + 5) / (6 + 7) What is this converted to RPN? What is this converted to RPN?

7 RPN Example #2 (4 + 5) / (6 + 7) (4 + 5) / (6 + 7) What is this converted to RPN? What is this converted to RPN? 4 5 + 6 7 + / 4 5 + 6 7 + /

8 RPN Example #3 [(4 + 5) * (2 + 3) + 6] / (8 + 7) [(4 + 5) * (2 + 3) + 6] / (8 + 7) What is this converted to RPN? What is this converted to RPN?

9 RPN Example #3 [(4 + 5) * (2 + 3) + 6] / (8 + 7) [(4 + 5) * (2 + 3) + 6] / (8 + 7) What is this converted to RPN? What is this converted to RPN? 4 5 + 2 3 + * 6 + 8 7 + / 4 5 + 2 3 + * 6 + 8 7 + /

10 Parsing RPN Expressions Writing a program to parse 2 * (3 + 4) is not easy. Writing a program to parse 2 * (3 + 4) is not easy. Writing a program to parse 2 3 4 + * is easier. Writing a program to parse 2 3 4 + * is easier. We can do so using a stack. We can do so using a stack.

11 Parsing RPN Expressions A program to parse 2 3 4 + * using a stack would work as follows: A program to parse 2 3 4 + * using a stack would work as follows: 2 3232 432432 432432 7272 7272 14 + *

12 Algorithm for a RPN Calculator While there are still input values left: While there are still input values left: Read the next value from input. Read the next value from input. If the value is a number If the value is a number Push it onto the stack. Push it onto the stack. Otherwise, the value is an operator. Otherwise, the value is an operator. It is known that the operator takes 2 operands. It is known that the operator takes 2 operands. So, pop the top 2 values from the stack. So, pop the top 2 values from the stack. If there are fewer than 2 values on the stack If there are fewer than 2 values on the stack (Error) The user has not input sufficient values in the expression. (Error) The user has not input sufficient values in the expression. Evaluate the operation, with the values as operands. Evaluate the operation, with the values as operands. Push the returned results, if any, back onto the stack. Push the returned results, if any, back onto the stack. If there is only one value in the stack If there is only one value in the stack That value is the result of the calculation. That value is the result of the calculation. If there are more values in the stack If there are more values in the stack (Error) The user input too many values. (Error) The user input too many values.

13 Sample Program Now let’s visit a Web site that implements a RPN calculator: Now let’s visit a Web site that implements a RPN calculator: Reverse Polish Notation (stack) calculator Reverse Polish Notation (stack) calculator Reverse Polish Notation (stack) calculator Reverse Polish Notation (stack) calculator


Download ppt "Reverse Polish Notation (RPN) & Stacks CSC 1401: Introduction to Programming with Java Week 14 – Lecture 2 Wanda M. Kunkle."

Similar presentations


Ads by Google