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
|
// In main
const Fraction f3(12, 8);
const Fraction f4(202, 303);
result = f3.multipliedBy(f4);
cout << "The product of ";
f3.print();
cout << " and ";
f4.print();
cout << " is ";
result.print();
cout << endl;
}
Fraction Fraction::multipliedBy(const Fraction &otherFraction)const
{
Fraction answer;
answer.numerator = (numerator * otherFraction.numerator);
answer.denominator = (denominator * otherFraction.denominator);
answer.simplify();
return answer;
}
void Fraction::simplify()
{
int reducedNumerator = numerator;
int reducedDenominator = denominator;
const int prime_SIZE = 26;
int prime[prime_SIZE] = { 2,3,5,7,11,13,17,19,23,29,31,37,41,43,
47,53,59,61,67,71,73,79,83,89,97,101 };
for (int i = 0; i < prime_SIZE; i++)
{
while (reducedNumerator % prime[i] == 0 && reducedDenominator % prime[i] == 0) {
reducedNumerator = reducedNumerator / prime[i];
reducedDenominator = reducedDenominator / prime[i];
}
}
numerator = numerator / reducedNumerator;
denominator = denominator / reducedDenominator;
if (numerator == denominator) {
numerator = reducedNumerator;
denominator = reducedDenominator;
}
}
void Fraction::print()const
{ // May need function call here. Not sure correct syntax.
//simplify();
cout << numerator << "/" << denominator;
}
|