rat001
Home ] Up ]

 

//CSIS 250
//Assignment #3 Solution
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
const int MIN_VALUE = 1;
const int MAX_VALUE = 20;
const int TEST_COUNT = 5;
////////////////////////////////////////////////////////////////////////////////
// This program will be able to simplify, add, subtract, divide,              //
// multiply, display, and display in a real number a fraction or fractions    //
////////////////////////////////////////////////////////////////////////////////
class CRational
  {
  private:
    int den;
    int num;
  public:
    CRational (int den=1, int num=1);   //  constructor
    CRational (char ch);                //  constructor
    void simplify(void);                //  member function to simplify
    CRational operator +(CRational rat);//  member function to add
    CRational operator -(CRational rat);//  member function to subtract
    CRational operator *(CRational rat);//  member function to multiply
    CRational operator /(CRational rat);//  member function to divide
    void display(void);                 //  member function to display
    void displayValue(void);            //  member function to display float
    bool operator == (const CRational &rat) const;
    bool operator != (const CRational &rat) const;
    bool operator >= (const CRational &rat) const;
    bool operator <= (const CRational &rat) const;
    bool operator > (const CRational &rat) const;
    bool operator < (const CRational &rat) const;
    
    friend ostream &operator << (ostream &out, const CRational &rat);
    friend istream &operator >> (istream &inp, CRational &rat);
  };
int gcd(int x, int y);                  //  global function for gcdt num;
////////////////////////////////////////////////////////////////////////
// This constructor sets the values and also simplifies the fraction. //
////////////////////////////////////////////////////////////////////////
CRational::CRational (int den, int num)
  {
  this->num = num;
  if (0 != den)
      this->den = den;
    else
      this->den = 1;
  simplify();
  }
////////////////////////////////////////////////////////////////////////
// This function tests the default constructor the class              //
////////////////////////////////////////////////////////////////////////
void TestConstructor(void)
  {
	cout << "TestConstructor" << endl;
	cout << "---------------" << endl;
  for (int i=0; i<TEST_COUNT; i++)
    {
		cout << "Test #" << i+1 << endl;
    int d = rand()%MAX_VALUE + 1;
    int n = rand()%MAX_VALUE + 1;
    if (rand()%2 == 1)
        n = -n;
    CRational fraction(d, n);
    fraction.display();
    cout << endl;
    }
  }
////////////////////////////////////////////////////////////////////////
// This constructor sets the values randomly                          //
// and also simplifies the fraction.                                  //
////////////////////////////////////////////////////////////////////////
CRational::CRational(char ch)
  {
  if (('r' == ch) || ('R' == ch)) //random
      {
      num = rand()%MAX_VALUE + 1;
      den = rand()%MAX_VALUE + 1;
      if (rand()%2 == 1)
          num = -num;
      }
    else if (('g' == ch) || ('G' == ch)) //unsimplified
      {
      num = rand()%MAX_VALUE + 1;
      den = rand()%MAX_VALUE + 1;
      if (rand()%2 == 1)
          num = -num;
      }
    else if (('u' == ch) || ('U' == ch)) //unit
      {
      num = 1;
      den = 1;
      }
    else if (('z' == ch) || ('Z' == ch)) //zero
      {
      num = 0;
      den = 1;
      if (rand()%2 == 1)
          {
          num = -num;
          }
      }
    else if (('i' == ch) || ('I' == ch)) //input
      {
      cout << "Enter num ";
      cin >> num;
      cout << "Enter den   ";
      cin >> den;
      }
    else
      {
      num = 1;
      den = 1;
      }
  if (ch != 'g' && ch != 'G')
      simplify();
  }
