Section 0101, 0102, 0103 and Honors, Fall 1995

Project 4

Due: Monday, November 20, 1995

Objective

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

Assignment

Your assignment is to write a program that answers a user's question about fractions. The user's input is formatted in English. For example, if the user enters:

     1/2 plus 3 / 4 minus7/ 3   times 3 / 8 is
Your program should respond with:
     The answer is -13/32
Note that the spacing around the "/" and around the operators "plus," "minus" and "times" is optional. That is, your program should handle the input properly when the user puts in the spaces, puts in extra spaces or leaves out the spaces. Division is specified using the words "divided by." For example, the user might enter:
     17/3 divided by 9/5 is
Your program should handle an arbitrary number of operations in the user's input line. However, you do not have to worry about operator precedence. Your program should provide some form of error checking and report syntax errors when they are detected. The sample runs at the end of this handout provide some examples of error checking.

Implementation Notes

You will of course use the functions from Project 3 to handle the actual operations on the fractions. You should leave those functions in a separate file and include proj3.h at the top of your file for this project. You should think carefully about what functions will be useful for this project before you write any code. The requirements of the project were designed so that it will be difficult to simply use the scanner programs in Chapter 10 of the textbook. You should study these functions to see how they work, but you will need to design your own functions. For example, you will probably find it useful to have one function that deals with the spaces. You should also think about global variables and whether they should be used in this project. Your grade for this project will depend on how well you designed the project--not just on whether it "works."

Extra Features

There are some optional features that you may want to implement in your project. These extra features let you check how well you designed your program. A well-designed program can be extended easily to handle new features. A poorly designed program will break or becomre really ugly when you try to extend it. The optional features are:
  1. Allow the user to enter the input over several lines. The end of the question is specified by the word "is." Make sure that the program reports the syntax errors as soon as they are found.
  2. Print out the answer for mixed fractions as a whole number followed by a proper fraction. For example, if the answer is 9/5, the program should print out -1 4/5.
  3. Allow the user to enter whole numbers without a denominator. For example, the user might enter "1/2 plus 3 is".
  4. Allow the user to enter the input with mixed fractions. For example, the user might enter the line "1 and 2/3 plus 2 and 1/3 is". This feature is very difficult to implement correctly. You should try this only if you finish the project early. Essentially, you have to implement "and" as an operator with higher precedence than the other operators.

What to turn in

When you are really certain that your program works correctly, use the script command to record some sample runs of your program. You should use difficult inputs to show that your program really works. Finding good test cases is an important part of programming and debugging. You should get into the habit of trying to break your own programs before some else does. Your grade for this project will depend on your choice of sample runs. Turn in the source code of your project and the typescript file using the mail2chang command. (Don't forget to exit from the script command first.)

Some Sample Runs

retriever% a.out
Enter a question about fractions
  1/2 plus 3 / 4 minus7/ 3   times 3 / 8 is
The answer is -13/32

retriever% a.out
Enter a question about fractions
   1/2 plus 24/5 times 1/4 is
The answer is 53/40

retriever% a.out
Enter a question about fractions
 1/2 plus 1/4 plus 1/8 plus 7/9  plus 3/16 minus 5/9 times 4/3 is
The answer is 185/108

retriever% a.out
Enter a question about fractions
   17/3 divided by 9/5 is
The answer is 85/27
retriever% 

retriever% a.out
Enter a question about fractions
  17\4 times 5 is
Error: Illegal operator: \

retriever% a.out
Enter a question about fractions
   1/2 divide 4/3 is
Error: Illegal operator: divide

retriever% a.out
Enter a question about fractions
   1/2 divided 4/3 is
Error: Syntax Error: "divided" not followed by "by".