Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Given Main Function: No SIM 10:29 PM daveteaches.com 68% Add a private \"simplif

ID: 3693647 • Letter: G

Question

Given Main Function:

No SIM 10:29 PM daveteaches.com 68% 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 functiona 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 other 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 factor, 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

Explanation / Answer

#include <iostream>
#include "fraction.h"
#include <fstream>
#include <cassert>
#include <string.h>

using namespace std;
using namespace cs2b_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 << " ----- Testing basic fraction creation & printing ";
   cout << "(fractions should be in reduced form, and as mixed numbers.) ";

   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 << " ----- Now reading fractions from file ";
   ifstream in("fraction.txt");
   assert(in);
   while (!eof(in)) {
      fraction f;
      if (in.peek() == '#') {
         in.ignore(128, ' ');                       //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 << " ----- Testing relational operators between fractions ";

   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 << " Is left < right? " << boolString(fr[i] < fr[i+1]) << endl;
      cout << " Is left <= right? " << boolString(fr[i] <= fr[i+1]) << endl;
      cout << " Is left > right? " << boolString(fr[i] > fr[i+1]) << endl;
      cout << " Is left >= right? " << boolString(fr[i] >= fr[i+1]) << endl;
      cout << " Does left == right? " << boolString(fr[i] == fr[i+1]) << endl;
      cout << " Does left != right ? " << boolString(fr[i] != fr[i+1]) << endl;
   }

   cout << " ----- Testing relations between fractions and integers ";
   fraction f(-3,6);
   int num = 2;
   cout << "Comparing " << f << " to " << num << endl;
   cout << " Is left < right? " << boolString(f < num) << endl;
   cout << " Is left <= right? " << boolString(f <= num) << endl;
   cout << " Is left > right? " << boolString(f > num) << endl;
   cout << " Is left >= right? " << boolString(f >= num) << endl;
   cout << " Does left == right? " << boolString(f == num) << endl;
   cout << " Does left != right ? " << boolString(f != num) << endl;

   fraction g(1,4);
   num = -3;
   cout << "Comparing " << num << " to " << g << endl;
   cout << " Is left < right? " << boolString(num < g) << endl;
   cout << " Is left <= right? " << boolString(num <= g) << endl;
   cout << " Is left > right? " << boolString(num > g) << endl;
   cout << " Is left >= right? " << boolString(num >= g) << endl;
   cout << " Does left == right? " << boolString(num == g) << endl;
   cout << " Does left != right ? " << boolString(num != g) << endl;
}

void BinaryMathTest()
{
   cout << " ----- Testing binary arithmetic between fractions ";

   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 << " ----- Testing arithmetic between fractions and integers ";
   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 << " ----- Testing shorthand arithmetic assignment on fractions ";

   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 << " ----- Testing shorthand arithmetic assignment using integers ";
   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 << " ----- Testing increment/decrement prefix and postfix ";
   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;
}


fraction.cpp

#include "fraction.h"

namespace cs2b_fraction {

