Project 5: RPN Calculator

The goal of this project is to practice using stacks in an algorithm.

1. Introduction

Polish mathematician Jan Lukasiewicz invented a notation for arithmetic where the operator comes before the operands, for example + 5 3 instead of 5 + 3. The advantage of this is that the order of operation is always unambiguous without the need for parentheses (or even precedence rules). For example, 5 * 3 - 9 is written - * 5 3 9, whereas 5 * (3 - 9) is written * 5 - 3 9. In honor of its inventor (or his nationality, at any rate), this is referred to as Polish notation. (It is also called prefix notation, in contrast to normal arithmetic notation which is thus infix.)

Reverse Polish notation is similar except that the operator is put after the operands (so it is also postfix notation). The advantage is that all the operands are then known before the operator is read, if we read the string from left to right. This notation is particularly interesting for our purposes, because a stack can be used to evaluate expressions in RPN. Your task is to write a calculator that evaluates expressions entered in RPN.

2. Setup

In this project you will use the same GUI as in the earlier calculator example. After making the directory for this project, checkout the same files as in Project 2:

cp /cslab.all/ubuntu/cs245/proj2/* .

3. Calculator details

One difficulty in using RPN, especially on a simulated hand-held calculator, is how to delimit the operands. The string 937+ could mean 9 + 37 or 93 + 7. In a string, this disambiguation would be done using spaces. For your calculator, you should interpret the "." button as indicating the boundary between numbers. Consequently, your calculator needs to work only on integers; division should be integer division. This also means that you don't have to worry about screen overflow or other formatting. I also won't be checking for your you handle division by zero. Additionally, the equals button is useless-- you do not need to do anything when it is pressed.

Generally this is an easier project than the previous calculator project (and easier than the next calculator project...). The emphasis is not on polishing (heh heh) the calculator's extreme cases but on using the stack.

Even though this section of the course is about data structures, you should keep thinking about object-oriented design. You've heard that in the future you will be given a project where you have to write a calculator without any ifs. I highly recommend you do a practice run on that this time. See if you can avoid using any if statements this time around--or at least as few as possible, and isolate any you need.

You should also implement the clear button. This should reset the entire calculator. Moreover, if the user presses the operation buttons when there is not enough information in the stack, your calculator should just ignore it. You calculator should not crash in such a situation.

4. To turn in

Copy all the files you made or modified to a turn-in directory I've made for you.

cp filename /cslab.all/ubuntu/cs245/turnin/proj5/{johncharles,suzanne,david,lily,kendall,michael,daniel}

DUE:Wednesday, April 1, 5:00 pm.


Thomas VanDrunen
Last modified: Mon Mar 30 15:55:51 CDT 2009