/*************************************************/
// R. A. Hillyard
// rational03.cpp
// November 2001
//
// class with constructors
// and friend functions
/*************************************************/

#include <iostream>
#include<string>

using namespace std;    //introduces namespace std

class Rational
  {
  public :
    friend bool equal(Rational f1, Rational f2);
    friend Rational subtract(Rational f1, Rational f2) ;  
    friend int gcd(int x, int y); 
      
    //constructors
    Rational();
    Rational(int top);
    Rational(int top, int bottom);
    //acessor functions
    void output();
    int getTop() ;
    int getBottom() ;
    //mutator functions
    void setTop(int t);
    void setBottom(int b);
    
  private:
    int top;
    int bottom;  
  };
/********************************************************/
bool equal(Rational f1, Rational f2)
  {
  //reduce fractions before comparision
  int factor1 = gcd(f1.top, f1.bottom);
  int factor2 = gcd(f2.top, f2.bottom);

  Rational t1(f1.top/factor1,f1.bottom/factor1);
  Rational t2(f2.top/factor2,f2.bottom/factor2);
  if( (t1.top == t2.top) && (t1.bottom == t2.bottom )) 
    return true;
  return false;
  }
/********************************************************/
Rational subtract(Rational f1, Rational f2)
  {
  int factor;
  //new top and bottom
  int t1 = (f1.top * f2.bottom) - (f2.top * f1.bottom);
  int b1 = (f1.bottom * f2.bottom);

  factor = gcd(t1,b1);                 //reduce fraction
  Rational temp(t1/factor,b1/factor);  //construct new object
  return temp;                         //return it
  }  
/********************************************************/
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() 
  {  return top;  }
/********************************************************/
int Rational::getBottom() 
  {  return bottom;  }
/********************************************************/
void Rational::output()
  {  cout << top << "/" << bottom;  }
/********************************************************/
void setTop(int t)
  {  top = t;  }
/********************************************************/
void setBottom(int b)
  {  bottom = b;  }
/********************************************************/

/********************************************************/
int main( void )
  {
  Rational f1(2,6);
  Rational f2(1,3);
  Rational f3;
  Rational f4;
  f3 = Rational(7,9);

  cout << "To start your fractions are: ";

  f1.output();  cout << " ";
  f2.output();  cout << " ";
  f3.output();  cout << endl;

  f1 = Rational(3,6);
  f2 = Rational(50,100);
  f4 = subtract(f1,f3);
  cout << "After your fractions are: ";

  f1.output();  cout << " ";
  f2.output();  cout << " ";
  f3.output();  cout << " ";
  f4.output();  cout << endl;
  
  if(equal(f1,f2))
    cout << "f1 and f2 are equal\n";
  else
    cout << "f1 and f2 are not equal\n";

  return 0;
  }
/********************Program Output*********************
To start your fractions are: 2/6 1/3 7/9
After your fractions are: 3/6 50/100 7/9 -5/18
f1 and f2 are equal
*/