/*************************************************/
// R. A. Hillyard
// rational05.cpp
// November 2001
//
// class with constructors, friend functions and
// more operator overloading
// overload insertion and extraction operator
/*************************************************/
#include<iostream>
using namespace std;
class Rational
{
public :
friend istream& operator >>(istream& ins, Rational& f1);
friend ostream& operator <<(ostream& output, const Rational& f1);
friend bool operator ==(const Rational& f1, const Rational& f2);
friend bool operator !=(const Rational& f1, const Rational& f2);
friend Rational operator -(const Rational& f1, const Rational& f2);
friend int gcd(int x, int y);
//constructors
Rational();
Rational(int top);
Rational(int top, int bottom);
//accessor functions
void output() const;
int getTop() const;
int getBottom() const;
//mutator functions
void setTop(int t);
void setBottom(int b);
private:
int top;
int bottom;
};
/********************************************************/
ostream& operator <<(ostream& output, const Rational& f1)
{
output << f1.top << "/" << f1.bottom;
return output;
}
/********************************************************/
istream& operator >>(istream& ins, Rational& f1)
{
cout << "Enter top and bottom > ";
ins >> f1.top >> f1.bottom;
return ins;
}
/********************************************************/
bool operator ==(const Rational& f1, const Rational& f2)
{ return((f1.top * f2.bottom) == (f1.bottom * f2.top )); }
/********************************************************/
bool operator !=(const Rational& f1, const Rational& f2)
{ return !(f1==f2); }
/********************************************************/
Rational operator -(const Rational& f1, const Rational& f2)
{
int factor;
int t = f1.top * f2.bottom - f2.top * f1.bottom;
int b = f1.bottom * f2.bottom;
factor = gcd(t,b);
Rational temp(t/factor,b/factor);
return temp;
}
/********************************************************/
int gcd(int x, int y)
{
int c; //store remainder
while( (c = x%y) != 0 )
{
x = y;
y = c;
}//end while
return y;
}//end of gcd
/********************************************************/
Rational::Rational()
{
top = 0;
bottom = 1;
}
/********************************************************/
Rational::Rational(int t)
{
top = t;
bottom = 1;
}
/********************************************************/
Rational::Rational(int t, int b)
{
top = t;
bottom = b;
}
/********************************************************/
int Rational::getTop() const
{ return top; }
/********************************************************/
int Rational::getBottom() const
{ return bottom; }
/********************************************************/
void Rational::output() const
{ cout << top << "/" << bottom; }
/********************************************************/
void Rational::setTop(int t)
{ top = t; }
/********************************************************/
void Rational::setBottom(int b)
{ bottom = b; }
/********************************************************/
/********************************************************/
int main( void )
{
char choice;
Rational f1;
Rational f2;
Rational f3;
Rational sentinal(-99, -99);
while(f1 != sentinal)
{
cout << "S to subtract fractions\n";
cout << "C to compare fractions\n";
cout << "Any other char to quit\n";
cin >> choice;
if(choice == 'S')
{
cout << "Enter fractions to subtract:\n";
cin >> f1 >> f2;
cout << f1 << " - " << f2 << " = " << f1-f2 << endl;
}
else if(choice == 'C')
{
cout << "Enter fractions to compare:\n";
cin >> f1 >> f2;
if(f1 == f2)
cout << f1 << " and " << f2 << " are equal\n";
else
cout << f1 << " and " << f2 << " are not equal\n";
}
else
f1 = Rational(-99, -99);
}
return 0;
}
/********************************************************/
/*******************Program Output**********************
S to subtract fractions
C to compare fractions
Any other char to quit
S
Enter fractions to subtract:
Enter top and bottom > 5 8
Enter top and bottom > 5 16
5/8 - 5/16 = 5/16
S to subtract fractions
C to compare fractions
Any other char to quit
S
Enter fractions to subtract:
Enter top and bottom > 5 16
Enter top and bottom > 5 8
5/16 - 5/8 = -5/16
S to subtract fractions
C to compare fractions
Any other char to quit
C
Enter fractions to compare:
Enter top and bottom > 5 13
Enter top and bottom > 3 8
5/13 and 3/8 are not equal
S to subtract fractions
C to compare fractions
Any other char to quit
C
Enter fractions to compare:
Enter top and bottom > 1 2
Enter top and bottom > 50 100
1/2 and 50/100 are equal
S to subtract fractions
C to compare fractions
Any other char to quit
g
*/