////////////////////////////////////////////////////////////////////////
// This function test the random constructor                          //
////////////////////////////////////////////////////////////////////////
void TestConstructor2(void)
  {
	cout << "\nTestConstructor2" << endl;
	cout << "----------------" << endl;
  for (int i=0; i<TEST_COUNT; i++)
      {
      char ch;
 			cout << "Test #" << i+1 << endl;
      switch (rand()%8)
        {
        case 0: ch = 'r'; break;
        case 1: ch = 'R'; break;
        case 2: ch = 'u'; break;
        case 3: ch = 'U'; break;
        case 4: ch = 'z'; break;
        case 5: ch = 'Z'; break;
        case 6: ch = 'g'; break;
        case 7: ch = 'G'; break;
        case 8: ch = 'i'; break;
        case 9: ch = 'I'; break;
        default:
          ch = 'k';
        }
			cout << ch << endl;
      CRational rat(ch);
      rat.display();
      cout << endl;
      } 
  }
////////////////////////////////////////////////////////////////////////
// This function simplifies the fraction by finding the               //
// greatest common fractor and dividing both the den and num by it    //
////////////////////////////////////////////////////////////////////////
void CRational::simplify(void)
  {
  if (den < 0)
      {
      den = -den;
      num = -num;
      }
  if (this->num != 0 && this->den != 0)
		  {
		  int gcfactor = gcd(abs(this->num), this->den);
		  this->num = this->num / gcfactor;
		  this->den = this->den / gcfactor;
		  }
	}
////////////////////////////////////////////////////////////////////////
// This functions tests the simplify function                         //
////////////////////////////////////////////////////////////////////////
void TestSimplify(void)
  {
	cout << "\nTestSimplify" << endl;
	cout << "------------" << endl;
    for (int i=0; i<TEST_COUNT; i++)
      {
 			cout << "Test #" << i+1 << endl;
      CRational r1('g');
      r1.display();
      r1.simplify();
      r1.display();
      }
  }
////////////////////////////////////////////////////////////////////////
// This implements add operator by the following method:              //
//                                                                    //
//  a   x       a * y + x * b                                         //
//  - + -  or   -------------                                         //
//  b   y           b * y                                             //
////////////////////////////////////////////////////////////////////////
CRational CRational::operator +(CRational rat)
  {
  CRational tempAmount;
  tempAmount.num = this->num * rat.den + this->den * rat.num;
  tempAmount.den = this->den * rat.den;
  tempAmount.simplify();
  return tempAmount;
  }
////////////////////////////////////////////////////////////////////////
// This function tests the add function.                              //
////////////////////////////////////////////////////////////////////////
void TestAdd(void)
  {
	cout << "\nTestAdd" << endl;
	cout << "-------" << endl;
	for (int i=0; i<TEST_COUNT; i++)
			{
			cout << "Test #" << i+1 << endl;
			CRational r1('r'), r2('r'), r3('r');
			cout << "r1 = "; r1.display();
			cout << "r2 = "; r2.display();
			cout << "r3 = "; r3.display();
			r1 = r2 + r3;
			cout << "After r1 = r2 + r3;\n";
			cout << "r1 = "; r1.display();
			cout << "r2 = "; r2.display();
			cout << "r3 = "; r3.display();
			cout << endl;
			}
  }
////////////////////////////////////////////////////////////////////////
// This implements subtraction operator by the following method:      //
//                                                                    //
//  a   x       a * y - x * b                                         //
//  - - -  or   -------------                                         //
//  b   y           b * y                                             //
////////////////////////////////////////////////////////////////////////
CRational CRational::operator -(CRational rat)
  {
  CRational tempAmount;
  tempAmount.num = this->num * rat.den - this->den * rat.num;
  tempAmount.den = this->den * rat.den;
  tempAmount.simplify();
  return tempAmount;
  }
////////////////////////////////////////////////////////////////////////
// This function tests the subtract function.                         //
////////////////////////////////////////////////////////////////////////
void TestSubtract(void)
  {
	cout << "\nTestSubtract" << endl;
	cout << "------------" << endl;
	for (int i=0; i<TEST_COUNT; i++)
			{
			cout << "Test #" << i+1 << endl;
			CRational r1('r'), r2('r'), r3('r');
			cout << "r1 = "; r1.display();
			cout << "r2 = "; r2.display();
			cout << "r3 = "; r3.display();
			r1 = r2 - r3;
			cout << "After r1 = r2 - r3;\n";
			cout << "r1 = "; r1.display();
			cout << "r2 = "; r2.display();
			cout << "r3 = "; r3.display();
			cout << endl;
			}
  }
