|
//file : p016.cpp //author: AOU //date : 07/23/2004 /* Project #12 07/23/2004 void populate(void); CArray(int m); CArray(char ch); //when ch=='r' friend ostream & operator << (ostream &bob , const CArray &a); Project #11 07/22/2004 CArray operator -(const CArray &a2) const; friend bool isEmpty(const CArray ta); Project #10 07/21/2004 bool operator <= (const CArray &a2) const; CArray operator ||(const CArray &a2) const; CArray operator &&(const CArray &a2) const; Project #9 07/20/2004 CArray operator +(const CArray &a2) const; friend ostream & operator << (ostream &bob , const CArray &a) Project #8 07/19/2004 bool deleteAtPos(int index); int purgeDupes(void); Project #7 07/15/2004 bool isEqualTo(const CArray &a2) const; bool operator ==(const CArray &a2) const; bool operator !=(const CArray &a2) const; Project #6 07/14/2004 friend bool isEqualTo(const CArray &a1, const CArray &a2); CArray(const CArray &second); and identify contstant functions Project #5 07/13/2004 void shuffle(void); friend void shuffle(CArray &a1); Project #4 07/12/2004 bool has(int x); bool hasDistinctValues(void); */ ///////////////////////////////////////////////// // include files ///////////////////////////////////////////////// #include <iostream.h> #include <stdlib.h> #include <time.h> ///////////////////////////////////////////////// // constants ///////////////////////////////////////////////// const int ARRAY_SIZE = 10; const int MIN_VALUE = 0; const int MAX_VALUE = 6; const int TEST_COUNT = 20; ///////////////////////////////////////////////// // test functions prototypes ///////////////////////////////////////////////// void testAll(void); void test_populate(void); void test_constructor_m(void); void test_constructor_r(void); void test_operator_output(void); void test_sortBubble(void); void test_isSorted(void); void test_constructor_s(void); void test_constructor_d(void); void test_has(void); void test_hasDistinctValues(void); void test_shuffle(void); void test_shuffle_friend(void); void test_isEqualTo_friend(void); void test_constructor_copy(void); void test_isEqualTo_member(void); void test_isEqualTo_operator(void); void test_isNotEqualTo_operator(void); void test_deleteAtPos(void); void test_purgeDupes(void); void test_operator_plus(void); void test_operator_subset(void); void test_operator_intersection(void); void test_operator_union(void); void test_operator_minus(void); ///////////////////////////////////////////////// // class CNode ///////////////////////////////////////////////// class CNode { private: int info; CNode *next; public: CNode(void); CNode(int x); friend class CArray; friend ostream & operator << (ostream &bob , const CArray &a); }; ///////////////////////////////////////////////// // class CArray ///////////////////////////////////////////////// class CArray { private: int n; CNode *head; CNode *tail; void initialize(void) { this->n = 0; this->head = NULL; this->tail = NULL; }; public: CArray(void); bool insertAtTail(int x); void display(void); void populate(void); CArray(int m); CArray(char ch); friend ostream & operator << (ostream &bob , const CArray &a); void displayDetails(void); }; ///////////////////////////////////////////////// // void main(void) ///////////////////////////////////////////////// void main(void) { //test_populate(); //test_constructor_m(); //test_constructor_r(); test_operator_output(); } ///////////////////////////////////////////////// // ostream & operator << (ostream &bob , const CArray &a) ///////////////////////////////////////////////// ostream & operator << (ostream &bob, const CArray &ta) { bob << "CArray[" << ta.n << "] = "; CNode * p; p = ta.head; while (p != NULL) { bob << p->info << ' '; p = p->next; } bob << endl; return bob; } ///////////////////////////////////////////////// // void test_operator_output(void) ///////////////////////////////////////////////// void test_operator_output(void) { cout << "++++++++++++++++++++\n"; cout << "test_operator_output\n"; cout << "++++++++++++++++++++\n"; for (int i=1; i<=TEST_COUNT; i++) { CArray a1('r'); a1.display(); cout << a1; a1.displayDetails(); cout << "----------------\n"; } } ///////////////////////////////////////////////// // CArray::CArray(char ch) ///////////////////////////////////////////////// CArray::CArray(char ch) { this->initialize(); //cout << "Constructor CArray(char ch) was called\n"; if ('r' == ch || 'R' == ch) this->populate(); /* else if ('s' == ch || 'S' == ch) { this->populate(); this->sortBubble(); } else if ('d' == ch || 'D' == ch) { n = 0; int times = rand()%(ARRAY_SIZE+1); for (int i=1; i<=times; i++) { int x = MIN_VALUE + rand()%(MAX_VALUE-MIN_VALUE+1); if (!this->has(x)) a[n++] = x; } } else n = 0; */ } ///////////////////////////////////////////////// // void test_constructor_r(void) ///////////////////////////////////////////////// void test_constructor_r(void) { cout << "++++++++++++++++++\n"; cout << "test_constructor_r\n"; cout << "++++++++++++++++++\n"; for (int i=1; i<=TEST_COUNT; i++) { CArray a1('r'); cout << "After CArray a1('r');\n"; a1.display(); cout << "----------------\n"; } } ///////////////////////////////////////////////// // CArray::CArray(int m) ///////////////////////////////////////////////// CArray::CArray(int m) { //cout << "Constructor CArray(int n) was called\n"; if (m > ARRAY_SIZE) m = ARRAY_SIZE; this->initialize(); for (int i=1; i<=m; i++) { int x = MIN_VALUE + rand()%(MAX_VALUE-MIN_VALUE+1); this->insertAtTail(x); } } ///////////////////////////////////////////////// // void test_constructor_m(void) ///////////////////////////////////////////////// void test_constructor_m(void) { cout << "++++++++++++++++++\n"; cout << "test_constructor_m\n"; cout << "++++++++++++++++++\n"; for (int i=1; i<=TEST_COUNT; i++) { int n = rand()%(ARRAY_SIZE*2); CArray a1(n); cout << "After CArray a1(" << n << ");\n"; a1.display(); cout << "----------------\n"; } } ///////////////////////////////////////////////// // void CArray::populate(void) ///////////////////////////////////////////////// void CArray::populate(void) { int m = rand()%(ARRAY_SIZE+1); for (int i=1; i<=m; i++) { int x = MIN_VALUE + rand()%(MAX_VALUE-MIN_VALUE+1); this->insertAtTail(x); } } ///////////////////////////////////////////////// // void test_populate(void) ///////////////////////////////////////////////// void test_populate(void) { cout << "+++++++++++++\n"; cout << "test_populate\n"; cout << "+++++++++++++\n"; for (int i=1; i<=TEST_COUNT; i++) { CArray a1; a1.display(); a1.populate(); cout << "After a1.populate();\n"; a1.display(); cout << "----------------\n"; } } ///////////////////////////////////////////////// // void CArray::display(void) ///////////////////////////////////////////////// void CArray::display(void) { cout << "CArray[" << this->n << "] = "; CNode * p; p = this->head; while (p != NULL) { cout << p->info << ' '; p = p->next; } cout << endl; } ///////////////////////////////////////////////// // void CArray::displayDetails(void) ///////////////////////////////////////////////// void CArray::displayDetails(void) { cout << "head = " << this->head << endl; cout << "tail = " << this->tail << endl; cout << " n = " << this->n << endl; cout << "NULL = " << NULL << endl; CNode * p; cout << "sizeof p = " << sizeof(p) << endl; cout << "sizeof NULL = " << sizeof(NULL) << endl; cout << "sizeof n = " << sizeof(n) << endl; cout << "sizeof head = " << sizeof(head) << endl; cout << "sizeof tail = " << sizeof(tail) << endl; cout << "sizeof this = " << sizeof(this) << endl; cout << "sizeof *this = " << sizeof(*this) << endl; p = this->head; while (p != NULL) { cout << "address=" << p << ' '; cout << "info=" << p->info << ' '; cout << "next=" << p->next << endl; p = p->next; } cout << endl; } ///////////////////////////////////////////////// // bool CArray::insertAtTail(int x) ///////////////////////////////////////////////// bool CArray::insertAtTail(int x) { CNode *p; p = new CNode; if (NULL == p) //2004.07.23 return false; p->info = x; p->next = NULL; if (NULL == this->head) this->head = this->tail = p; else { this->tail->next = p; this->tail = p; } this->n++; return true; } ///////////////////////////////////////////////// // CArray::CArray(void) ///////////////////////////////////////////////// CArray::CArray(void) { this->initialize(); } ///////////////////////////////////////////////// // CNode::CNode(void) ///////////////////////////////////////////////// CNode::CNode(void) { this->info = 0; this->next = NULL; } ///////////////////////////////////////////////// // CNode::CNode(int x) ///////////////////////////////////////////////// CNode::CNode(int x) { this->info = x; this->next = NULL; } /* Output: [0 ]= [1 ]= 5 [2 ]= 5 2 [3 ]= 5 2 4 [4 ]= 5 2 4 9 Press any key to continue */ |