CSCI 216
Programming Project #2
Date
Due: 7 October 2015
For
this project you are to implement a fraction library that does exact fraction
calculations. You should set up a
structure named fraction that contains two integers, one for the numerator and one
for the denominator. You should
implement functions to set a fraction to a default value, to add, subtract,
multiply and divide two fractions and return a fraction, to read a fraction,
print a fraction, reduce a fraction, and find the gcd
of two integers. Your program should
work with the sample main program provided below. Here are prototypes for the functions you
need to write:
void init(fraction x); // set fraction to 0
void init(fraction x, int n); //
set fraction to n/1
void init(fraction x, int a, int b); // set fraction to a/b
fraction add(fraction a, fraction b); // adds a and b and
returns the sum (a and b unchanged)
fraction sub(fraction a, fraction b); // subtracts a and b and
returns difference
fraction mul(fraction a,
fraction b); //
multiplies a and b and returns the product
fraction div(fraction a, fraction b); // divides a and b and returns the
quotient
void print(fraction x); //
prints the fraction (no improper fractions)
void read(fraction x); //
reads fraction 3 / 5 into x
void reduce(fraction x); //
simplifies x to lowest common terms
int gcd(int a, int b); // finds
greatest common divisor of a and b
You
should make a .h file that has these prototypes and
the structure definition in it. Don’t
forget our trick to make sure it is only included once.
You
should also make a .cpp file with the implementations
of all these functions.
Finally
I will give you a main.cpp
file that you are to include to actually test and use your fraction
implementation.