////////////////////////////////////////////////////////////////////////
// This implements multiplication operation by the following method:  //
//                                                                    //
//  a   x           a * x                                             //
//  - * -  or       -----                                             //
//  b   y           b * y                                             //
////////////////////////////////////////////////////////////////////////
CRational CRational::operator *(CRational rat)
  {
  CRational tempAmount;
  tempAmount.num = this->num * rat.num;
  tempAmount.den = this->den * rat.den;
  tempAmount.simplify();
  return tempAmount;
  }
////////////////////////////////////////////////////////////////////////
// This function tests the multiply function.                         //
////////////////////////////////////////////////////////////////////////
void TestMultiply(void)
  {
	cout << "\nTestMultiply" << endl;
	cout << "------------" << endl;
	for (int i=0; i<TEST_COUNT; i++)
			{
			cout << "Test #" << i+1 << endl;
			CRational r1('r'), r2('r'), r3('r');
			cout << "r1 = "; r1.display();
			cout << "r2 = "; r2.display();
			cout << "r3 = "; r3.display();
			r1 = r2 * r3;
			cout << "After r1 = r2 * r3;\n";
			cout << "r1 = "; r1.display();
			cout << "r2 = "; r2.display();
			cout << "r3 = "; r3.display();
			cout << endl;
			}
  }
////////////////////////////////////////////////////////////////////////
// This implements divide operation  by the following method:         //
//                                                                    //
//  a   x      a   y                                                  //
//  - / -  or  - * -                                                  //
//  b   y      b   x                                                  //
////////////////////////////////////////////////////////////////////////
CRational CRational::operator / (CRational rat)
  {
  CRational tempAmount;
  tempAmount.num = this->num * rat.den;
  tempAmount.den = this->den * rat.num;
  tempAmount.simplify();
  return tempAmount;
  }
////////////////////////////////////////////////////////////////////////
// This function tests the divide function.                           //
////////////////////////////////////////////////////////////////////////
void TestDivide(void)
  {
	cout << "\nTestDivide" << endl;
	cout << "----------" << endl;
  for (int i=0; i<TEST_COUNT; i++)
    {
    cout << "Test #" << i+1 << endl;
    CRational r1('r'), r2('r'), r3('r');
    cout << "r1 = "; r1.display();
    cout << "r2 = "; r2.display();
    cout << "r3 = "; r3.display();
    r1 = r2 / r3;
    cout << "After r1 = r2 / r3;\n";
    cout << "r1 = "; r1.display();
    cout << "r2 = "; r2.display();
    cout << "r3 = "; r3.display();
    cout << endl;
    }
  }
////////////////////////////////////////////////////////////////////////
// This global function that find the greatest common divisor         //
// by subtracting the greater number until both numbers are equal.    //
////////////////////////////////////////////////////////////////////////
int gcd(int x, int y)
  {
  while (x != y)
    if (y > x)
        y = y - x;
      else
        x = x - y;
  return x;
  }
////////////////////////////////////////////////////////////////////////
// This function tests the gcd function.                              //
////////////////////////////////////////////////////////////////////////
void TestGcd(void)
  {
  cout << "\nTestGcd" << endl;
  cout << "-------" << endl;
  for (int i=0; i<TEST_COUNT; i++)
    {
    cout << "Test #" << i+1 << endl;
    int x = rand()%100 + 1;
    int y = rand()%100 + 1;
    int greatestcd = gcd(x, y);
    cout << "x = " << x << endl;
    cout << "y = " << y << endl;
    cout << "gcd = " << greatestcd << endl;
    cout << endl;
    }
  }
