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

################################################################################

ID: 3543045 • Letter: #

Question

##################################################################################################

Here is what I have done so far:#########################################################################

Fraction.h file is#####################################################################################

##################################################################################################




#ifndef FRACTION_H

#define FRACTION_H

#include <cmath>

#include <string>

#include <sstream>

#include <exception>

using namespace std;

class Fraction

{

private:

int _numerator;

int _denominator;


void simplify()

{

reduce();

checkForNegatives();

}


//reduces the fraction by common devisors

void reduce()

{

for(int i = 2; i <= _denominator; i++)

{

//did we find a common divisor?

if(_numerator % i == 0 && _denominator % i == 0)

{

_numerator /= i;

_denominator /= i;


//recursively call ourselves to check for more devisors

reduce();


//we can stop looping after the recursive call

break;

}

}

}


//if we have a negative fraciton, make sure "-" symbol is attached to the numerator

void checkForNegatives()

{

//#If we have just one negative number, make

//sure that it's on the numerator.  If we have two negative numbers,

//then turn both into positive numbers

if(_denominator < 0)

{

//If we have at least one negative number, the denominator is

//guaranteed to be positive

_denominator = abs(_denominator);


//check for negative numerator

if(_numerator < 0)

{

_numerator = abs(_numerator);

}

else

{

_numerator = -_numerator;

}

}

}


public:

Fraction()

{

_numerator = 0;

_denominator = 1;

}


Fraction(int num, int denom)

{

_numerator = num;

setDenominator(denom);

simplify();

}


int getNumerator() const

{

return _numerator;

}


void setNumerator(const int n)

{

_numerator = n;

}


int getDenominator() const

{

return _denominator;

}


void setDenominator(const int d)

{

//allow if not 0

if(d != 0)

{

_denominator = d;

}

        else

        {

            throw exception("Cannot have negative denominator");

        }

}


//example operator overload

bool operator==(const Fraction &other)

{

if(_numerator == other.getNumerator() && _denominator == other.getDenominator())

{

return true;

}

else

{

return false;

}

}



//TODO: implement + operator


//TODO: implement - operator

//TODO: implement * operator


//TODO: implement / operator


string toString()

{

ostringstream s;

s << _numerator << "+" << _denominator;

return s.str();

}

};


#endif






################################################################################################


and here is main.cpp:###############################################################################


################################################################################################






#include <iostream>

#include <string>

#include <istream>

#include <ostream>

#include <fstream>

#include <iomanip>

#include <cstdio>


using namespace std;

int main()

{

return 0;

}

Explanation / Answer

#ifndef FRACTION_H
#define FRACTION_H
#include <cmath>
#include <string>
#include <sstream>
#include <exception>
using namespace std;
class Fraction
{
private:
int _numerator;
int _denominator;
void simplify()
{
reduce();
checkForNegatives();
}
//reduces the fraction by common devisors
void reduce()
{
for(int i = 2; i <= _denominator; i++)
{
//did we find a common divisor?
if(_numerator % i == 0 && _denominator % i == 0)
{
_numerator /= i;
_denominator /= i;
//recursively call ourselves to check for more devisors
reduce();
//we can stop looping after the recursive call
break;
}
}
}
//if we have a negative fraciton, make sure "-" symbol is attached to the numerator
void checkForNegatives()
{
//#If we have just one negative number, make
//sure that it's on the numerator. If we have two negative numbers,
//then turn both into positive numbers
if(_denominator < 0)
{
//If we have at least one negative number, the denominator is
//guaranteed to be positive
_denominator = abs(_denominator);

//check for negative numerator
if(_numerator < 0)
{
_numerator = abs(_numerator);
}
else
{
_numerator = -_numerator;
}
}
}
public:
Fraction()
{
_numerator = 0;
_denominator = 1;
}

Fraction(int num, int denom)
{
_numerator = num;
setDenominator(denom);
simplify();
}

int getNumerator() const
{
return _numerator;
}

void setNumerator(const int n)
{
_numerator = n;
}

int getDenominator() const
{
return _denominator;
}

void setDenominator(const int d)
{
//allow if not 0
if(d != 0)
{
_denominator = d;
}
else
{
throw exception("Cannot have negative denominator");
}
}

//example operator overload
bool operator==(const Fraction &other)
{
if(_numerator == other.getNumerator() && _denominator == other.getDenominator())
{
return true;
}
else
{
return false;
}
}


//TODO: implement + operator
Fraction operator+(const Fraction& f1)
{
    Fraction temp((_numerator*f1._denominator+_denominator*f1._numerator),_denominator*f1._denominator);
    return temp;
}

//TODO: implement - operator
Fraction operator-(const Fraction& f1)
{
    Fraction temp((_numerator*f1._denominator-_denominator*f1._numerator),_denominator*f1._denominator);
    return temp;
}

//TODO: implement / operator
Fraction operator/(const Fraction& f1)
{
    Fraction temp(_numerator*f1._denominator,_denominator*f1._numerator);
    return temp;
}

//TODO: implement * operator
Fraction operator*(const Fraction& f1)
{
    Fraction temp(_numerator*f1._numerator,_denominator*f1._denominator);
    return temp;
}


string toString()
{
ostringstream s;
s << _numerator << "/" << _denominator;
return s.str();
}
};

#endif

#include <iostream>
#include <string>
#include <istream>
#include <ostream>
#include <fstream>
#include <iomanip>
#include <cstdio>
#include "fraction.h"
using namespace std;
int main()
{
Fraction f1(1,3);
Fraction f2(1,4);
Fraction f3= f1*f2;
cout << f1.toString();
cout << " * ";
cout << f2.toString();
cout << " = ";
cout << f3.toString() << endl;
system("pause");
return 0;
}