As stated in the project syllabus:
A sample run of the program should execute as follows:
Enter the numerator and denominator of f1 > 3 5
Enter the numerator and denominator of f2 > 6 10
Enter the numerator and denominator of f3 > -5 3
Enter the numerator and denominator of f4 > 5 3
3/5 + -5/3 = ....
-5/3(5/3 - 6/10) / (-5/3 - 5/3) = ...
(-5/3)^6 = ...
(3/5 / 6/10) / (-5/3 + 5/3) = ...
1 / (-5/3 x 5/3) = ...
3/5(-5/3 - 5/3) / 6/10 = -10/3 <> 3/5 / (6/10(-5/3 - 5/3)) = -3/10
Run you program for various values of the fractions f1, f2, f3 and f4, including
cases in which there are divide-by-zero errors and cases in which there are
not any. Make sure that all of the arithmetic operators work properly and
that all fractions are displayed in their normalized in-line form
It may be helpful to include this, as instructed in the program's syllabus:
The Fraction class should also provide the following functions:
1. constructors: There will be three versions of the constructor:
(a) Fraction() : the default constructor sets the top to 0 and bottom
to 1; it creates the fraction 0/1.
(B) Fraction(int n) : the second constructor sets top to n and bottom
to 1; it creates the fraction n/1.
© Fraction(int n, int d): the third constructor sets top to n and
bottom to d. A precondition here is that d must not be 0. If d is
0, terminate the program..
2. Define these functions using the headers below and observe the following:
(a) Fraction add(const Fraction& f ) const : The sum must be
normalized. Neither operands should be changed.
(B) Fraction subtract(const Fraction& f ) const : The difference
must be normalized. Neither operands should be changed.
© Fraction multiply(const Fraction& f ) const : The product
must be normalized. Neither operands should be changed.
(d) Fraction divide(const Fraction& f ) const : The quotient
must be normalized. Neither operands should be changed. If f is
equal to the zero fraction, throw a string exception.
(e) bool equal(const Fraction& f ) const : determines whether
two fractions are equal.
I have updated my code to the following, but still receiving errors::
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
|
#include <iostream>
#include <cmath>
#include <iomanip>
#include <fstream>
using namespace std;
class Fraction
{
private:
int numerator;
int denominator;
int gcd(int a, int b);
void reduce();
public:
Fraction() : numerator(0), denominator(1)
{}
Fraction(int n) : numerator(n), denominator(1)
{}
Fraction(int n, int d) : numerator(n), denominator(d)
{}
void sglfrac();
void displaysgl();
string str();
Fraction add(const Fraction& f ) const;
Fraction subtract(const Fraction& f ) const;
Fraction multiply(const Fraction& f ) const;
Fraction divide(const Fraction& f ) const;
bool equal(const Fraction& f );
};
void Fraction::reduce()
{
int a,b,r,gcd;
a = abs(numerator);
b = abs(denominator);
if (b == 0)
{
cout << "Illegal fraction: division by 0";
exit(1);
}
else if (a == 0)
{
numerator = 0;
denominator = 1;
return;
}
while (a != 0)
if (a < b)
{
r = a; a = b; b = r;
a = a - b;
}
gcd = b;
numerator = numerator / gcd;
denominator = denominator / gcd;
}
void Fraction::sglfrac()
{
cout << "numerator: ";
cin >> numerator;
cout << "denominator: ";
cin >> denominator;
}
void Fraction::displaysgl()
{
cout << numerator << " / " << denominator;
}
Fraction add(const Fraction& f1, const Fraction& f2 )
{
Fraction result;
numerator = (f1.numerator * f2.denominator) + (f1.denominator * f2.numerator);
denominator = (f1.denominator * f2.denominator);
numerator.reduce();
result.reduce();
return result;
}
Fraction subtract(const Fraction& f1, const Fraction& f2 )
{
numerator = (f1.numerator * f2.denominator) - (f1.denominator * f2.numerator);
denominator = (f1.denominator * f2.denominator);
}
Fraction multiply(const Fraction& f1, const Fraction& f2 )
{
numerator = f1.numerator * f2.numerator;
denominator = f1.denominator * f2.denominator;
}
Fraction divide(const Fraction& f1, const Fraction& f2 )
{
numerator = (f1.numerator * f2.denominator);
denominator = (f1.denominator * f2.numerator);
}
int main()
{
int a,b;
Fraction f1(a,b);
Fraction f2(a,b);
Fraction f3(a,b);
Fraction f4(a,b);
cout << "Enter the numerator and denominator of f1 >";
f1.sglfrac();
cout << "Enter the numerator and denominator of f2 >";
f2.sglfrac();
cout << "Enter the numerator and denominator of f3 >";
f3.sglfrac();
cout << "Enter the numerator and denominator of f4 >";
f4.sglfrac();
}
|