////////////////////////////////////////////////////////////////////////
// This function displays the fraction                                //
////////////////////////////////////////////////////////////////////////
void CRational::display(void)
  {
  cout << this->num << " / " << this->den << endl;
  if (0 == den)
      cout << "*** Error - denominator is zero\n";
  }
////////////////////////////////////////////////////////////////////////
// This function tests the display function.                          //
////////////////////////////////////////////////////////////////////////
void TestDisplay(void)
  {
	cout << "TestDisplay" << endl;
	cout << "-----------" << endl;
  for (int i=0; i<TEST_COUNT; i++)
    {
    cout << "Test #" << i+1 << endl;
    CRational r1('r');
    r1.display();
    cout << endl;
    }
  }
////////////////////////////////////////////////////////////////////////
// This function display the fraction as a real number.               //
////////////////////////////////////////////////////////////////////////
void CRational::displayValue(void)
  {
  if (den != 0)
      {
      double realValue;
      realValue = double(this->num) / this->den;
      cout << realValue << endl;
      }
    else
      cout << "*** Error - denominator is zero\n";
  }
////////////////////////////////////////////////////////////////////////
// This function tests the displayValue function.                     //
////////////////////////////////////////////////////////////////////////
void TestDisplayValue(void)
  {
	cout << "TestDisplayValue" << endl;
	cout << "----------------" << endl;
  for (int i=0; i<TEST_COUNT; i++)
    {
		cout << "Test #" << i+1 << endl;
    CRational r1('r');
    r1.display();
    r1.displayValue();
    cout << endl;
    }  
  }
/////////////////////////////////////////////////////////////////////////
// This extraction operator overloading
// function provides an easier way to input fractions.
/////////////////////////////////////////////////////////////////////////
istream &operator >> (istream &inp, CRational &rat)
  {
  cout << "Enter num.   ";
  inp >> rat.num;
  if (rat.num < MIN_VALUE || rat.num > MAX_VALUE)
      rat.num = 1;
  cout << "Enter den.   ";
  inp >> rat.den;
  if (rat.den < MIN_VALUE || rat.den > MAX_VALUE)
      rat.den = 1;
  return inp;
  }
/////////////////////////////////////////////////////////////////////////
// This function tests the operator >> function 5 times.
/////////////////////////////////////////////////////////////////////////
void TestOperatorExtraction(void)
  {
  cout << "TestOperatorExtraction" << endl;
  cout << "----------------------" << endl;
  for (int i=0; i<TEST_COUNT; i++)
    {
    cout << "Test #" << i+1 << endl;
    CRational r1('r');
    cout << "r1 = ";
    r1.display();
    cin >> r1;
    cout << "r1 = " << r1 << endl;
    cout << endl;
    }
  }
/////////////////////////////////////////////////////////////////////////
// This insertion operator overloading
// functions provides an easier way to display fractions.
/////////////////////////////////////////////////////////////////////////
ostream &operator << (ostream &out, const CRational &rat)
  {
  out << rat.num << " / " << rat.den << endl;
  if (0 == rat.den)
      out << "*** Error - denominator is zero\n";
  return out;
  }
/////////////////////////////////////////////////////////////////////////
// This function tests the operator << function 5 times.
/////////////////////////////////////////////////////////////////////////
void TestOperatorInsertion(void)
  {
  cout << "TestOperatorInsertion" << endl;
  cout << "---------------------" << endl;
  for (int i=0; i<TEST_COUNT; i++)
    {
    cout << "Test #" << i+1 << endl;
    CRational r1('r');
    cout << "r1 = ";
    r1.display();
    cout << "r1 = " << r1 << endl;
    cout << endl;
    }
  }
/////////////////////////////////////////////////////////////////////////
// This function overloads the equal operator.
// to check if two fractions are equal.
// a/b == x/y
// a*y == x*b
/////////////////////////////////////////////////////////////////////////
bool CRational::operator == (const CRational &rat) const
  {
  return ((this->num * rat.den) == (this->den * rat.num));
  }
