// File: ex8-7.cpp
#include <iostream>
#include <cmath>
using namespace std;
class Fraction
{
private:
int numer;
int denom;
public:
Fraction(int n = 0, int d = 1) : numer(n), denom(d) {}
int get_numer() const
{
return numer;
}
int get_denom() const
{
return denom;
}
void reduce();
friend istream& operator>>(istream& s, Fraction& f);
};
void Fraction::reduce()
{
int min;
// find the minimum of the denom and numer
min = abs(denom) < abs(numer) ? abs(denom) : abs(numer);
for (int i = 2; i <= min; i++)
{
while ((abs(numer) % i == 0) && (abs(denom) % i == 0))
{
numer /= i;
denom /= i;
}
}
}
ostream& operator<<(ostream& s, Fraction f)
{
f.reduce();
s << f.get_numer() << '/' << f.get_denom();
return s;
}
istream& operator>>(istream& s, Fraction& f)
{
s >> f.numer >> f.denom;
return s;
}
int main ()
{
Fraction f(3,4);
cout << f << endl;
Fraction g(2,4);
cout << g << endl;
cout << "Enter a Fraction: numerator denominator => ";
Fraction h;
cin >> h;
cout << h << endl;
}