Return to labs index
September 18, 2007 (Assignment #3 - Multi-function Calculator in C)

Multi-function Calculator in C
Due September 25, 2007 at 11:59PM(Tuesday)

Overview

This assignment asks you to implement a multi-function calculator in C. It has some peculiar requirements to force you to excercise some of the language features that we've recently learned. It may also require that you use some control structures, loops and conditionals, that we haven't specifically discussed, but in these cases, the syntax will match Java.

Multi-function Calculator Model

You are asked to implement a multi-function calculator. The calculator works much like a simple, standard calculator. The computing model is straight-forward. The calculator has, in effect, three registers. The accumulator keeps the "running total" and is used as the left operand. The accumulator is also the value that is shown on the screen. The input register holds the input from the user, which acts as the right operand. And the operation register, which holds the operation such as add, subtract, multiply, divide, or equals, until it can be executed.

By example, an addition operation would look something like this:

  accumulator = accumulator + input
  
Consider the following example. It shows a few operations as they might proceed on a real calculator. On the left, it shows the key that is pressed on the keyboard. In the middle, it shows the resulting display on the screen. And, on the right, it shows the effect on the registers.

  ON      0     {Accumulator<-0; Input <- 0; Operation <- +}
  4       4     {Input <- 4}
  +       4     {Operation <- +, Accumulator <- (0+4)=4; Input <- 0}
  4       4     {Input <- 4}
  -       8     {Operation <- -; Accumulator <- (4+4)=8; Input <- 0}
  2       2     {Input <- 2}
  /       6     {Operation <- /; Accumulator <- (8-2)=6; Input <- 0}
  2       2     {Input <- 2}
  +       3     {Operation <- +; Accumulator <- (6/2)=3; Input <- 0}
  3       3     {Input <- 3}
  =       6     {Operation <- +; Accum <- (3+3)=6; Input <- 0}
  +       6     {Operation <- -; Accumulator <- 6+0=6; Input <- 0 } 
  

Operations and Implementation

Your calculator library should be called "calculator". It should be implemented using good style. Keep in mind the guidelines we discussed in class for using separate files, naming them properly and with the correct extension, and making proper use of header files, static variables, &c.

Recall from the example above that the initial state of the system is as follows:

Also, please take a careful look at the =operator. It is a bit ticklish.

You should implement the following functions.

Each function represents pressing the corresponding key on the keyboard, such as the +-add key, or X-multiply key, or /-divide key, or =-equals key. The enternumber() function is the equivalent of entering a number at the keyboard. Notice that each function returns a int. This return value is the value that should be displayed on the screen after the key is pressed.

This is a Library, not a Program

The bulk of this assignment asks you to implement a library -- not a functioning program. We will test your library, in part, by compiling it against our test program. Please stick strictly to the interface we describe -- or things may not compile or work as we expect.

The Test Driver

You would be wise to implement a "test driver" to make sure your library works. It should be a non-interactive, prescribed series of tests that ensure that your library works correctly. This test driver should take the form of a main() function, possibly calling other functions within a properly named testdriver file.

We will be grading your test driver as well as the library itself, so please turn it in as well.

Special Restrictions

To make things fun, you are not permitted to use any mathematical operator other than +addition and -subtraction.

We're Here To Help!

As always -- remember, we're here to help!