Homework 4, A Fraction Class.

Short Description:

Create a C++ class to implement fractions.

This assignment is worth 80 points.

Goals

When you finish this homework, you should have: When this assignment is complete, you should have three executable program, Calculator, FractionTest and PrintTest. You should have five source code files (.cpp or .C), two header files and a Makefile.

Formal Description

For this assignment you will be implementing and using a fraction class. This assignment will consist of several parts:

For the first part, implement a fraction class. Fractions represent the ratio of two integers a numerator and a denominator. In reduced form, the greatest common divisor (GCD) between the numerator and the denominator should be 1.

Your class should be defined by FractionT.h.

GCD

Euclid described an algorithm for computing the GCD of two positive integers. The algorithm for this function is given as
function gcd(a, b)
    while b ≠ 0
        t := b
        b := a mod b
        a := t
    return a
If you have had Discrete Math, you probably know this algorithm. If not, don't worry about why it works, just use it.

Implement a test driver for your class called FractionTest.C/.cpp. Actually, you should do this as you are developing the classs.

Next implement a pair of utility functions in FunctionUtils.C/cpp.

Using FractionUtils.h implement the two routines PrintFraction and PrintReducedFraction.

PrintFraction prints the given input as an improper function. The following rules apply

PrintMixedFraction uses the same rules with the exception that if the numerator is greater than the denominator, the fraction is printed as a mixed fraction. Ie 4/3 should be printed as 1 1/3.

Implement a test driver for your utility functions called PrintTest.C/.cpp.

Finally, implement a simple calculator that uses fractions.

This calculator should prompt with "> ", read the line that consist of an expression in the from fraction operation fraction, and prints the result.

The operations supported should be +, -, *, /. All components will be separated by a space and the expression will be terminated by an newline. You may assume that there will be no errors in the input and that there are no spaces in the fractions, including the sign of the fraction.

If the first fraction is "exit", the program should exit.

An example run should look like this

> 1/2 + 1/2
1
> 1/2 * 1/2
1/4
> 2/3 + 2/3 
1 1/3
> 1/2 + -1/2 
0
> -1/2 + -1/2
-1
> exit

If you name your test driver FractionTest and PrintTest this Makefile will build your entire project.

Notes

Required Files

A single tar file containing the source code and makefile for this program.

Submission

Email your tar file as an attachment to dbennett@edinboro.edu. Make sure that the title indicates that this is homework4 and that your name and section are included in the body of the message.