Skip to main content
ICT
Lesson AB31 - Stacks and Queues
 
Main   Previous
 

LAB ASSIGNMENT AB31.3 page 10 of 10

RPN (Reverse Polish Notation)

Background:

  1. Most calculators use infix notation, in which the operator is typed between the operands. Everyone is familiar with solving infix math expressions, such as 2 + 3, or 9 - 5.

  2. There are two other types of notation: prefix and postfix. In prefix, the operator comes before ("pre") the operands, such as in these expressions: + 2 3 or - 9 5.

  3. Postfix expressions are used on some specialized calculators called RPN calculators, so complex math expressions can be entered without the need for parentheses. Postfix math is also called reverse polish notation (RPN). RPN is defined as a mathematical expression in which the numbers precede the operation (i.e., 2 + 2 is 2 2 + in RPN, or 10 - 3 * 4 is 10 3 4 * - in RPN). Postfix statements look like 2 3 + or 9 5 -.

  4. Here is a comparison of infix versus postfix math expressions:

    infix
    postfix
    5 + ((7 + 9) * 2)
    5 7 9 + 2 * +

    A postfix expression is evaluated as follows:

    - if a value is entered, it is placed on the stack
    - if an operation is entered (+, -, *, /), the stack is popped twice and the operation is applied to those two numbers. The resulting answer is placed back on the stack.

    The expression 5 7 9 + 2 * + is solved in this order:

    Stack: 5
    Stack: 5 7
    Stack: 5 7 9
    Stack: 5 16       (popped 7 and 9, added, pushed result back to stack)
    Stack: 5 16 2
    Stack: 5 32       (popped 2 and 16, multiplied, pushed result back)
    Stack: 37         (The answer is 37)

    Notice that postfix expressions are simply solved by moving from left to right and parentheses are not needed.

  5. When subtracting or dividing, note the order of the operands.

    If the problem to be solved is 7 - 5 (infix), the problem is entered on an RPN calculator in the following order.

    7 (enter)
    5 (enter)
    -

    This causes the stack to be popped twice and the correct expression to be evaluated is:

    7 5 -

    The answer is 2.

    A similar ordering issue surrounds the (/) operator:

    If the problem to be solved is 9 / 2 (infix), the problem is entered on an RPN calculator as:

    9
    2
    /

    The answer is 4.

Assignment:

  1. Write a program to implement a simple RPN calculator that processes only the following type of input:

  2. single-digit integers
    the four integer math operations: +, -, *, /
    'q' or 'Q' indicating taht the problem has been entered

  3. The user will type in single character input until ‘Q’ or ‘q’ is entered. As described above, if a digit is entered, the number is placed on the stack. If an operation is entered, pop two values off the stack, do the math, then return the answer back onto the stack. As keyboard input is entered, the program should keep track of all keystrokes entered to be replayed after ‘Q’ or ‘q’ is entered.

  4. When ‘Q’ or ‘q’ is entered, the program will print out the entire problem (i.e. the sequence of characters that had been input) and the answer. It is suggested that a queue and a stack should be used in this lab.

  5. You must create a modular solution and have at least 2 helper methods beyond the calculate() method that you see in the starter code.

Examples:

    Here are 5 examples of the input and output:

  1. 9
    5
    -
    q
    9 5 - = 4    // Note: This is the output that should be printed out

  2. 7
    3
    *
    6
    /
    Q        // Note: either a 'q' or 'Q'...
    7 3 * 6 / = 3

  3. 8
    4
    7
    +
    -
    q
    8 4 7 + - = -3

  4. 7
    3
    9
    4
    5
    *
    +
    -
    -
    q
    7 3 9 4 5 * + - - = 33

  5. 8
    4
    3
    *
    *
    6
    4
    2
    -
    +
    +
    Q
    8 4 3 * * 6 4 2 - + + = 104

 

Main   Previous
Contact
 © ICT 2006, All Rights Reserved.