1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
//Header File <fraction.h>
class Fraction
{
public:
Fraction();
Fraction(int n, int d);
void Write() const;
int GetNumerator() const;
int GetDenominator() const;
int LCM(int a, int b);
int GCD(int a, int b);
void Reduce();
Fraction Add(Fraction otherFraction);
Fraction Multiply(Fraction otherFraction);
Fraction Divide(Fraction otherFraction);
Fraction Reciprocal();
bool IsEqualTo(Fraction otherFraction);
bool IsLessThan(Fraction otherFraction);
bool IsGreaterThan(Fraction otherFraction);
private:
int numerator;
int denominator;
};
//IMPLEMENTION FILE(Fraction.cpp)
//#include "Fraction.h"
#include <iostream>
using namespace std;
Fraction::Fraction()
{
numerator = 1;
denominator = 2;
}
Fraction::Fraction(int n, int d)
{
numerator = n;
if(d == 0)
cout << "Error: Cannot divide by 0!" << endl;
else
denominator = d;
}
int Fraction::GetNumerator() const
{
return numerator;
}
int Fraction::GetDenominator() const
{
return denominator;
}
//This is where I started having some difficulties
void Fraction::Reduce()
{
int denominator, numerator;
int gcd;
gcd = GCD(numerator, denominator);
numerator /= gcd;
denominator /= gcd;
}
int Fraction::GCD(int a, int b)
{
if(b == 0)
return a;
else
return GCD(b, a % b);
}
Fraction::Fraction Add(Fraction otherFraction)
{
int denominator, numerator;
int Fraction LCM;
int lcm = LCM(denominator, otherFraction.denominator);
int num1 = numerator * (lcm / denominator);
int num2 = otherFraction.numerator * (lcm / otherFraction.denominator);
int newNum = num1 + num2;
Fraction newFraction(newNum, lcm);
newFraction.Reduce();
return newFraction;
}
int Fraction::LCM(int a, int b)
{
return(a * b) / GCD(a, b);
}
Fraction Fraction::Reciprocal()
{
int tempNum = denominator;
int tempDen = numerator;
return Fraction(tempNum, tempDen);
}
Fraction::Fraction Multiply(Fraction otherFraction)
{
int numerator;
int denominator;
int n = numerator * otherFraction.numerator;
int d = denominator * otherFraction.denominator;
Fraction newFraction(n, d);
newFraction.Reduce();
return newFraction;
}
Fraction::Fraction Divide(Fraction otherFraction)
{
int div = Reciprocal(otherFraction);
Fraction Multiply(Fraction, div);
return Fraction;
}
bool Fraction::IsEqualTo(Fraction otherFraction)
{
Fraction Fraction;
Fraction.Reduce();
otherFraction.Reduce();
int lcm = LCM(denominator, otherFraction.denominator);
int num1 = numerator * (lcm / denominator);
int num2 = otherFraction.numerator * (lcm / otherFraction.denominator);
if(num1 == num2)
return true;
else
return false;
}
bool Fraction::IsLessThan(Fraction otherFraction)
{
int lcm = LCM(denominator, otherFraction.denominator);
int num1 = numerator * (lcm / denominator);
int num2 = otherFraction.numerator * (lcm / otherFraction.denominator);
if(num1 < num2)
return true;
else
return false;
}
bool Fraction::IsGreaterThan(Fraction otherFraction)
{
int lcm = LCM(denominator, otherFraction.denominator);
int num1 = numerator * (lcm / denominator);
int num2 = otherFraction.numerator * (lcm / otherFraction.denominator);
if(num1 > num2)
return true;
else
return false;
}
|