Recent Changes - Search:

Home

Is the Laser up?

People

Publications

Calendar

Projects

Spring 2012

Older Courses

Fall 2011

Spring 2011

Fall 2010

Spring 2010

Fall 2009

Spring 2009

Fall 2008

Spring 2008

Fall 2007

HOWTOs

edit SideBar

Stack-BasedCalculator

Elliott Moy
December 10, 2009

Overview

This project implements a stack-based Reverse Polish Notation calculator in Scheme. Unlike infix form (in which the operations are between arguments), numbers are placed on a stack and operations are done on the top of the stack.

Screenshot

Concepts Demonstrated

  • Data abstraction
    • Types are data abstractions on lists.
    • The stack object's internal stack is actually a list with stack functions to access it.
  • Object Orientation
    • Stacks and calculators are implemented as objects using message passing style
    • I used the message passing style since it looks a lot like objects in C++/Java and that's what I'm most comfortable with

External Technology

  • Scheme GUI Toolkit
    • I used the Scheme GUI Toolkit for the calculator interface.
    • The interface is a dialog window.
    • Buttons are organized in panel objects.

Innovation

Most calculators use infix notation, for example (1 + 2 * (3 - 4) / 5). Reverse Polish notation is an alternate way to do calculations. I find it a lot easier to use since I don't have to worry about order of operations and balancing parentheses.

Technology Use Block Diagram

Additional Remarks

This project was inspired by an old HP 48-GX calculator that I used to use a lot in Physics I and II. It was stack based but did not have a type system.

Using the calculator: Place stuff on the stack by entering numbers in the VALUE field and their types in the TYPE field. The TYPE is a series of alternating symbols and numbers, starting with a symbol and ending with a number. Symbols represent a unit of measurement such as meters and the numbers after them represent their magnitudes. For example, acceleration in meters per second square might be represented as m 1 s -2 since the magnitude of meters is 1 and the magnitude of seconds is -2.

Operations are always done at the top of the stack unless otherwise specified.

Finally, the display is inverted. The bottom of the stack is at the top of the display and the top of the stack is at the bottom of the display. This is so that when numbers are entered for division or subtraction, the numerator or the number to be subtracted from comes first.

Examples:

Simple Numbers: Type the number into the VALUE field and click enter.

Number with type: Gravity (-9.8 meters per second squared) is entered as: Type -9.8 into the VALUE field. Type m 1 s -2 into the TYPE field. Click Enter.

Complex Numbers: Just type them into the value field normally, for example: 4+2i. Click Enter.

Arithmetic operations:

Enter two numbers, first operand entered first (follow above examples) and press the add, subtract, multiply, or divide buttons. Note that you can only add or subtract values of the same type.

Trigonometric functions: Place a number on the stack and press a trigonometric function.

Special irrationals (e and pi): There are buttons that immediately place these on the stack.

Conversions: If there's a number of a type containing units that need to be changed to other units, there is a "convert" button to convert units.

For example, converting square feet to square meters:

Type 10 in the value field and "ft 2" without the quotes. Click the enter button. Next, type "ft" without quotes in the value field and "m" without quotes in the type field. Click the Convert VALUE to TYPE button.

Edit - History - Print - Recent Changes - Search
Page last modified on December 09, 2009, at 09:56 PM