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:
- Implemented a C++ class.
- Implemented a set of functions that use the class.
- Implemented a program that employs a C++ class and utility functions.
- Implement test drivers for all your functions.
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:
- Your implementation of the fraction class. (30 points)
- A test driver, FractionTest, for the fraction class. (10 points)
- Your implementation of a output library of support functions. (10 point)
- A test driver, PrintTest, for your output library (5 points)
- Your implementation of a simple fraction calculator (Calculator). (25 points)
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.
- FractionT(); Initialize the fraction. The default value should be 0/1
- void Set(int numerator); Set the numerator to the given value. The denominator should be set to be 1.
- void Set(int numerator, int denominator); Set both the numerator and denominator. Reduce the fraction if necessary.
- int Numerator(void ) const; Return the numerator.
- int Denominator(void ) const; Return the denominator.
- float RealValue(void) const; Return the floating point value of the fraction.
- FractionT operator +(const FractionT & other) const; Add two fractions producing a new fraction. The new fraction should be reduced.
- FractionT operator -(const FractionT & other) const; Subtract two fractions producing a new fraction. The new fraction should be reduced.
- FractionT operator *(const FractionT & other) const; Multiply two fractions producing a new fraction. The new fraction should be reduced.
- FractionT operator /(const FractionT & other) const; Divide two fractions producing a new fraction. The new fraction should be reduced.
- int GCD(int a, int b) const; Compute the GCD of two integers. See notes below.
- void Reduce(void); Reduce the fraction.
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
- If the numerator is 0, print a 0.
- If the denominator is 1, just print the numerator.
- Otherwise print numerator/denominator
- Do not print an endl.
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.
- Print a space between the integer and the fraction.
- Print the negative sign with the integer.
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
- At no time should the denominator be allowed to become 0.
- If this would occur due to a call to Set, do not change the value of the fraction.
- If this occurs due to an operation (0/1 / 7/2) set the fraction to be 0/1
- Again, this is bad, we really should have an error, but that is for 330.
- Fractions must always be in reduced form.
- If a fraction is negative, the negative sign should be associated with the numerator.
- - 3/4 should be stored as -3/4
- -3/-4 should be stored as 3/4
- 3/-4 should be stored as -3/4
- When computing GCD make sure both numbers are positive. Mod with negative numbers is not what you expect in C++.
- You may use the atoi function if you wish.
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.