    using namespace std;
    fraction::fraction(int inNumerator, int inDenominator) {
        assert(inDenominator != 0);
        numerator = inNumerator;
        denominator = inDenominator;
        this->simplify();
    }
    ostream& operator<< (ostream& out, const fraction& right) {
        if (abs(right.numerator) >= abs(right.denominator)) {
            out << right.numerator/right.denominator;
            if (right.numerator%right.denominator != 0) {
                out << "+" << abs(right.numerator%right.denominator) << "/" << right.denominator;
            }
        }
        else if (right.numerator == 0) {
            out << 0;
        }
        else {
            out << right.numerator << "/" << right.denominator;
        }
        return out;
    }
    istream& operator>> (istream& in, fraction& right) {
        int temp = 0;
        right.denominator = 1;
        in >> right.numerator;
        if (in.peek() == '+') {
            in.ignore();
            temp = right.numerator;
            in >> right.numerator;
        }
        if (in.peek() == '/') {
            in.ignore();
            in >> right.denominator;
        }
        if (temp < 0) {
            right.numerator *= -1;
        }
        right.numerator += temp*right.denominator;
        right.simplify();
        return in;
    }
    bool operator< (const fraction& left, const fraction& right) {
        return (left.numerator*right.denominator < right.numerator*left.denominator);
    }
    bool operator<= (const fraction& left, const fraction& right) {
        return (left.numerator*right.denominator <= right.numerator*left.denominator);
    }
    bool operator> (const fraction& left, const fraction& right) {
        return (left.numerator*right.denominator > right.numerator*left.denominator);
    }
    bool operator>= (const fraction& left, const fraction& right) {
        return (left.numerator*right.denominator >= right.numerator*left.denominator);
    }
    bool operator== (const fraction& left, const fraction& right) {
        return (left.numerator*right.denominator == right.numerator*left.denominator);
    }
    bool operator!= (const fraction& left, const fraction& right) {
        return (left.numerator*right.denominator != right.numerator*left.denominator);
    }
    fraction operator+ (const fraction& left, const fraction& right) {
        fraction result(left.numerator*right.denominator+
                        right.numerator*left.denominator,
                        left.denominator*right.denominator);
        return result;
    }
    fraction operator- (const fraction& left, const fraction& right) {
        fraction result(left.numerator*right.denominator -
                        right.numerator*left.denominator,
                        left.denominator*right.denominator);
        return result;
    }
    fraction operator* (const fraction& left, const fraction& right) {
        fraction result(left.numerator*right.numerator,
                        left.denominator*right.denominator);
        return result;
    }
    fraction operator/ (const fraction& left, const fraction& right) {
        fraction result(left.numerator*right.denominator,
                        left.denominator*right.numerator);
        return result;
    }
    fraction fraction::operator+=(const fraction &right) {
        *this = *this + right;
        return *this;
    }
    fraction fraction::operator-=(const fraction &right) {
        *this = *this - right;
        return *this;
    }
    fraction fraction::operator*=(const fraction &right) {
        *this = *this * right;
        return *this;
    }
    fraction fraction::operator/=(const fraction &right) {
        *this = *this / right;
        return *this;
    }
    fraction fraction::operator++ () {
        numerator += denominator;
        return *this;
    }
   fraction fraction::operator++ (int) {
        fraction temp(numerator, denominator);
        numerator += denominator;
        return temp;
    }
    fraction fraction::operator-- () {
        numerator -= denominator;
        return *this;
    }
    fraction fraction::operator-- (int) {
        fraction temp(numerator, denominator);
        numerator -= denominator;
        return temp;
    }
    void fraction::simplify() {
        if (denominator <0) { // denominator will be positive
            numerator *= -1;
            denominator *= -1;
        }
        long i = (min(abs(numerator), abs(denominator)));
        if (i != 0) {

            // this construct allows simplify() to work whether i is prime or not
            if (numerator%i==0 && denominator%i ==0) {
                numerator /= i;
                denominator /= i;
            }
            else {
                for (long j = i/2; j>1; j--) {
                    // look for factors of i, starting at i/2
                    if (numerator%j==0 && denominator%j==0) {
                        numerator /= j;
                        denominator /= j;
                        j = (min(numerator, denominator))/2; // recursive idea
                    }
                }
            }
        }
    }
} // end namespace


fraction.h

