**REDUCE FRACTIONS:**

**Client Program:**

#include #include "Fraction.h" #include #include #include using namespace std; using namespace cs_Fraction; void BasicTest(); void RelationTest(); void BinaryMathTest(); void MathAssignTest(); bool eof(ifstream& in); string boolString(bool convertMe); int main() { BasicTest(); RelationTest(); BinaryMathTest(); MathAssignTest(); } void BasicTest() { cout << "n----- Testing basic Fraction creation & printingn"; cout << "(Fractions should be in reduced form, and as mixed numbers.)n"; const Fraction fr[] = {Fraction(4, 8), Fraction(-15,21), Fraction(10), Fraction(12, -3), Fraction(), Fraction(28, 6), Fraction(0, 12)}; for (int i = 0; i < 7; i++){ cout << "Fraction [" << i <<"] = " << fr[i] << endl; } cout << "n----- Now reading Fractions from filen"; ifstream in("Fraction.txt"); assert(in); while (!eof(in)) { Fraction f; if (in.peek() == '#') { in.ignore(128, 'n'); //skip this line, it's a comment } else { in >> f; cout << "Read Fraction = " << f << endl; } } } bool eof(ifstream& in) { char ch; in >> ch; in.putback(ch); return !in; } string boolString(bool convertMe) { if (convertMe) { return "true"; } else { return "false"; } } void RelationTest() { cout << "n----- Testing relational operators between Fractionsn"; const Fraction fr[] = {Fraction(3, 6), Fraction(1,2), Fraction(-15,30), Fraction(1,10), Fraction(0,1), Fraction(0,2)}; for (int i = 0; i < 5; i++) { cout << "Comparing " << fr[i] << " to " << fr[i+1] << endl; cout << "tIs left < right? " << boolString(fr[i] < fr[i+1]) << endl; cout << "tIs left <= right? " << boolString(fr[i] <= fr[i+1]) << endl; cout << "tIs left > right? " << boolString(fr[i] > fr[i+1]) << endl; cout << "tIs left >= right? " << boolString(fr[i] >= fr[i+1]) << endl; cout << "tDoes left == right? " << boolString(fr[i] == fr[i+1]) << endl; cout << "tDoes left != right ? " << boolString(fr[i] != fr[i+1]) << endl; } cout << "n----- Testing relations between Fractions and integersn"; Fraction f(-3,6); int num = 2; cout << "Comparing " << f << " to " << num << endl; cout << "tIs left < right? " << boolString(f < num) << endl; cout << "tIs left <= right? " << boolString(f <= num) << endl; cout << "tIs left > right? " << boolString(f > num) << endl; cout << "tIs left >= right? " << boolString(f >= num) << endl; cout << "tDoes left == right? " << boolString(f == num) << endl; cout << "tDoes left != right ? " << boolString(f != num) << endl; Fraction g(1,4); num = -3; cout << "Comparing " << num << " to " << g << endl; cout << "tIs left < right? " << boolString(num < g) << endl; cout << "tIs left <= right? " << boolString(num <= g) << endl; cout << "tIs left > right? " << boolString(num > g) << endl; cout << "tIs left >= right? " << boolString(num >= g) << endl; cout << "tDoes left == right? " << boolString(num == g) << endl; cout << "tDoes left != right ? " << boolString(num != g) << endl; } void BinaryMathTest() { cout << "n----- Testing binary arithmetic between Fractionsn"; const Fraction fr[] = {Fraction(1, 6), Fraction(1,3), Fraction(-2,3), Fraction(5), Fraction(-4,3)}; for (int i = 0; i < 4; i++) { cout << fr[i] << " + " << fr[i+1] << " = " << fr[i] + fr[i+1] << endl; cout << fr[i] << " - " << fr[i+1] << " = " << fr[i] - fr[i+1] << endl; cout << fr[i] << " * " << fr[i+1] << " = " << fr[i] * fr[i+1] << endl; cout << fr[i] << " / " << fr[i+1] << " = " << fr[i] / fr[i+1] << endl; } cout << "n----- Testing arithmetic between Fractions and integersn"; Fraction f(-1, 2); int num = 4; cout << f << " + " << num << " = " << f + num << endl; cout << f << " - " << num << " = " << f - num << endl; cout << f << " * " << num << " = " << f * num << endl; cout << f << " / " << num << " = " << f / num << endl; Fraction g(-1, 2); num = 3; cout << num << " + " << g << " = " << num + g << endl; cout << num << " - " << g << " = " << num - g << endl; cout << num << " * " << g << " = " << num * g << endl; cout << num << " / " << g << " = " << num / g << endl; } void MathAssignTest() { cout << "n----- Testing shorthand arithmetic assignment on Fractionsn"; Fraction fr[] = {Fraction(1, 6), Fraction(4), Fraction(-1,2), Fraction(5)}; for (int i = 0; i < 3; i++) { cout << fr[i] << " += " << fr[i+1] << " = "; cout << (fr[i] += fr[i+1]) << endl; cout << fr[i] << " -= " << fr[i+1] << " = "; cout << (fr[i] -= fr[i+1]) << endl; cout << fr[i] << " *= " << fr[i+1] << " = "; cout << (fr[i] *= fr[i+1]) << endl; cout << fr[i] << " /= " << fr[i+1] << " = "; cout << (fr[i] /= fr[i+1]) << endl; } cout << "n----- Testing shorthand arithmetic assignment using integersn"; Fraction f(-1, 3); int num = 3; cout << f << " += " << num << " = "; cout << (f += num) << endl; cout << f << " -= " << num << " = "; cout << (f -= num) << endl; cout << f << " *= " << num << " = "; cout << (f *= num) << endl; cout << f << " /= " << num << " = "; cout << (f /= num) << endl; cout << "n----- Testing increment/decrement prefix and postfixn"; Fraction g(-1, 3); cout << "Now g = " << g << endl; cout << "g++ = " << g++ << endl; cout << "Now g = " << g << endl; cout << "++g = " << ++g << endl; cout << "Now g = " << g << endl; cout << "g-- = " << g-- << endl; cout << "Now g = " << g << endl; cout << "--g = " << --g << endl; cout << "Now g = " << g << endl; }

