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
|
#include <iostream>
#include <stdlib.h>
using namespace std;
// Creates the Fraction class.
class Fraction
{
private:
int numerator;
int denominator;
public:
Fraction() : numerator(0), denominator(0) {}
void SetNumerator(int n);
void SetDenominator(int d);
int GetNumerator() const;
int GetDenominator() const;
// Fraction AddFraction(Fraction *, Fraction *);
void print() { cout << numerator << "/" << denominator; };
};
Fraction operator+(const Fraction& a, const Fraction& b);
std::ostream& operator<<(std::ostream& os, const Fraction& f)
{
return os << f.GetNumerator() << '/' << f.GetDenominator();
}
int main()
{
// Creates 3 new fractions.
Fraction frac1, frac2, frac3;
int n, d;
// Prompts the user for data and then assigns it to the correct variables.
// --Fraction 1--
cout << "Enter First Numerator: ";
cin >> n;
cout << "Enter First Denominator: ";
cin >> d;
frac1.SetNumerator(n);
frac1.SetDenominator(d);
// --Fraction 2--
cout << "Enter Second Numerator: ";
cin >> n;
cout << "Enter Second Denominator: ";
cin >> d;
frac2.SetNumerator(n);
frac2.SetDenominator(d);
frac3 = frac1 + frac2;
std::cout << frac1 << " + " << frac2 << " = " << frac3 << '\n';
// frac3.AddFraction(frac1p, frac2p);
// Don't know what this crap is:
//cout << "---------------------------\n";
//object.print();
//cout << " + ";
//object.print();
//cout << " = ";
//frac3.print();
//cout << endl;
return 0;
}
void Fraction::SetNumerator(int n)
{
numerator = n;
}
void Fraction::SetDenominator(int d)
{
denominator = d;
}
int Fraction::GetNumerator() const
{
return numerator;
}
int Fraction::GetDenominator() const
{
return denominator;
}
//Fraction Fraction::AddFraction(Fraction *f1, Fraction *f2)
//{
// Fraction f3;
//
// SetNumerator(&f3, (f1->numerator*f2->denominator +
// f1->denominator*f2->numerator));
// SetDenominator(&f3, f1->denominator*f2->denominator);
//
// return f3;
//}
Fraction operator+(const Fraction& a, const Fraction& b)
{
Fraction result;
result.SetNumerator(a.GetNumerator() * b.GetDenominator() + a.GetDenominator() * b.GetNumerator());
result.SetDenominator(a.GetDenominator() * b.GetDenominator());
return result;
}
|