#include #include "Fraction.h" using namespace std; // Fraction - default constructor. Set neumerator to 0 and denominator to 1 by default. // param // none // returns // none Fraction::Fraction() { n = 0; d = 1; } // Fraction - Constructor takes n and d as parameters and initializes classes neumerator and // denominator as indicated. // param // int - n The value to initialize the neumerator // int - d The value to initialize the denominator // returns // none Fraction::Fraction(int n, int d) { setValue(n,d); } // setValue - accessor method to (re)set the Fraciton to the indicated neumerator and denominator // param // int - n The value to initialize the neumerator // int - d The value to initialize the denominator // returns // void void Fraction::setValue(int n, int d) { this->n = n; if (d != 0) { this->d = d; } else { cerr << "Error : Cannot set denominator of fraction to 0, received initialize n=" << n << " d=" << d << endl; this->d = 1; } reduce(); } // print - accessor method to display a representation of the fraciton to standard output // param // none // returns // void void Fraction::print() { if (n == 0) { cout << 0; } else if (d == 1) { cout << n; } else if (n > d) { int whole = n / d; int rem = n % d; cout << whole << " " << rem << "/" << d; } else { cout << n << "/" << d; } } // getValue - accessor method returns a floating point representation of the fraciton // param // none // returns // float - The rational fractions value converted to a floating point representation float Fraction::getValue() { return (float(n) / float(d)); } // numerator - accessor method returns the neumerator of this Fraction // param // none // returns // int - The Fraction's neumerator int Fraction::numerator() { return n; } // numerator - accessor method returns the denominator of this Fraction // param // none // returns // int - The Fraction's demonimator int Fraction::denominator() { return d; } // add - perform an arithmetic addition between this Fraction and the Fraction passed in as the right hand // side (r) parameter. Return the result in a newly created Fraction object. // param // Fraction - The right hand side of the addition operation between two Fractions. // returns // Fraction - A new Fraction holding the result of adding this Fraction to the passed in parameter r Fraction Fraction Fraction::add(Fraction r) { int newNeumerator = (n * r.d) + (r.n * d); int newDenominator = d * r.d; return Fraction(newNeumerator, newDenominator); } // sub - perform an arithmetic subtraction between this Fraction and the Fraction passed in as the right hand // side (r) parameter. Return the result in a newly created Fraction object. // param // Fraction - The right hand side of the subtraction operation between two Fractions. // returns // Fraction - A new Fraction holding the result of subtracting the r Fraction from this Fraction Fraction Fraction::sub(Fraction r) { int newNeumerator = (n * r.d) - (r.n * d); int newDenominator = d * r.d; if (newNeumerator < 0) { cerr << "Error - subtraction resulted in negative value, Fraction does not correctly handle negatives" << endl; newNeumerator = -newNeumerator; } return Fraction(newNeumerator, newDenominator); } // mult - perform an arithmetic multiplication between this Fraction and the Fraction passed in as the right hand // side (r) parameter. Return the result in a newly created Fraction object. // param // Fraction - The right hand side of the multiplication operation between two Fractions. // returns // Fraction - A new Fraction holding the result of multiplying the r Fraction times this Fraction Fraction Fraction::mult(Fraction r) { int newNeumerator = n * r.n; int newDenominator = d * r.d; return Fraction(newNeumerator, newDenominator); } // div - perform an arithmetic division between this Fraction and the Fraction passed in as the right hand // side (r) parameter. Return the result in a newly created Fraction object. // param // Fraction - The right hand side of the division operation between two Fractions. // returns // Fraction - A new Fraction holding the result of dividing this Fraction by the right hand Fraction Fraction Fraction::div(Fraction r) { int newNeumerator = n * r.d; int newDenominator = d * r.n; return Fraction(newNeumerator, newDenominator); } // equal - perform boolean comparison to determine if this Fraction is equivalent (==) to the right hand side // Fraction // param // Fraction - The right hand side of the comparison operation being performed. // returns // bool - true if the Fractions are equal, false otherwise. bool Fraction::equal(Fraction r) { return ((n == r.n) && (d == r.d)); } // greaterthan - perform boolean comparison to determine if this Fraction is greater than (>) the right hand side // Fraction // param // Fraction - The right hand side of the greater than comparison operation being performed. // returns // bool - true if this Fraction is greater than the right hand (r) Fraction, false otherwise bool Fraction::greaterthan(Fraction r) { // first find neumerators for left and right based on common denominator in order to correctly compare int leftNeumerator = n * r.d; int rightNeumerator = r.n * d; return (leftNeumerator > rightNeumerator); } // lessthan - perform boolean comparison to determine if this Fraction is less than (<) the right hand side // Fraction // param // Fraction - The right hand side of the less than comparison operation being performed. // returns // bool - true if this Fraction is less than the right hand (r) Fraction, false otherwise bool Fraction::lessthan(Fraction r) { // first find neumerators for left and right based on common denominator in order to correctly compare int leftNeumerator = n * r.d; int rightNeumerator = r.n * d; return (leftNeumerator < rightNeumerator); } // reduce - When called will cause the fraction to reduce itself to it lowest terms. For example // if the fraction is 4/8 3/6 5/10 or 1/2 these will all be reduced to 1/2 after call to this // method. // param // none // returns // void void Fraction::reduce() { int divisor; divisor = gcd(n, d); n = n / divisor; d = d / divisor; } // gcd - This is a private method of the Fraction class that does the actual work of finding the greatest // common denominator (gcd) that can be used as a divisor to reduce a fraction to its simplest terms. // This function implements an algorithm known as Euclids method for finding the gcd. This is an example // or a recursive function, can you figure out how it works? // param // int - n The neumerator of the fraction to find the gcd // int - d The denominator of the fraction to find the gcd // returns // int - The greatest common denominator, for example if n=4 and d=8, gcd is 4 (because both 4 and 8 can be // divided by 4 and this is the largest such number that is a divisor of both n and d). int Fraction::gcd(int n, int d) { if (d == 0) return n; return gcd(d, n % d); }