**Frac data:**

# This file shows the patterns your Fraction class needs to be able to # read. A Fraction may be just a single integer, two integers separated by # a slash, or a mixed number which consists of an integer, followed by a + # and then two integers with a slash. A minus sign may appear in the # very first character to indicate the whole Fraction is negative. # No white space is allowed in between the component parts of a Fraction. # 1/3 3/6 3072/4096 -4/5 12/2 5 -8 21/15 -50/3 1+1/4 1+5/5 -4+3/12 -10+10/12

**Output:**

----- Testing basic Fraction creation & printing (Fractions should be in reduced form, and as mixed numbers.) Fraction [0] = 1/2 Fraction [1] = -5/7 Fraction [2] = 10 Fraction [3] = -4 Fraction [4] = 0 Fraction [5] = 4+2/3 Fraction [6] = 0 ----- Now reading Fractions from file Read Fraction = 1/3 Read Fraction = 1/2 Read Fraction = 3/4 Read Fraction = -4/5 Read Fraction = 6 Read Fraction = 5 Read Fraction = -8 Read Fraction = 1+2/5 Read Fraction = -16+2/3 Read Fraction = 1+1/4 Read Fraction = 2 Read Fraction = -4+1/4 Read Fraction = -10+5/6 ----- Testing relational operators between Fractions Comparing 1/2 to 1/2 Is left < right? false Is left <= right? true Is left > right? false Is left >= right? true Does left == right? true Does left != right ? false Comparing 1/2 to -1/2 Is left < right? false Is left <= right? false Is left > right? true Is left >= right? true Does left == right? false Does left != right ? true Comparing -1/2 to 1/10 Is left < right? true Is left <= right? true Is left > right? false Is left >= right? false Does left == right? false Does left != right ? true Comparing 1/10 to 0 Is left < right? false Is left <= right? false Is left > right? true Is left >= right? true Does left == right? false Does left != right ? true Comparing 0 to 0 Is left < right? false Is left <= right? true Is left > right? false Is left >= right? true Does left == right? true Does left != right ? false ----- Testing relations between Fractions and integers Comparing -1/2 to 2 Is left < right? true Is left <= right? true Is left > right? false Is left >= right? false Does left == right? false Does left != right ? true Comparing -3 to 1/4 Is left < right? true Is left <= right? true Is left > right? false Is left >= right? false Does left == right? false Does left != right ? true ----- Testing binary arithmetic between Fractions 1/6 + 1/3 = 1/2 1/6 - 1/3 = -1/6 1/6 * 1/3 = 1/18 1/6 / 1/3 = 1/2 1/3 + -2/3 = -1/3 1/3 - -2/3 = 1 1/3 * -2/3 = -2/9 1/3 / -2/3 = -1/2 -2/3 + 5 = 4+1/3 -2/3 - 5 = -5+2/3 -2/3 * 5 = -3+1/3 -2/3 / 5 = -2/15 5 + -1+1/3 = 3+2/3 5 - -1+1/3 = 6+1/3 5 * -1+1/3 = -6+2/3 5 / -1+1/3 = -3+3/4 ----- Testing arithmetic between Fractions and integers -1/2 + 4 = 3+1/2 -1/2 - 4 = -4+1/2 -1/2 * 4 = -2 -1/2 / 4 = -1/8 3 + -1/2 = 2+1/2 3 - -1/2 = 3+1/2 3 * -1/2 = -1+1/2 3 / -1/2 = -6 ----- Testing shorthand arithmetic assignment on Fractions 1/6 += 4 = 4+1/6 4+1/6 -= 4 = 1/6 1/6 *= 4 = 2/3 2/3 /= 4 = 1/6 4 += -1/2 = 3+1/2 3+1/2 -= -1/2 = 4 4 *= -1/2 = -2 -2 /= -1/2 = 4 -1/2 += 5 = 4+1/2 4+1/2 -= 5 = -1/2 -1/2 *= 5 = -2+1/2 -2+1/2 /= 5 = -1/2 ----- Testing shorthand arithmetic assignment using integers -1/3 += 3 = 2+2/3 2+2/3 -= 3 = -1/3 -1/3 *= 3 = -1 -1 /= 3 = -1/3 ----- Testing increment/decrement prefix and postfix Now g = -1/3 g++ = -1/3 Now g = 2/3 ++g = 1+2/3 Now g = 1+2/3 g-- = 1+2/3 Now g = 2/3 --g = -1/3 Now g = -1/3

