Return to labs index
Assignment #3 - Multi-function Calculator in C

Multi-function Calculator in C
Due: September 28, 2010 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 remains 4}
2       2     {Input <- 2}
/       6     {Operation <- /; Accumulator <- (8-2)=6; Input remains 2}
2       2     {Input <- 2}
+       3     {Operation <- +; Accumulator <- (6/2)=3; Input remains 2}
3       3     {Input <- 3}
=       6     {Operation <- +; Accum <- (3+3)=6; Input remains 3 }
+       6     {Operation <- +; Accumulator <- 6+0=6; Input remains 3 }
```

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:

• Accumulator = 0
• Input = 0
• Operation = =

Also, please take a careful look at the =operator. It is a bit ticklish. It, itself, is mathematically a do-nothing operator. But, it still forces the evaluation of the prior operator. If the pror operator is also an equals, then the prior equals is exercised -- which doesn't do any computation.

You should implement the following functions.

• int add()
• int subtract()
• int multiply()
• int divide()
• int equals()
• int power() -- mathematical, not the power-swich
• int enternumber (int number)

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

In the future, you'll be required to make use of shell scripts or Perl programs to serve as test drivers for the prograsm you write in the C Language. But, because we have not yet learned how to implement user or file interaction in C, this program is a special case.

For this assignment, please implement, in C, 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 not be grading your test driver for this lab, but we will in the future. Do not turn it in this time. However, please do put effort into this portion of the assignment -- it will guarantee a good grade for the calculator.

Special Restrictions

• To make things fun, you are not permitted to use any mathematical operator other than +addition and -subtraction.
• Make sure to name your final C file calculator.c and your final header file calculator.h

We're Here To Help!

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

Autograde Tests

• Test 1: Basic Tests - test of each operator and some special cases for each. There are 8 tests of 5 points each.
• Test 2: Edge Case Tests - Tests edge cases such as multiply and power by zero, imperfect division, and maximum/minimum integer values. There are 9 tests of 5 points each.
• Test 3: Stress Tests - There are three tests, each with randomized values and operation. The first test has 10 inputs, while the last two have 100 inputs. The tests include dealing with values outside the range of a signed integer. Each test is worth 5 points.