Skip to main content
ICT
Lesson AB31 - Stacks and Queues
 
Main Previous
Title Page >  
Summary >  
Lesson A1 >  
Lesson A2 >  
Lesson A3 >  
Lesson A4 >  
Lesson A5 >  
Lesson A6 >  
Lesson A7 >  
Lesson A8 >  
Lesson A9 >  
Lesson A10 >  
Lesson A11 >  
Lesson A12 >  
Lesson A13 >  
Lesson A14 >  
Lesson A15 >  
Lesson A16 >  
Lesson A17 >  
Lesson A18 >  
Lesson A19 >  
Lesson A20 >  
Lesson A21 >  
Lesson A22 >  
Lesson AB23 >  
Lesson AB24 >  
Lesson AB25 >  
Lesson AB26 >  
Lesson AB27 >  
Lesson AB28 >  
Lesson AB29 >  
Lesson AB30 >  
Lesson AB31 >  
Lesson AB32 >  
Lesson AB33 >  
Vocabulary >  
 

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:

    5 16 2 * +
    5 32 +
    37

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

  5. The following special cases must be addressed:

    - 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: +, -, *, /

  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 and the answer. It is suggested that a queue and a stack should be used in this lab.

Instructions:

  1. Enter the following 5 postfix problems:

    9 5 -
    7 3 * 6 /
    8 4 7 + -
    7 3 9 4 5 * + - -
    8 4 3 * * 6 4 2 - + +

  2. Your run output should look something like this:

    9 5 - = 4
    7 3 * 6 / = 3
    8 4 7 + - = -3
    7 3 9 4 5 * + - - = 33
    8 4 3 * * 6 4 2 - + + = 104

 

Main Previous
Contact
 © ICT 2006, All Rights Reserved.