|
//file : CDirectory01.cpp //date : 04/26/2002 //author: AOU //What is new? #include <iostream.h> #include <time.h> #include <stdlib.h> #include <string.h> const int TEST_COUNT = 5; const int MAX_SIZE = 5; char *testFirstNames[] = { "Joe", "Tom", "Kim", "Ann", "Bob", "May", "Jay", "Dan" }; const int COUNT_FNAMES = sizeof(testFirstNames)/sizeof(testFirstNames[0]); char *testLastNames[] = { "Smith", "Howard", "Ray", "Rather", "Letterman", "Leno" }; const int COUNT_LNAMES = sizeof(testLastNames)/sizeof(testLastNames[0]); char *testStreets[] = { "123 Main St", "23 Broadway", "12 Joplin St", "34 Rouse Dr" }; const int COUNT_STREETS = sizeof(testStreets)/sizeof(testStreets[0]); char *testCities[] = { "Pittsburg", "Parsons", "Joplin", "Miami", "Neosho" }; const int COUNT_CITIES = sizeof(testCities)/sizeof(testCities[0]); char *testStates[] = { "KS", "MO", "OK", "AR", "TX" }; const int COUNT_STATES = sizeof(testStates)/sizeof(testStates[0]); char *testZips[] = { "66762", "66921", "44234", "22312", "62123" }; const int COUNT_ZIPS = sizeof(testZips)/sizeof(testZips[0]); class CAddress { private: char *m_firstName; char *m_lastName; char *m_street; char *m_city; char *m_state; char *m_zip; int m_age; CAddress *m_next; public: CAddress(void); CAddress(char *fn, char *ln, char *sa, char *ct, char *st, char *zp, int age); CAddress(const CAddress &a); CAddress(char ch); ~CAddress(void); CAddress & operator =(const CAddress &a); void swap(CAddress &a2); bool operator ==(const CAddress &a2) const; bool operator !=(const CAddress &a2) const; CAddress * addressOfNext(void) const {return m_next;}; friend ostream & operator << (ostream &bob, const CAddress &a); friend class CDirectory; }; class CDirectory { private: CAddress *m_head; int m_count; public: CDirectory(void); friend ostream & operator << (ostream &bob, const CDirectory &d); bool insertAtHead(const CAddress &a); CDirectory(char ch); /*3*/ CDirectory(char *fn, char *ln, char *sa, char *ct, char *st, char *zp, int age); //03 CDirectory(const CAddress &a); /*1*/ CDirectory(int n); CDirectory(const CDirectory &d); //? CDirectory & operator =(const CAddress &a); //? CDirectory & operator =(const CDirectory &d); //? /*2*/ bool insertAtHead(char ch); //02 /*4*/ bool insertAtHead(char *fn, char *ln, char *sa, char *ct, char *st, char *zp, int age); void display(void); ~CDirectory(void); }; void testCAddressConstructorDefault(void); void testCAddressConstructorValue(void); void testCAddressConstructorCopy(void); void testCAddressConstructorRandom(void); void testCAddressOperatorAssign(void); void testCAddressSwap(void); void testCAddressOperatorEqual(void); void testCAddressOperatorNotEqual(void); void testCDirectoryConstructorDefault(void); void testCDirectoryInsertAtHeadNode(void); void testCDirectoryInsertAtHeadRandom(void); void testCDirectoryInsertAtHeadValues(void); void testCDirectoryConstructorNode(void); void testCDirectoryConstructorNodeValues(void); void testCDirectoryConstructorCount(void); void testCDirectoryConstructorRandom(void); void testCDirectoryDestructor(void); void main(void) { srand(time(NULL)); //testCAddressConstructorDefault(); //testCAddressConstructorValue(); //testCAddressConstructorCopy(); //testCAddressConstructorRandom(); //testCAddressOperatorAssign(); //testCAddressSwap(); //testCAddressOperatorEqual(); //testCAddressOperatorNotEqual(); //testCDirectoryConstructorDefault(); //testCDirectoryInsertAtHeadRandom(); //testCDirectoryInsertAtHeadNode(); //testCDirectoryInsertAtHeadValues(); //testCDirectoryConstructorNode(); //testCDirectoryConstructorNodeValues(); //testCDirectoryConstructorCount(); //testCDirectoryConstructorRandom(); testCDirectoryDestructor(); } void testCDirectoryDestructor(void) { for (int i=0; i<TEST_COUNT; i++) { int n = rand()%MAX_SIZE; cout << "n = " << n << endl; CDirectory *p = new CDirectory(n); cout << *p; delete p; cout << "-----------------------\n"; } } CDirectory::~CDirectory(void) { cout << "Destructor for CDirectory called\n"; CAddress *p; while (this->m_head != NULL) { p = this->m_head; this->m_head = p->m_next; delete p; } this->m_count = 0; } void testCDirectoryConstructorRandom(void) { for (int i=0; i<TEST_COUNT; i++) { int n = rand()%MAX_SIZE; cout << "n = " << n << endl; CDirectory d(n); cout << d; cout << "-----------------------\n"; } } CDirectory::CDirectory(char ch) { if ('r' == ch) { this->m_head = NULL; this->m_count = 0; int n = rand()%MAX_SIZE; while (n > 0) { this->insertAtHead('r'); n--; } } else { this->m_count = 0; this->m_head = NULL; } } void testCDirectoryConstructorCount(void) { for (int i=0; i<TEST_COUNT; i++) { int n = rand()%MAX_SIZE; cout << "n = " << n << endl; CDirectory d(n); cout << d; cout << "-----------------------\n"; } } CDirectory::CDirectory(int n) { this->m_head = NULL; this->m_count = 0; while (n > 0) { this->insertAtHead('r'); n--; } } void testCDirectoryConstructorNodeValues(void) { for (int i=0; i<TEST_COUNT; i++) { CDirectory d ( testFirstNames[rand()%COUNT_FNAMES], testLastNames[rand()%COUNT_LNAMES], testStreets[rand()%COUNT_STREETS], testCities[rand()%COUNT_CITIES], testStates[rand()%COUNT_STATES], testZips[rand()%COUNT_ZIPS], rand()%55+17 ); d.display(); cout << "---------------------\n"; } } CDirectory::CDirectory(char *fn, char *ln, char *sa, char *ct, char *st, char *zp, int age) { this->m_head = NULL; this->m_count = 0; this->insertAtHead(fn, ln, sa, ct, st, zp, age); } void testCDirectoryConstructorNode(void) { CDirectory d; for (int i=0; i<TEST_COUNT; i++) { CAddress a('r'); cout << "Constructing node:\n " << a << endl; CDirectory d(a); d.display(); } } CDirectory::CDirectory(const CAddress &a) { this->m_head = NULL; this->m_count = 0; this->insertAtHead(a); } void testCDirectoryInsertAtHeadValues(void) { CDirectory d; for (int i=0; i<TEST_COUNT; i++) { d.insertAtHead ( testFirstNames[rand()%COUNT_FNAMES], testLastNames[rand()%COUNT_LNAMES], testStreets[rand()%COUNT_STREETS], testCities[rand()%COUNT_CITIES], testStates[rand()%COUNT_STATES], testZips[rand()%COUNT_ZIPS], rand()%55+17 ); d.display(); cout << "-------------------\n"; } } bool CDirectory::insertAtHead(char *fn, char *ln, char *sa, char *ct, char *st, char *zp, int age) { CAddress *p = new CAddress(fn, ln, sa, ct, st, zp, age); if (NULL == p) return false; p->m_next = this->m_head; this->m_head = p; this->m_count++; return true; } void testCDirectoryInsertAtHeadNode(void) { CDirectory d; for (int i=0; i<TEST_COUNT; i++) { CAddress a('r'); cout << "After inserting " << a << endl; d.insertAtHead(a); d.display(); } } bool CDirectory::insertAtHead(const CAddress &a) { CAddress *p = new CAddress(a); if (NULL == p) return false; p->m_next = this->m_head; this->m_head = p; this->m_count++; return true; } void testCDirectoryInsertAtHeadRandom(void) { CDirectory d; for (int i=0; i<TEST_COUNT; i++) { d.insertAtHead('r'); d.display(); } } bool CDirectory::insertAtHead(char ch) { if ('r' == ch) { CAddress *p = new CAddress('r'); if (NULL == p) return false; p->m_next = this->m_head; this->m_head = p; this->m_count++; return true; } else return false; } void testCDirectoryConstructorDefault(void) { for (int i=0; i<TEST_COUNT; i++) { CDirectory d; d.display(); cout << "------------------\n"; } } CDirectory::CDirectory(void) { this->m_head = NULL; this->m_count = 0; } ostream & operator << (ostream &bob, const CDirectory &d) { bob << "Directory(" << d.m_count << ") =\n"; CAddress *p = d.m_head; while (p != NULL) { cout << *p << endl; p = p->addressOfNext(); } return bob; } void CDirectory::display(void) { cout << "Directory(" << this->m_count << ") =\n"; CAddress *p = this->m_head; while (p != NULL) { cout << *p << endl; p = p->m_next; } } void testCAddressOperatorNotEqual(void) { cout << "testCAddressOperatorNotEqual\n"; cout << "============================\n"; for (int i=1; i<=TEST_COUNT; i++) { CAddress a1('r'), a2('r'); cout << "a1=\n"; cout << a1 << endl; cout << "a2=\n"; cout << a2 << endl; if(a1 != a2) cout << "a1 and a2 are not equal\n"; else cout << "a1 and a2 are equal\n"; a1 = a2; cout << "After a1 = a2;\n"; cout << "a1=\n"; cout << a1 << endl; cout << "a2=\n"; cout << a2 << endl; if(a1 != a2) cout << "a1 and a2 are not equal\n"; else cout << "a1 and a2 are equal\n"; cout << "-------------------------\n"; } } bool CAddress::operator != (const CAddress &a2) const { return !(*this == a2); } void testCAddressOperatorEqual(void) { cout << "testCAddressOperatorEqual\n"; cout << "=========================\n"; for (int i=1; i<=TEST_COUNT; i++) { CAddress a1('r'), a2('r'); cout << "a1=\n"; cout << a1 << endl; cout << "a2=\n"; cout << a2 << endl; if(a1 == a2) cout << "a1 and a2 are equal\n"; else cout << "a1 and a2 are not equal\n"; a1 = a2; cout << "After a1 = a2;\n"; cout << "a1=\n"; cout << a1 << endl; cout << "a2=\n"; cout << a2 << endl; if(a1 == a2) cout << "a1 and a2 are equal\n"; else cout << "a1 and a2 are not equal\n"; cout << "-------------------------\n"; } } bool CAddress::operator == (const CAddress &a2) const { if (strcmp(this->m_firstName, a2.m_firstName) != 0) return false; if (strcmp(this->m_lastName, a2.m_lastName) != 0) return false; if (strcmp(this->m_street, a2.m_street) != 0) return false; if (strcmp(this->m_city, a2.m_city) != 0) return false; if (strcmp(this->m_state, a2.m_state) != 0) return false; if (strcmp(this->m_zip, a2.m_zip) != 0) return false; if (this->m_age != a2.m_age) return false; return true; } void testCAddressSwap(void) { cout << "testCAddressSwap\n"; cout << "================\n"; for (int i=1; i<=TEST_COUNT; i++) { CAddress a1('r'), a2('r'); cout << "a1=\n"; cout << a1 << endl; cout << "a2=\n"; cout << a2 << endl; a1.swap(a2); cout << "After CAddress a1.swap(a2);\n"; cout << "a1=\n"; cout << a1 << endl; cout << "a2=\n"; cout << a2 << endl; cout << "-------------------------\n"; } } void CAddress::swap(CAddress &a2) { CAddress temp; temp = *this; *this = a2; a2 = temp; } void testCAddressConstructorCopy(void) { cout << "testCAddressConstructorCopy\n"; cout << "===========================\n"; for (int i=1; i<=TEST_COUNT; i++) { CAddress a2('r'); cout << "a2=\n"; cout << a2 << endl; CAddress a3(a2); cout << "After CAddress a3(a2);\n"; cout << "a3=\n"; cout << a3 << endl; cout << "-------------------------\n"; } } CAddress::CAddress(const CAddress &a) { this->m_firstName = new char[strlen(a.m_firstName)+1]; strcpy(this->m_firstName, a.m_firstName); this->m_lastName = new char[strlen(a.m_lastName)+1]; strcpy(this->m_lastName, a.m_lastName); this->m_street = new char[strlen(a.m_street)+1]; strcpy(this->m_street, a.m_street); this->m_city = new char[strlen(a.m_city)+1]; strcpy(this->m_city, a.m_city); this->m_state = new char[strlen(a.m_state)+1]; strcpy(this->m_state, a.m_state); this->m_zip = new char[strlen(a.m_zip)+1]; strcpy(this->m_zip, a.m_zip); this->m_age = a.m_age; this->m_next = NULL; } void testCAddressOperatorAssign(void) { cout << "testCAddressOperatorAssign\n"; cout << "==========================\n"; for (int i=1; i<=TEST_COUNT; i++) { CAddress a1('r'), a2('r'), a3('r'); cout << "a1 a2 a3\n"; cout << a1 << endl << a2 << endl << a3 << endl; a1 = a3 = a2; cout << "After a1 = a3 = a2;\n"; cout << "a1 a2 a3\n"; cout << a1 << endl << a2 << endl << a3 << endl; cout << "-----------------------------------\n"; } } CAddress & CAddress::operator =(const CAddress &a) { this->m_firstName = new char[strlen(a.m_firstName) + 1]; strcpy(m_firstName, a.m_firstName); this->m_lastName = new char[strlen(a.m_lastName) + 1]; strcpy(m_lastName, a.m_lastName); this->m_street = new char[strlen(a.m_street) + 1]; strcpy(m_street, a.m_street); this->m_city = new char[strlen(a.m_city) + 1]; strcpy(m_city, a.m_city); this->m_state = new char[strlen(a.m_state) + 1]; strcpy(m_state, a.m_state); this->m_zip = new char[strlen(a.m_zip) + 1]; strcpy(m_zip, a.m_zip); this->m_age = a.m_age; this->m_next = NULL; return *this; } CAddress::~CAddress(void) { //cout << "Destructor called\n"; delete []this->m_firstName; delete []this->m_lastName; delete []this->m_street; delete []this->m_city; delete []this->m_state; delete []this->m_zip; this->m_firstName = NULL; this->m_lastName = NULL; this->m_street = NULL; this->m_city = NULL; this->m_state = NULL; this->m_zip = NULL; this->m_age = 0; this->m_next = NULL; } void testCAddressConstructorRandom(void) { cout << "testCAddressConstructorRandom\n"; cout << "=============================\n"; for (int i=1; i<=TEST_COUNT; i++) { CAddress a('r'); cout << "After CAddress a('r');\n"; cout << a << endl; cout << "------------------------\n"; } } CAddress::CAddress(char ch) { if ('r' == ch || 'R' == ch) { int i; i=rand()%COUNT_FNAMES; this->m_firstName = new char[strlen(testFirstNames[i])+1]; strcpy(this->m_firstName, testFirstNames[i]); i=rand()%COUNT_LNAMES; this->m_lastName = new char[strlen(testLastNames[i])+1]; strcpy(this->m_lastName, testLastNames[i]); i=rand()%COUNT_STREETS; this->m_street = new char[strlen(testStreets[i])+1]; strcpy(this->m_street, testStreets[i]); i=rand()%COUNT_CITIES; this->m_city = new char[strlen(testCities[i])+1]; strcpy(this->m_city, testCities[i]); i=rand()%COUNT_STATES; this->m_state = new char[strlen(testStates[i])+1]; strcpy(this->m_state, testStates[i]); i=rand()%COUNT_ZIPS; this->m_zip = new char[strlen(testZips[i])+1]; strcpy(this->m_zip, testZips[i]); this->m_age = rand()%50 + 17; this->m_next = NULL; } else if ('i' == ch || 'I' == ch) { } else { //do not replace it with init() function this->m_firstName = NULL; this->m_lastName = NULL; this->m_street = NULL; this->m_city = NULL; this->m_state = NULL; this->m_zip = NULL; this->m_age = 0; this->m_next = NULL; } } void testCAddressConstructorValue(void) { cout << "testCAddressConstructorValue\n"; cout << "============================\n"; CAddress a2("John", "Smith", "123 Main St", "Pittsburg", "KS", "66762", 23); cout << a2 << endl; } CAddress::CAddress(char *fn, char *ln, char *sa, char *ct, char *st, char *zp, int age) { m_firstName = new char[strlen(fn) + 1]; strcpy(m_firstName, fn); m_lastName = new char[strlen(ln) + 1]; strcpy(m_lastName, ln); m_street = new char[strlen(sa) + 1]; strcpy(m_street, sa); m_city = new char[strlen(ct) + 1]; strcpy(m_city, ct); m_state = new char[strlen(st) + 1]; strcpy(m_state, st); m_zip = new char[strlen(zp) + 1]; strcpy(m_zip, zp); this->m_age = age; this->m_next = NULL; } ostream & operator << (ostream &bob, const CAddress &a) { bob << "First Name: "; if (a.m_firstName != NULL) bob << a.m_firstName; bob << "\n Last Name: "; if (a.m_lastName != NULL) bob << a.m_lastName; bob << "\n Street: "; if (a.m_street != NULL) bob << a.m_street; bob << "\n City: "; if (a.m_city != NULL) bob << a.m_city; bob << "\n State: "; if (a.m_state != NULL) bob << a.m_state; bob << "\n Zip: "; if (a.m_zip != NULL) bob << a.m_zip; bob << "\n Age: "; if (a.m_age != 0) bob << a.m_age; bob << endl; return bob; } void testCAddressConstructorDefault(void) { cout << "testCAddressConstructorDefault\n"; cout << "==============================\n"; CAddress a1; cout << a1 << endl; } CAddress::CAddress(void) { this->m_firstName = NULL; this->m_lastName = NULL; this->m_street = NULL; this->m_city = NULL; this->m_state = NULL; this->m_zip = NULL; this->m_age = 0; this->m_next = NULL; } /*OUTPUT: */ |