THANKS IN ADVANCE!

Add a private “simplify)” function to your class and call it from the appropriate member functions. (If you write your code the way that 90% of students write it, there will be 6 places where you need to call it. But if you call it a different number of times and your class works, that’s also fine.) The best way to do this is to make the function a void function with no parameters that reduces the calling object Recall that “simplifying” or “reducing” a fraction is a separate task from converting it from an improper fraction to a mixed number. Make sure you keep those two tasks separate in your mind For now (until you get down to the part of the assignment where you improve your insertion operator) your fractions will still be printed as improper fractions, not mixed numbers. In other words, 19/3 will still be 19/3, not 6+1/3. Make sure that your class will reduce ANY fraction, not just the fractions that are tested in the provided client program. Fractions should not be simply reduced upon output, they should be stored in reduced form at all times. In ather words, you should ensure that all fraction objects are reduced before the end of any member function. To put it yet another way: each member function must be able to assume that all fraction objects are in simple form when it begins execution. You must create your own algorithm for reducing fractions. Don’t look up an already existing algorithm for reducing fractions or finding GCF. The point here is to have you practice solving the problem on your own. In particular, don’t use Euclid’s algorithm. Don’t worry about being efficient. It’s fine to have your function check every possible facto, even if it would be more efficient to just check prime numbers, Just create something of your own that works correctly on ANY fraction. Your simplify() function should also ensure that the denominator is never negative. If the denominator is negative, fix this by multiplying numerator and denominator by -1 Better Insertion Operator [10 points] Now modify your overloaded