I need to implement a code to simplify my fractions. I\'m not sure where to add
ID: 3771292 • Letter: I
Question
I need to implement a code to simplify my fractions. I'm not sure where to add it in... Here is what I have.
#ifndef MYFRACTION_H_INCLUDED
#define MYFRACTION_H_INCLUDED
#include <iostream>
#include <cstdlib>
using namespace std;
class fractionType
{
friend ostream& operator << (ostream&, const fractionType&);
friend istream& operator >> (istream&, fractionType&);
public:
const fractionType& operator = (const fractionType&);
fractionType();
fractionType(const fractionType&);
fractionType(const int&, const int&);
~fractionType();
bool operator == (const fractionType&) const;
bool operator != (const fractionType&) const;
bool operator <= (const fractionType&) const;
bool operator < (const fractionType&) const;
bool operator >= (const fractionType&) const;
bool operator > (const fractionType&) const;
fractionType operator + (const fractionType&);
fractionType operator - (const fractionType&);
fractionType operator * (const fractionType&);
fractionType operator / (const fractionType&);
private:
int a;
int b;
};
#endif
#include <iostream>
#include "myFraction.h"
using namespace std;
fractionType :: fractionType(const int& num, const int& den)
{
a = num;
if(den == 0)
{
cout << "Invalid denominator.";
}
}
fractionType :: fractionType()
{
a = 0;
b = 1;
}
fractionType :: fractionType(const fractionType& rightFraction)
{
a = rightFraction.a;
b = rightFraction.b;
}
fractionType :: ~fractionType()
{
}
bool fractionType :: operator == (const fractionType& rightFraction) const
{
return ((a == rightFraction.a) && (b == rightFraction.b));
}
bool fractionType :: operator != (const fractionType& rightFraction) const
{
return ((a != rightFraction.a) || (b != rightFraction.b));
}
bool fractionType :: operator < (const fractionType& rightFraction) const
{
return (a * rightFraction.b < b * rightFraction.a);
}
bool fractionType :: operator <= (const fractionType& rightFraction) const
{
return (a * rightFraction.b <= b * rightFraction.a);
}
bool fractionType :: operator > (const fractionType& rightFraction) const
{
return (a * rightFraction.b > b * rightFraction.a);
}
bool fractionType :: operator >= (const fractionType& rightFraction) const
{
return (a * rightFraction.b >= b * rightFraction.a);
}
fractionType fractionType :: operator + (const fractionType& rightFraction)
{
fractionType temp;
temp.a = (a * rightFraction.b) + (b * rightFraction.a), (rightFraction.b / rightFraction.a);
temp.b = b * rightFraction.b;
return temp;
}
fractionType fractionType :: operator - (const fractionType& rightFraction)
{
fractionType temp;
temp.a = (a * rightFraction.b) - (b * rightFraction.a);
temp.b = b * rightFraction.b;
return temp;
}
fractionType fractionType :: operator * (const fractionType& rightFraction)
{
fractionType temp;
temp.a = a * rightFraction.a;
temp.b = b * rightFraction.b;
return temp;
}
fractionType fractionType :: operator / (const fractionType& rightFraction)
{
fractionType temp;
if((rightFraction.a == 0) || (rightFraction.b == 0))
{
temp.a = 0;
temp.b = 1;
cout << "Cannot perform division.";
}
else
{
temp.a = a * rightFraction.b;
temp.b = b * rightFraction.a;
}
return temp;
}
ostream& operator << (ostream& osObject, const fractionType& myFraction)
{
osObject << myFraction.a << "/" << myFraction.b;
return osObject;
}
istream& operator >> (istream& isObject, fractionType& myFraction)
{
char ch;
isObject >> myFraction.a >> ch >> myFraction.b;
return isObject;
}
#include <iostream>
#include "myFraction.h"
using namespace std;
int main()
{
fractionType myFraction1;
fractionType myFraction2;
cout << "Enter fraction 1 (a/b): ";
cin >> myFraction1;
cout << endl;
cout << "Enter fraction 2 (a/b): ";
cin >> myFraction2;
cout << endl;
cout << myFraction1 << " + " << myFraction2 << " = " << myFraction1 + myFraction2;
cout << endl;
cout << myFraction1 << " - " << myFraction2 << " = " << myFraction1 - myFraction2;
cout << endl;
cout << myFraction1 << " * " << myFraction2 << " = " << myFraction1 * myFraction2;
cout << endl;
cout << myFraction1 << " / " << myFraction2 << " = " << myFraction1 / myFraction2;
cout << endl << endl;
cout << myFraction1 << " == " << myFraction2;
if(myFraction1 == myFraction2)
cout << " true.";
else
cout << " false.";
cout << endl;
cout << myFraction1 << " != " << myFraction2;
if(myFraction1 != myFraction2)
cout << " true";
else
cout << " false";
cout << endl;
cout << myFraction1 << " < " << myFraction2;
if(myFraction1 < myFraction2)
cout << " true.";
else
cout << " false.";
cout << endl;
cout << myFraction1 << " <= " <<myFraction2;
if(myFraction1 <= myFraction1)
cout << " true.";
else
cout << " false.";
cout << endl;
cout << myFraction1 << " > " << myFraction2;
if(myFraction1 > myFraction2)
cout << " true.";
else
cout << " false.";
cout << endl;
cout << myFraction1 << " >= " << myFraction2;
if(myFraction1 >= myFraction2)
cout << " true.";
else
cout << " false.";
cout << endl;
cout << endl;
system("PAUSE");
return 0;
}
Explanation / Answer
#ifndef MYFRACTION_H_INCLUDED
#define MYFRACTION_H_INCLUDED
#include <iostream>
#include <cstdlib>
using namespace std;
class fractionType
{
friend ostream& operator << (ostream&, const fractionType&);
friend istream& operator >> (istream&, fractionType&);
public:
const fractionType& operator = (const fractionType&);
fractionType();
fractionType(const fractionType&);
fractionType(const int&, const int&);
~fractionType();
bool operator == (const fractionType&) const;
bool operator != (const fractionType&) const;
bool operator <= (const fractionType&) const;
bool operator < (const fractionType&) const;
bool operator >= (const fractionType&) const;
bool operator > (const fractionType&) const;
fractionType operator + (const fractionType&);
fractionType operator - (const fractionType&);
fractionType operator * (const fractionType&);
fractionType operator / (const fractionType&);
void simplify();
private:
int a;
int b;
};
#endif
#include <iostream>
#include "myFraction.h"
using namespace std;
fractionType :: fractionType(const int& num, const int& den)
{
a = num;
if(den == 0)
{
cout << "Invalid denominator.";
}
}
fractionType :: fractionType()
{
a = 0;
b = 1;
}
fractionType :: fractionType(const fractionType& rightFraction)
{
a = rightFraction.a;
b = rightFraction.b;
}
fractionType :: ~fractionType()
{
}
bool fractionType :: operator == (const fractionType& rightFraction) const
{
return ((a == rightFraction.a) && (b == rightFraction.b));
}
bool fractionType :: operator != (const fractionType& rightFraction) const
{
return ((a != rightFraction.a) || (b != rightFraction.b));
}
bool fractionType :: operator < (const fractionType& rightFraction) const
{
return (a * rightFraction.b < b * rightFraction.a);
}
bool fractionType :: operator <= (const fractionType& rightFraction) const
{
return (a * rightFraction.b <= b * rightFraction.a);
}
bool fractionType :: operator > (const fractionType& rightFraction) const
{
return (a * rightFraction.b > b * rightFraction.a);
}
bool fractionType :: operator >= (const fractionType& rightFraction) const
{
return (a * rightFraction.b >= b * rightFraction.a);
}
fractionType fractionType :: operator + (const fractionType& rightFraction)
{
fractionType temp;
temp.a = (a * rightFraction.b) + (b * rightFraction.a), (rightFraction.b / rightFraction.a);
temp.b = b * rightFraction.b;
return temp;
}
fractionType fractionType :: operator - (const fractionType& rightFraction)
{
fractionType temp;
temp.a = (a * rightFraction.b) - (b * rightFraction.a);
temp.b = b * rightFraction.b;
return temp;
}
fractionType fractionType :: operator * (const fractionType& rightFraction)
{
fractionType temp;
temp.a = a * rightFraction.a;
temp.b = b * rightFraction.b;
return temp;
}
fractionType fractionType :: operator / (const fractionType& rightFraction)
{
fractionType temp;
if((rightFraction.a == 0) || (rightFraction.b == 0))
{
temp.a = 0;
temp.b = 1;
cout << "Cannot perform division.";
}
else
{
temp.a = a * rightFraction.b;
temp.b = b * rightFraction.a;
}
return temp;
}
void fractionType :: simplify()
{
int i, gcd = 1;
for( i=1 ; i<=a && i<=b ; i++)
if( a%i == 0 && b%i == 0 )
gcd = i;
a = a/gcd;
b = b/gcd;
}
ostream& operator << (ostream& osObject, const fractionType& myFraction)
{
osObject << myFraction.a << "/" << myFraction.b;
return osObject;
}
istream& operator >> (istream& isObject, fractionType& myFraction)
{
char ch;
isObject >> myFraction.a >> ch >> myFraction.b;
return isObject;
}
#include <iostream>
#include "myFraction.h"
using namespace std;
int main()
{
fractionType myFraction1;
fractionType myFraction2;
cout << "Enter fraction 1 (a/b): ";
cin >> myFraction1;
cout << endl;
cout << "Enter fraction 2 (a/b): ";
cin >> myFraction2;
cout << endl;
cout<<"Fraction1 before Simplification: "<<myFraction1<<endl;
cout<<"Fraction2 before Simplification: "<<myFraction2<<endl<<endl;
myFraction1.simplify();
myFraction2.simplify();
cout<<"Fraction1 after Simplification: "<<myFraction1<<endl;
cout<<"Fraction2 after Simplification: "<<myFraction2<<endl<<endl;
cout << myFraction1 << " + " << myFraction2 << " = " << myFraction1 + myFraction2;
cout << endl;
cout << myFraction1 << " - " << myFraction2 << " = " << myFraction1 - myFraction2;
cout << endl;
cout << myFraction1 << " * " << myFraction2 << " = " << myFraction1 * myFraction2;
cout << endl;
cout << myFraction1 << " / " << myFraction2 << " = " << myFraction1 / myFraction2;
cout << endl << endl;
cout << myFraction1 << " == " << myFraction2;
if(myFraction1 == myFraction2)
cout << " true.";
else
cout << " false.";
cout << endl;
cout << myFraction1 << " != " << myFraction2;
if(myFraction1 != myFraction2)
cout << " true";
else
cout << " false";
cout << endl;
cout << myFraction1 << " < " << myFraction2;
if(myFraction1 < myFraction2)
cout << " true.";
else
cout << " false.";
cout << endl;
cout << myFraction1 << " <= " <<myFraction2;
if(myFraction1 <= myFraction1)
cout << " true.";
else
cout << " false.";
cout << endl;
cout << myFraction1 << " > " << myFraction2;
if(myFraction1 > myFraction2)
cout << " true.";
else
cout << " false.";
cout << endl;
cout << myFraction1 << " >= " << myFraction2;
if(myFraction1 >= myFraction2)
cout << " true.";
else
cout << " false.";
cout << endl;
cout << endl;
system("PAUSE");
return 0;
}