/////////////////////////////////////////////////////////////////////////
// This function tests the == function.
/////////////////////////////////////////////////////////////////////////
void TestOperatorEqual(void)
  {
  cout << "TestOperatorEqual" << endl;
  cout << "-----------------" << endl;
  for (int i=0; i<TEST_COUNT; i++)
    {
    cout << "Test #" << i+1 << endl;
    CRational r1('r'), r2('r');
    cout << "r1 = "; r1.display();
    cout << "r2 = "; r2.display();
    if (r1 == r2)
        cout << "r1 is equal to r2\n";
      else
        cout << "r1 IS NOT  equal to r2\n";
    cout << "After r1 = r2;\n";
    r1 = r2;
    cout << "r1 = "; r1.display();
    cout << "r2 = "; r2.display();
    if (r1 == r2)
        cout << "r1 is equal to r2\n";
      else
        cout << "r1 IS NOT  equal to r2\n";
    cout << endl;
    }
  }
///////////////////////////////////////////////////////////
// This function overloads the not equal operator.
// to check if two functions are not equal.
// !(a/b == x/y)
///////////////////////////////////////////////////////////
bool CRational::operator != (const CRational &rat) const
  {
  return !(*this == rat);
  }
///////////////////////////////////////////////////////////
// This functions test the != operator function.
///////////////////////////////////////////////////////////
void TestOperatorNotEqual(void)
  {
  cout << "TestOperatorNotEqual" << endl;
  cout << "--------------------" << endl;
  for (int i=0; i<TEST_COUNT; i++)
    {
    cout << "Test #" << i+1 << endl;
    CRational r1('r'), r2('r');
    cout << "r1 = "; r1.display();
    cout << "r2 = "; r2.display();
    if (r1 != r2)
        cout << "r1 IS NOT  equal to r2\n";
      else
        cout << "r1 is eqial to r2\n";
    cout << "After r1 = r2;\n";
    r1 = r2;
    cout << "r1 = "; r1.display();
    cout << "r2 = "; r2.display();
    if (r1 == r2)
        cout << "r1 is equal to r2\n";
      else
        cout << "r1 IS NOT  equal to r2\n";
    cout << endl;
    }
  }
/////////////////////////////////////////////////////////////////
// This function checks if one fraction is greater than 
// or equal to the second fraction by overloading the operator.
// a/b >= x/y  
// a*y >= x*b
/////////////////////////////////////////////////////////////////
bool CRational::operator >= (const CRational &rat) const
  {
  return ((this->num * rat.den) >= (rat.num * this->den));
  }
/////////////////////////////////////////////////////////////////////////
// This function test the >= operator function.
/////////////////////////////////////////////////////////////////////////
void TestOperatorGTOET(void)
  {
  cout << "testOperatorGTOET" << endl;
  cout << "-----------------" << endl;
  for (int i=0; i<TEST_COUNT; i++)
    {
    cout << "Test #" << i+1 << endl;
    CRational r1('r'), r2('r');
    cout << "r1 = "; r1.display();
    cout << "r2 = "; r2.display();
    if (r1 >= r2)
        cout << "r1 is greater than or equal to r2\n";
    else
        cout << "r1 IS NOT greater than or equal to r2\n";
    cout << "After r1 = r2;\n";
    r1 = r2;
    cout << "r1 = "; r1.display();
    cout << "r2 = "; r2.display();
    if (r1 >= r2)
        cout << "r1 is greater than or equal to r2\n";
    else
        cout << "r1 IS NOT greater than or equal to r2\n";
    cout << endl;
    }
  }
/////////////////////////////////////////////////////////////////////////
// This function checks if one fraction is less than 
// or equal to the second fraction by overloading the operator.
// a/b <= x/y
// !(a/b > x/y)
/////////////////////////////////////////////////////////////////////////
bool CRational::operator <= (const CRational &rat) const
  {
  return !(*this > rat);
  }