/*

Public Functions:


fraction(int inNumerator = 0, int inDenominator = 1);
Precondition: inNumerator is the intended numerator of the fraction.
                inDenominator is the intended denominator of the function.
Postcondition: An object of type fraction is instantiated. numerator and
                denominator data members will be instantiated with values
                of 0 and 1, respectively, if respective parameter is empty.
                If numerator and denominator can be reduced to simplified
                values, the new fraction object will be made with those
                values instead. The fraction will always represent the
                denominator as a positive value.

friend std::ostream& operator<<(std::ostream& out, const fraction& right);
Precondition: out is a valid out stream. right is a fraction.
Postcondition: The fraction, right, is sent to the stream in the format
                of ((-)numerator"/"denominator) if the absolute value of
                the numerator is less than the denominator, (1) if numerator
                is equal to denominator, (0) if numerator is 0. If the
                absolute value of numerator is greater than the denominator,
                the format will be an int with the value of
                numerator/denominator, a "+" symbol, followed by numerator
                mod denominator, a "/" symbol, and denominator. Returns out.
                (e.g. -3/4, 1, 0, or 2+1/3).

friend void operator>>(std::istream& in, fraction& right);
Precondition: in is a valid in stream, right is a fraction. Funtion reads
                a valid fraction format, and assigns the read value to right.
                Valid fraction format is considered to be the following:
                a mixed fraction (e.g. 2+1/4 or -3+3/4), a proper or improper
                fraction (e.g. 5/8 or -14/3), a whole number (e.g. 4 or -5),
                or 0.

Postcondition: right numerator and denominator values will be set to the
                values that were read.

friend bool operator< (const fraction& left, const fraction& right);
Precondition: left and right are fraction objects or integers.
Postcondition: Returns a boolean representing the relation between
                fractions left and right. If left's fractional value is less
                than right's fractional value, this function returns true.
                Otherwise, it returns false.

friend bool operator<= (const fraction& left, const fraction& right);
Precondition: left and right are fraction objects or integers.
Postcondition: Returns a boolean representing the relation between
                fractions left and right. If left's fractional value is less
                than or equal to right's fractional value, this function
                returns true. Otherwise, it returns false.

friend bool operator> (const fraction& left, const fraction& right);
Precondition: left and right are fraction objects or integers.
Postcondition: Returns a boolean representing the relation between
                fractions left and right. If left's fractional value is greater
                than right's fractional value, this function
                returns true. Otherwise, it returns false.

friend bool operator>= (const fraction& left, const fraction& right);
Precondition: left and right are fraction objects or integers.
Postcondition: Returns a boolean representing the relation between
                fractions left and right. If left's fractional value is greater
                than or equal to right's fractional value, this function
                returns true. Otherwise, it returns false.

friend bool operator== (const fraction& left, const fraction& right);
Precondition: left and right are fraction objects or integers.
Postcondition: Returns a boolean representing the relation between
                fractions left and right. If left's fractional value is equal
                to right's fractional value, this function returns true.
                Otherwise, it returns false.

friend bool operator!= (const fraction& left, const fraction& right);
Precondition: left and right are fraction objects or integers.
Postcondition: Returns a boolean representing the relation between
                fractions left and right. If left's fractional value is not
                equal to right's fractional value, this function returns true.
                Otherwise, it returns false.

friend fraction operator+ (const fraction& left, const fraction& right);
Precondition: left and right are fraction objects or integers.
Postcondition: Returns a new fraction with numerator and denominator values
                representing the result of the left and right fractional
                summation.

friend fraction operator- (const fraction& left, const fraction& right);
Precondition: left and right are fraction objects or integers.
Postcondition: Returns a new fraction with numerator and denominator values
                representing the result of the left and right fractional
                subtraction.

friend fraction operator* (const fraction& left, const fraction& right);
Precondition: left and right are fraction objects or integers.
Postcondition: Returns a new fraction with numerator and denominator values
                representing the result of the left and right fractional
                multiplication.

friend fraction operator/ (const fraction& left, const fraction& right);
Precondition: left and right are fraction objects or integers.
Postcondition: Returns a new fraction with numerator and denominator values
                representing the result of the left and right fractional
                division.

fraction operator+= (const fraction& right);
Precondition: right is a fraction object or integer.
Postcondition: Calling fraction object uses overloaded operator+ function
                with itself and right.

fraction operator-= (const fraction& right);
Precondition: right is a fraction object or integer.
Postcondition: Calling fraction object uses overloaded operator- function
                with itself and right.

fraction operator*= (const fraction& right);
Precondition: right is a fraction object or integer.
Postcondition: Calling fraction object uses overloaded operator* function
                with itself and right.

fraction operator/= (const fraction& right);
Precondition: right is a fraction object or integer.
Postcondition: Calling object uses overloaded operator/ function with itself
                and right.

fraction operator++ ();
Precondition: Calling object is of type fraction.
Postcondition: Adds 1 to the fractional value of the caller object, then
                returns that fraction object. Prefix increment.
                (e.g. 4/5 becomes 9/5, returns 9/5).

fraction operator++ (int);
Precondition: Calling object is of type fraction.
Postcondition: Makes a copy of the calling fraction, adds 1 to the fractional
                value of the calling fraction, then returns the copy. Postfix
                increment.
                (e.g. 4/5 becomes 9/5, returns 4/5).

fraction operator-- ();
Precondition: Calling object is of type fraction.
Postcondition: Subtracts 1 from the fractional value of the calling function,
                then returns that fraction object. Prefix decrement.
                (e.g. 4/5 becomes -1/5, returns -1/5).

fraction operator-- (int);
Precondition: Calling object is a fraction.
Postcondition: Makes a copy of the calling fraction, subtracts 1 from the
                fractional value of the calling fraction, then returns the
                copy. Postfix decrement.
                (e.g. 4/5 becomes -1/5, returns 4/5).

*/


#ifndef FRACTION_H
#define FRACTION_H

#include <iostream>
#include <cassert>
#include <cmath>

namespace cs2b_fraction {

   class fraction {
   public:
      fraction(int inNumerator = 0, int inDenominator = 1);
      friend std::ostream& operator<<(std::ostream& out, const fraction& right);
      friend std::istream& operator>>(std::istream& in, fraction& right);
      friend bool operator< (const fraction& left, const fraction& right);
      friend bool operator<= (const fraction& left, const fraction& right);
      friend bool operator> (const fraction& left, const fraction& right);
      friend bool operator>= (const fraction& left, const fraction& right);
      friend bool operator== (const fraction& left, const fraction& right);
      friend bool operator!= (const fraction& left, const fraction& right);
      friend fraction operator+ (const fraction& left, const fraction& right);
      friend fraction operator- (const fraction& left, const fraction& right);
      friend fraction operator* (const fraction& left, const fraction& right);
      friend fraction operator/ (const fraction& left, const fraction& right);
      fraction operator+= (const fraction& right);
      fraction operator-= (const fraction& right);
      fraction operator*= (const fraction& right);
      fraction operator/= (const fraction& right);
      fraction operator++ ();
      fraction operator++ (int);
      fraction operator-- ();
      fraction operator-- (int);

   private:
      int numerator;
      int denominator;
      void simplify();
   };

} // namespace end

#endif


sample 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

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote