Homework 4

A Simple Calculator

The Specification - A Simple Calculator

In this program we will be implementing a calculator which accepts positive integer operands and operators as input forming an expression which is then evaluated and the results displayed. The calculator uses a "fancy" display to show input and results in a larger-than-normal size (try the executable provided to see what I mean).

The operators supported by the calculator are:

  +  for addition
  -  for subtraction
  x  for multiplication (note this is not *)
  /  for division
  =  for evaluation

In this calculator, there are NO precedence rules. Operators are evaluated from left to right as they are entered. When the evaluate operator ('=') is entered, the result of the expression is to be displayed, and no further input is accepted until an newline is entered. This newline clears the calculator display making it ready to accept the next expression.

Operands for this calculator may be arbitrary positive integers which may be entered in one of two forms, but MUST ALWAYS be followed by a single space character (this makes implementation easier). You will note that the display shows a small '@' character to indicate when the required space following an operand has been entered. Other than the newline at the end of an expression, all other white space characters entered are ignored by the calculator and not displayed. The forms of operands are:

  • a sequence of digit characters representing a decimal integer.
  • a base integer raised to an integer power. This form is entered by entering the digits of the base followed by the exponent which is entered by holding the "SHIFT" key while entering the exponent digits. The exponent digits are displayed raised above the base digits (again, see the sample executable to see how this looks).

Expressions are entered as an operand followed by an operator. For the arithmetic operators ('+', '-', 'x', and '/'), another operand is expected to follow. As described above, when the evaluate operator ('=') is entered, the result is displayed until a newline is entered.

One additional expression can be entered; namely, a 'q' character entered as the first character after the calculator prompt. This expression terminates the calculator.


1. Understand the problem

In this case, the spec above gives a detailed description of the operation of the calculator. The best way to get a good understanding of how it works is to try running the sample executable in ~ee160/Code.lect/Calc/calc.

2. Do a small example by hand

Sample output from the calculator is given in lieu of a hand example. There are more details to look at but the implementation page shows what each driver is responsible for each of these sample outputs.


3. Write an algorithm for solving the problem

This program is one of moderately significant size, and the only way to even hope to get it working is to take a systematic, modular approach to its implementation. For this homework, I will do the portioning into modules for you to show you how it is done. (Later in the course will you be required to perform this type of design on your own). The development steps you will go through below are the same that I used when writing the sample.

In this section I describe the partioning I choose to approach this task. This is by no means the only partioning possible, but is a reasonable one. Looking at the specification for the calculator, it appears we will need to do things like:

  • get operands and operators from the input,
  • compute the values of expressions, and
  • display things in the "larger-than-life" format.

So, I will divide the task into five subtasks or modules,

the Operands Module, the Operators Module, the Results Module, and the Display Module. Of course, we will also need a Driver Module

as the "glue" that binds this whole thing together. (In fact, we will be writing several driver modules as we go along to test the other modules).

Here is the structure diagram for the entire project:


******************Insert Image****************



4. Translate the algorithm into a programming language (like C)

Before beginning any implementation, let us first discuss the organization we will use for the source code files. It makes sense to store the code for each module in its own file or files.


*********Insert Image***************


the Operands Module,

This module is divided into two submodules, each implemented in its own file:

  • you will write the code in opnd.c for the functions with prototypes in opnd.h
  • and exponent.c will contain functions with prototypes in exponent.h (you have already written part of this in the previous homework)

the Operators Module,

  • you will write the code in ops.c for the functions with prototypes in ops.h.

the Results Module,

This module is divided into two submodules, each implemented in its own file:

  • you will write the code in compute.c for the functions with prototypes in compute.h
  • I have written the code in presults.c and prototypes in presults.h.

the Display Module.

I have written the code for the Display module. I will only give you the compiled module, display.o, with functions with prototypes in display.h.

and the Driver Module

You will be writing 5 drivers in developing this project. See the implementation for details.

In addition, we may be making use of functions provided in chrutil.c with prototypes in chrutil.h.

Even with all this organization, implementing this project is a big task. So we will take a bottom up approach - developing small portions of the overall program, one at a time. We will then test each portion using a "throw away" driver to make sure it works before going on to the next part. We will require you to develop and test your calculator in pieces as described in the implementation.

5. Test the program

Submit using the format according to HW 1, HW2 and HW3

Include all .c files, .h files, and the makefile. Do not submit executables, or the makefile.

Don't forget about the Rating.