/////////////////////////////////////////////////////////////////////////
// This function test the <= operator function.
/////////////////////////////////////////////////////////////////////////
void TestOperatorLTOET(void)
  {
  cout << "testOperatorLTOET" << endl;
  cout << "-----------------" << endl;
    for (int i=0; i<TEST_COUNT; i++)
    {
    cout << "Test #" << i+1 << endl;
    CRational r1('r'), r2('r');
    cout << "r1 = "; r1.display();
    cout << "r2 = "; r2.display();
    if (r1 <= r2)
        cout << "r1 is less than or equal to r2\n";
    else if (r1 > r2)
        cout << "r1 IS NOT less than or equal to r2\n";
    cout << "After r1 = r2;\n";
    r1 = r2;
    cout << "r1 = "; r1.display();
    cout << "r2 = "; r2.display();
    if (r1 <= r2)
        cout << "r1 is less than or equal to r2\n";
    else if (r1 > r2)
        cout << "r1 IS NOT less than or equal to r2\n";
    cout << endl;
    }
  }
///////////////////////////////////////////////////////////
// This function overloads the > operator to compare if 
// one fraction is greater than the second fraction.
///////////////////////////////////////////////////////////
bool CRational::operator > (const CRational &rat) const
  // a/b > x/y  
  // a*y > x*b
  {
  return ((this->num * rat.den) > (this->den * rat.num));
  }
/////////////////////////////////////////////////////////////////////////
// This function tests the > operater overloaded function.
/////////////////////////////////////////////////////////////////////////
void TestOperatorGreaterThan(void)
  {
  cout << "testOperatorGreaterThan" << endl;
  cout << "-----------------" << endl;
  for (int i=0; i<TEST_COUNT; i++)
    {
    cout << "Test #" << i+1 << endl;
    CRational r1('r'), r2('r');
    cout << "r1 = "; r1.display();
    cout << "r2 = "; r2.display();
    if (r1 > r2)
          cout << "r1 is greater than r2\n";
      else
          cout << "r1 IS NOT greater than r2\n";
      cout << "After r1 = r2;\n";
    r1 = r2;
    cout << "r1 = "; r1.display();
    cout << "r2 = "; r2.display();
    if (r1 > r2)
          cout << "r1 is greater than r2\n";
      else 
          cout << "r1 IS NOT greater than r2\n";
      cout << endl;
    }
  }
/////////////////////////////////////////////////////////////////////////
// This function overloads the < operator to compare if 
// one fraction is less than the second fraction.
// a/b < x/y
// !(a/b >= x/y)
/////////////////////////////////////////////////////////////////////////
bool CRational::operator < (const CRational &rat) const
  {
  return !(*this >= rat);
  }
/////////////////////////////////////////////////////////////////////////
// This function tests the < operater overloaded function.
/////////////////////////////////////////////////////////////////////////
void TestOperatorLessThan(void)
  {
  cout << "testOperatorLessThan" << endl;
  cout << "-----------------" << endl;
    for (int i=0; i<TEST_COUNT; i++)
    {
    cout << "Test #" << i+1 << endl;
    CRational r1('r'), r2('r');
    cout << "r1 = "; r1.display();
    cout << "r2 = "; r2.display();
    if (r1 < r2)
        cout << "r1 is less than r2\n";
    else
        cout << "r1 IS NOT less than r2\n";
    cout << "After r1 = r2;\n";
    r1 = r2;
    cout << "r1 = "; r1.display();
    cout << "r2 = "; r2.display();
    if (r1 < r2)
        cout << "r1 is less than r2\n";
    else
        cout << "r1 IS NOT less than r2\n";
    cout << endl;
    }
  }
void main(void)
  {
  srand(time(NULL));
  TestConstructor();
  TestConstructor2();
  TestAdd();
  TestSubtract();
  TestMultiply();
  TestDivide();
  TestDisplay();
  TestDisplayValue();
  TestGcd();
  TestOperatorEqual();
  TestOperatorNotEqual();
  TestOperatorExtraction();
  TestOperatorInsertion();
  TestOperatorGTOET();
  TestOperatorLTOET();
  TestOperatorGreaterThan();
  TestOperatorLessThan();
  }