|
//file : p017.cpp //author: AOU //date : 07/26/2004 /* Project #13 07/23/2004 void sortBubble(void); bool isSorted(void) const; int deleteAll(void); ~CArray(void); 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 = 600; const int TEST_COUNT = 10; ///////////////////////////////////////////////// // 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_deleteAll(void); void test_destructor(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 sortBubble(void); bool isSorted(void) const; int deleteAll(void); ~CArray(void); }; ///////////////////////////////////////////////// // void main(void) ///////////////////////////////////////////////// void main(void) { srand(time(NULL)); //test_populate(); //test_constructor_m(); //test_constructor_r(); //test_operator_output(); test_sortBubble(); test_isSorted(); test_deleteAll(); test_destructor(); } ///////////////////////////////////////////////// // void test_destructor(void) ///////////////////////////////////////////////// void test_destructor(void) { cout << "++++++++++++++\n"; cout << "test_destructor\n"; cout << "++++++++++++++\n"; for (int i=1; i<=TEST_COUNT; i++) { CArray *p; p = new CArray('r'); cout << "*p = " << *p; delete p; cout << "----------------\n"; } for (i=1; i<=TEST_COUNT; i++) { CArray ta('r'); cout << "ta = " << ta; cout << "----------------\n"; } } ///////////////////////////////////////////////// // CArray::~CArray(void) ///////////////////////////////////////////////// CArray::~CArray(void) { cout << "Destructor called\n"; this->deleteAll(); } ///////////////////////////////////////////////// // int CArray::deleteAll(void); ///////////////////////////////////////////////// /* p = head c = 0 while p <> NULL q = p>next kill p c++ p = q head = tail = null return c */ int CArray::deleteAll(void) { CNode *p, *q; int count = 0; p = this->head; while (p!=NULL) { q = p->next; delete p; count++; p = q; } this->head = this->tail = NULL; return count; } ///////////////////////////////////////////////// // void test_deleteAll(void) ///////////////////////////////////////////////// void test_deleteAll(void) { cout << "++++++++++++++\n"; cout << "test_deleteAll\n"; cout << "++++++++++++++\n"; for (int i=1; i<=TEST_COUNT; i++) { CArray a1('r'); cout << "a1 = " << a1; int d = a1.deleteAll(); cout << "After a1.deleteAll();\n"; cout << "and deleteing " << d << " nodes\n"; cout << "a1 = " << a1; cout << "----------------\n"; } } ///////////////////////////////////////////////// // bool CArray::isSorted(void) const ///////////////////////////////////////////////// bool CArray::isSorted(void) const { /* for (int i=0; i<=n-2; i++) if (a[i] > a[i+1]) return false; return true; */ if (this->n <=1) return true; CNode *p; p = this->head; while (p->next != NULL) { if (p->info > p->next->info) return false; p = p->next; } return true; } ///////////////////////////////////////////////// // void test_isSorted(void) ///////////////////////////////////////////////// void test_isSorted(void) { cout << "+++++++++++++\n"; cout << "test_isSorted\n"; cout << "+++++++++++++\n"; for (int i=1; i<=TEST_COUNT; i++) { CArray a1('r'); cout << "a1 = " << a1; a1.sortBubble(); cout << "After sortBubble()\n"; cout << "a1 = " << a1; if (a1.isSorted()) cout << "SORT successful\n"; else cout << "SORT NOT successful\n"; cout << "----------------\n"; } } ///////////////////////////////////////////////// // void CArray::sortBubble(void) ///////////////////////////////////////////////// void CArray::sortBubble(void) { if (this->n <= 1) return; int swapCount; do { swapCount = 0; //for (int i=0; i<=n-2; i++) for (CNode *p=this->head; p->next != NULL; p=p->next) { //if (a[i] > a[i+1]) if (p->info > p->next->info) { //int temp = a[i]; int temp = p->info; //a[i]=a[i+1]; p->info = p->next->info; //a[i+1] = temp; p->next->info = temp; swapCount++; } } } while (swapCount>0); } ///////////////////////////////////////////////// // void test_sortBubble(void) ///////////////////////////////////////////////// void test_sortBubble(void) { cout << "+++++++++++++++\n"; cout << "test_sortBubble\n"; cout << "+++++++++++++++\n"; for (int i=1; i<=TEST_COUNT; i++) { CArray a1('r'); cout << "a1 = " << a1; a1.sortBubble(); cout << "After sortBubble()\n"; cout << "a1 = " << a1; cout << "----------------\n"; } } ///////////////////////////////////////////////// // ostream & operator << (ostream &bob , const CArray &ta) ///////////////////////////////////////////////// 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; } /* */ |