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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
#pragma once
#include<iostream>
using namespace std;
//Function prototypes for overloaded stream operators
class Fraction
{
private:
int num;
int den;
public:
//Default Constructor creates space for num and den, then initializes
//its default value to
//num=0, den=1
Fraction();
//constructors sets num
Fraction(int n);
Fraction(int n, int d);
void simplify();
//Overloaded Opertators
Fraction& operator +=(const Fraction &rhs);
Fraction& operator *=(const Fraction &rhs);
Fraction operator -();
Fraction& operator /=(const Fraction &rhs);
//Helper Functions
friend ostream& operator << (ostream &out, const Fraction &obj)
{
out << obj.num << "/" << obj.den;
return out;
}
friend istream& operator >> (istream &strm, Fraction &obj)
{
strm >> obj.num >> obj.den;
obj.simplify();
return strm;
}
};
Fraction::Fraction()
{
num = 0;
den = 1;
}
//simplify function will allow the constructor to quickly deligate thisat the commence of the
//the code
Fraction::Fraction(int n)
{
num = n;
den = 1;
}
Fraction:: Fraction(int n, int d)
{
num = n;
den = d;
simplify();
}
//non member funtion to add new fraction object and return its value
//a copy of the left hand side is made into a fraction,
//modifies the left hand side.
//right handside is not modified.
//a copy of the value is return...all with the help of math already performed in your
//public member function plusequals.
Fraction operator+ (Fraction lhs, const Fraction &rhs)
{
return lhs += rhs;
}
//negate the right hand side
//modify the numerator then add both values by calling a member function
Fraction Fraction::operator -()
{
return(-num, den);
}
Fraction operator *(Fraction lhs, const Fraction& rhs)
{
return lhs *= rhs;
}
Fraction operator /(Fraction lhs, const Fraction& rhs)
{
return lhs /= rhs;
}
Fraction& Fraction::operator /=(const Fraction &rhs)
{
// a/b (/) c/d = a*d/b*c
int a = num;
int b = den;
int c = rhs.num;
int d = rhs.den;
num =(a * d);
den = (b * c);
simplify();
return (*this);
}
Fraction& Fraction::operator *=(const Fraction &rhs)
{
// a/b *c/d = a*c/b*d
int a = num;
int b = den;
int c = rhs.num;
int d = rhs.den;
num = (a * c);
den = (b * d);
simplify();
return (*this);
}
Fraction& Fraction ::operator+=(const Fraction& rhs)
{
// a/b + c/d =(a*d +b*c)/(b*d)
int a = num;
int b = den;
int c = rhs.num;
int d = rhs.den;
num = (a * d + b*c);
den = (b * d);
simplify();
return (*this);
}
int GCD(int a, int b)
{
if (b == 0)
return a;
return GCD(b, a%b);
}
void Fraction::simplify()
{
/*for (int i = num * den; i > 1; i--)
{
if ((num%i) == 0 && (den% i) == 0)
{
num = num / i;
den = den / i;
}
}*/
int division_Factor = GCD(num,den);
num = num/ division_Factor;
den = den/ division_Factor;
}
int main()
{
Fraction f1(1, 2), f2;
cout << "My Fraction is: " << f1 << endl;
cout << "Enter a fraction: ";
cin >> f2;
cout << "Addition: " << (f1 += f2 )<< endl;
//cout << "Subtraction: " << f1 - f2 << endl;
//cout << "Multiplication: " << (f1 *= f2 )<< endl;
//cout << "Division: " << (f1 /= f2) << endl;
system("Pause");
return 0;
}
|