Im having a difficult time wondering why im having problems with my insertion (<<) and extraction (>>) operators. If anyone can help me out with this minor problem...it would be grateful. Thanks!
Code:
#include <math.h>
#include <iostream.h>
//using namespace std;
class Fraction
{
private:
int denom;
int numer;
public:
Fraction()
{
numer = 0;
denom = 1;
};
Fraction(int n, int d)
{
numer = n;
denom = d;
};
Fraction operator + (Fraction f2)
{
Fraction temp;
temp.numer = numer * f2.denom + denom * f2.numer;
temp.denom = denom * f2.denom;
return temp;
};
Fraction operator - (Fraction f2)
{
Fraction temp;
temp.numer = numer * f2.denom - denom * f2.numer;
temp.denom = denom * f2.denom;
return temp;
};
Fraction operator * (Fraction f2)
{
Fraction temp;
temp.numer = numer * f2.numer;
temp.denom = denom * f2.denom;
return temp;
};
Fraction operator / (Fraction f2)
{
Fraction temp;
temp.numer = numer * f2.denom;
temp.denom = denom * f2.numer;
return temp;
};
bool operator > (Fraction f1)
{
float temp = f1.numer / f1.denom;
float temp1 = numer / denom;
return (temp > temp1) ? true:false;
};
bool operator < (Fraction f1)
{
float temp = numer / denom;
float temp1 = f1.numer / f1.denom;
return (temp < temp1) ? true:false;
};
bool operator == (Fraction f1)
{
if ((numer == f1.numer) && (denom == f1.denom))
return 1;
else
return 0;
}
bool operator != (Fraction f1)
{
if ((numer != f1.numer) && (denom != f1.denom))
return 1;
else
return 0;
}
void operator += (Fraction f1)
{
numer = numer + f1.numer;
denom = denom + f1.denom;
}
void operator -= (Fraction f1)
{
numer = numer - f1.numer;
denom = denom - f1.denom;
}
void operator *= (Fraction f1)
{
numer = numer * f1.numer;
denom = denom * f1.denom;
}
void operator /= (Fraction f1)
{
numer = numer / f1.numer;
denom = denom / f1.denom;
}
void lowTerms()
{
long rNumer, rDenom, temp, gcd;
rNumer = labs(numer);
rDenom = labs(denom);
if (rDenom == 0)
{
cout<<"Illegal Fraction: division by 0"<<endl;
return;
}
else if (rNumer == 0)
{
numer = 0;
denom = 1;
return;
}
while (rNumer != 0)
{
if (rNumer < rDenom)
{
temp = rNumer;
rNumer = rDenom;
rDenom = temp;
}
rNumer -= rDenom;
}
gcd = rDenom;
numer = numer / gcd;
denom = denom / gcd;
}
};
friend istream& operator >> (istream& in, Fraction f1)
{
char slash;
in>>numer;
cin>>slash;
in>>denom;
return;
}
friend ostream& operator << (ostream& out, Fraction f1)
{
out<<numer;
cout<<"/";
out<<denom;
return out;
}
void main()
{
Fraction f1, f2, f3, f4;
char slash = '/';
char op;
char valid_operator= 'y';
char another_try='y';
cout << "\n-------- The four-function calculator ---------" << endl;
do {
cout << "\nEnter a fraction, an operator (+, -, *, /), and another fraction: "
<< "\n (for example: 1/2 * 3/4) ";
cin >> f1 >> op >> f2;
switch (op)
{
case '+':
f3 = f1 + f2;
f4 += f2;
break;
case '-':
f3 = f1 - f2;
f4 -= f2;
break;
case '*':
f3 = f1 * f2;
f4 *= f2;
break;
case '/':
f3 = f1 / f2;
f4 /= f2;
break;
default:
valid_operator = 'n';
break;
}
if (valid_operator == 'y')
{
cout << " The result is "<<f3;
if ( f1 < f2 )
{
cout<<f1;
cout << " is less than ";
cout<<f2;
cout << endl;
}
if ( f1 > f2 )
{
cout<<f1;
cout<<" is greater than ";
cout<<f2;
cout<<endl;
}
if ( f1 == f2 )
{
cout<<f1;
cout << " is equal to ";
cout<<f2;
cout << endl;
}
if ( f1 != f2 )
{
cout<<f1;
cout << " is not equal to ";
cout<<f2;
cout << endl;
}
}
if (valid_operator == 'y')
{
cout<< " The result is ";
f3.lowTerms();
cout<<f3;
cout<< "\n The assignment operator result is: ";
cout<<f4;
}
else
{
cout << " The operator must be +, -, *, or /. Try again! " << endl;
valid_operator = 'y';
}
cout << " Do another calculation? ";
cin >> another_try;
} while (another_try == 'y');
}