UMBC CMSC201, Computer Science I, Fall 1994 Section 0101, 0102 and Honors

Project 4

Due: Wednesday, November 23, 1994


The objective of this program is to practice working with characters and strings.


Your assignment is to write a simple calculator program that accepts string inputs from the user and evaluates the floating point expression that the user entered. Your calculator program should support the operators +, -, * and /. As in C, all white space (spaces, tabs and newline characters) should be ignored. In particular, the user is allowed to enter the expression over several lines. Also, the input may contain lines with no characters as well as lines which contain only spaces. The end of the expression is signified by the '=' character. Here's a sample run of the program:
lassie% a.out
Enter an expression ending with '='
   1.2 + 2.0 + 3.1 * 4 -

5 + 6

   * 7 =
Answer = 183.4
You do not have to worry about numbers that are too big or too small (overflow and underflow errors) and you do not have to handle input in scientific notation (e.g., 314.159e-2). For simplicity, assume that all numbers begin with a digit (i.e., .1234 is not allowed, but 0.1234 is). Also, your calculator should evaluate the operators left to right without regard to operator precedence. For example:
lassie% a.out
Enter an expression ending with '='
3 + 4 * 5 =
Answer = 35
However, your calculator should give rudimentary error messages:
lassie% a.out
Enter an expression ending with '='
3 4 + 7 =
Error: Unknown operator: 4
lassie% a.out
Enter an expression ending with '='
34a9 + 7 =
Error: Unknown operator: a
lassie% a.out
Enter an expression ending with '='
abd + bde =
Error: Bad Syntax, pos = 0, abd + bde =
You absolutely must spend some time early to think about this project before writing any code. A good design will make your project easy to implement and to debug. A bad design will result in messy, error-prone and undebuggable code.

Implementation Notes

Challenge Problem

Modify your program so it respects operator precedence.

What to turn in

Test your program on the examples shown above. If your program works, copy the file test4 from the directory ~chang/pub/cs201. This file contains a particularly nasty input for your program.
lassie% cat test4

   1.1 * 3.4
+ 7.92

-             4.8

          /2+7+56        / 3 =
To run your program on the test file, use the UNIX redirection command:
lassie% a.out <test4
Enter an expression ending with '='
Answer = 22.1433
Use the script command to save a copy of your output. To do this, remove any old typescript files you have, type script, run your program to duplicate the sample runs then type exit at the prompt. Use an editor (e.g., emacs) to check that the file actually contains your output before you turn it in. When you have successfully created the typescript file, and have deleted all the blank lines at the end of your program, submit your program using the submit201 command that you copied for Project~1. To submit both your project and the typescript file, issue the command:
   % submit201 proj4.c typescript
Please check your own mail to see if you have submitted the correct files.
Last Modified: Wed Nov 9 13:53:28 EST 1994

Richard Chang,