bigInt03
Home ] Up ]

 

//Date:   2003.11.10
//File:   bigInt03.cpp
//Author: AOU


///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
#include <iostream.h>
#include <stdlib.h>
#include <time.h>


/*********************************************************/
/* BEGIN WHAT IS NEW                                     */
/*********************************************************/
/*

    CDigit(char ch);
    CDigit(const CDigit &d2);
    bool operator     ==  (const CBigInt &i2) const;

*/
/*********************************************************/
/* END WHAT IS NEW                                       */
/*********************************************************/


///////////////////////////////////////////////////////////
//constants
///////////////////////////////////////////////////////////
const unsigned int MIN_VALUE   = 0;
const unsigned int MAX_VALUE   = 9;
const unsigned int MAX_EXPO    = 4;
const unsigned int UNDEFINED   = 911;
const int MAX_SIZE    = 10;
const int TEST_COUNT  = 25;


///////////////////////////////////////////////////////////
//prototypes
///////////////////////////////////////////////////////////
void driverConstructorDefault_CDigit(void);
void driverConstructorChar_CDigit(void);
void driverConstructorCopy_CDigit(void);
void driverOperatorEqual_CDigit(void);


///////////////////////////////////////////////////////////
//class CDigit
///////////////////////////////////////////////////////////
class CDigit
  {
  private:
    CDigit *_prev;
    unsigned int _coef;
    unsigned int _expo;
    CDigit *_next;
  public:
    CDigit(void); //
    CDigit(char ch);          //2003.11.10
    CDigit(const CDigit &d2); //2003.11.10

    void display(void) const; //
    void displayAll(void) const; //
    void init(void); //

    bool   operator   == (const CDigit &d2) const;
    bool   operator   != (const CDigit &d2) const;
    bool operator     <  (const CDigit &d2) const;
    bool operator     >  (const CDigit &d2) const;
    bool operator     >= (const CDigit &d2) const;
    bool operator     <= (const CDigit &d2) const;
    CDigit operator   +  (const CDigit &d2) const;
    CDigit operator   *  (const CDigit &d2) const;
    CDigit operator   -  (const CDigit &d2) const;
    CDigit operator   /  (const CDigit &d2) const;
    CDigit & operator =  (const CDigit &d2);
    friend ostream & operator << (ostream &bob, const CDigit &d2);
  };


///////////////////////////////////////////////////////////
//class CBigInt
///////////////////////////////////////////////////////////
class CBigInt
  {
  private:
    CDigit *_first;
    CDigit *_last;
    int _count;
  public:
    CBigInt(void);
    CBigInt(char ch);
    CBigInt(int n);
    CBigInt(const CBigInt &i2);

    void display(void) const;
    void init(void);
    bool insert(CDigit x);
    bool isSorted(void) const;
    void shuffle(void);
    void sortBubble(void);
    void populate(int n);

    CBigInt & operator = (const CBigInt &i2);

    bool operator     ==  (const CBigInt &i2) const;//2003.11.10
    bool operator     !=  (const CBigInt &i2) const;
    bool operator      <  (const CBigInt &i2) const;
    bool operator      <= (const CBigInt &i2) const;
    bool operator      >  (const CBigInt &i2) const;
    bool operator      >= (const CBigInt &i2) const;
    CBigInt operator   +  (const CBigInt &i2) const;
    CBigInt operator   *  (const CBigInt &i2) const;
    CBigInt operator   -  (const CBigInt &i2) const;
    CBigInt operator   /  (const CBigInt &i2) const;

    friend ostream & operator << (ostream &bob, const CBigInt &bi);

    bool hasDistinct(void) const;
    
    bool deleteAtPos(int p);
  };


///////////////////////////////////////////////////////////
//main
///////////////////////////////////////////////////////////
void main(void)
  {
  srand(time(NULL));
  //driverConstructorDefault_CDigit();

  //driverConstructorChar_CDigit();
  //driverConstructorCopy_CDigit();
  driverOperatorEqual_CDigit();

}


///////////////////////////////////////////////////////////
// void driverOperatorEqual_CDigit()
///////////////////////////////////////////////////////////
void driverOperatorEqual_CDigit()
  {
  cout << "--------------------------\n";
  cout << "driverOperatorEqual_CDigit\n";
  cout << "--------------------------\n";
  for (int i=1; i<=TEST_COUNT; i++)
    {
    CDigit d1('r');
    CDigit d2('r');
    CDigit d3(d1);

    cout << "d1="; d1.display(); cout << endl;
    cout << "d1="; d1.displayAll(); cout << endl;
    cout << "d2="; d2.display(); cout << endl;
    cout << "d2="; d2.displayAll(); cout << endl;
    cout << "d3="; d3.display(); cout << endl;
    cout << "d3="; d3.displayAll(); cout << endl;

    if(d1 == d2)
      cout << "Digits d1 & d2 Are Equal" << endl;
    else
      cout << "Digits d1 & d2 Are NOT Equal" << endl;

    if(d1 == d3)
      cout << "Digits d1 & d3 Are Equal" << endl;
    else
      cout << "Digits d1 & d3 Are NOT Equal" << endl;

    cout << "......................." << endl;

    }

  }


///////////////////////////////////////////////////////////
// bool CDigit::operator == (const CDigit &d2) const
///////////////////////////////////////////////////////////
bool CDigit::operator == (const CDigit &d2) const
  {
  if(this->_coef != d2._coef || this->_expo != d2._expo)
    return false;
  return true;
  }


///////////////////////////////////////////////////////////
// void CDigit::init(void)
///////////////////////////////////////////////////////////
void CDigit::init(void)
  {
  this->_prev = NULL;
  this->_next = NULL;
  }


///////////////////////////////////////////////////////////
// void driverConstructorCopy_CDigit(void)
///////////////////////////////////////////////////////////
void driverConstructorCopy_CDigit(void)
  {
  cout << "----------------------------\n";
  cout << "driverConstructorCopy_CDigit\n";
  cout << "----------------------------\n";
  for (int i=1; i<=TEST_COUNT; i++)
    {
    CDigit d1('r');
    CDigit d2(d1);
    cout << "d1="; d1.display(); cout << endl;
    cout << "d1="; d1.displayAll(); cout << endl;
    cout << "d2="; d2.display(); cout << endl;
    cout << "d2="; d2.displayAll(); cout << endl;
    cout << "...................\n";
    }

  }


///////////////////////////////////////////////////////////
// CDigit::CDigit(const CDigit &d2)
///////////////////////////////////////////////////////////
CDigit::CDigit(const CDigit &d2)
  {
  this->_coef = d2._coef;
  this->_expo = d2._expo;
  this->init();
  }


///////////////////////////////////////////////////////////
// void driverConstructorChar_CDigit(void)
///////////////////////////////////////////////////////////
void driverConstructorChar_CDigit(void)
  {
  cout << "----------------------------\n";
  cout << "driverConstructorChar_CDigit\n";
  cout << "----------------------------\n";
  for (int i=1; i<=TEST_COUNT; i++)
    {
    CDigit d1('r'), d2('r');
    cout << "d1="; d1.display(); cout << endl;
    cout << "d1="; d1.displayAll(); cout << endl;
    cout << "d2="; d2.display(); cout << endl;
    cout << "d2="; d2.displayAll(); cout << endl;
    cout << "...................\n";
    }
  }


///////////////////////////////////////////////////////////
// CDigit::CDigit(char ch)
///////////////////////////////////////////////////////////
CDigit::CDigit(char ch)
  {
  this->_coef = rand()%(MAX_VALUE-MIN_VALUE+1)+MIN_VALUE;
  this->_expo = rand()%MAX_EXPO;
  this->init();
  }



///////////////////////////////////////////////////////////
// void driverConstructorDefault_CDigit(void)
///////////////////////////////////////////////////////////
void driverConstructorDefault_CDigit(void)
  {
  cout << "-------------------------------\n";
  cout << "driverConstructorDefault_CDigit\n";
  cout << "-------------------------------\n";
  for (int i=1; i<=TEST_COUNT; i++)
    {
    CDigit d1, d2;
    cout << "d1="; d1.display(); cout << endl;
    cout << "d1="; d1.displayAll(); cout << endl;
    cout << "d2="; d2.display(); cout << endl;
    cout << "d2="; d2.displayAll(); cout << endl;
    cout << "...................\n";
    }
  }


///////////////////////////////////////////////////////////
// void CDigit::displayAll(void) const
///////////////////////////////////////////////////////////
void CDigit::displayAll(void) const
  {
  cout << '@' << this << "=";
  cout << '[' << this->_prev << ' ';
  cout << this->_coef << ' ';
  cout << this->_expo << ' ';
  cout << this->_next << ']';
  }


///////////////////////////////////////////////////////////
// void CDigit::display(void) const
///////////////////////////////////////////////////////////
void CDigit::display(void) const
  {
  cout << this->_coef;
  }


///////////////////////////////////////////////////////////
// CDigit::CDigit(void)
///////////////////////////////////////////////////////////
CDigit::CDigit(void)
  {
  this->_coef = UNDEFINED;
  this->_expo = UNDEFINED;
  this->init();
  }