Telephone

zhaozj2021-02-16  49

Include #include #include

Using namespace std;

Class phoneBookenTry {public: typef unsigned long phoneenumber; typedef char const * namestring

PhoneBookenTry (NameString Person, Phonenumber Televhone); ~ PhoneBookentry (void) {}

// // bit-wise copy is fine // // PhoneBookenTry (PhoneBookenT &); // PhoneBookenTry Operator = (PhoneBookentry Const &);

Void setName (NameString name); NameString const GetName (void) const;

Void setphoneEnumber (Phonenumber Telephone); Phonenumber Const GetphoneNumber (void) Const;

Friend Ostream & Operator << (Ostream & Out, PhoneBookentry Const & Entry); Friend Bool Operator == (PhoneBookentry Const & RHS);

PRIVATE: Static int const Max_name_len = 60;

Phonenumber mphonenumber; char mPersonname [MAX_NAME_LEN];

PhoneBookenTry :: PhoneBooken, phonenumber telephone: mphonenumber (telephone) {strncpy (mPersonname, Person, max_name_len);}

Void PhoneBookenTry :: SetName (NameString name) {strncpy (mPersonname, name, max_name_len);

PhoneBookenTry :: NameString const phonebookentry :: getname (void) const {return mPersonname;}

Void PhoneBookenTry :: setphone telephone {mphonenumber = telephone;}

PhoneBookenTry :: Phonenumber Const PhoneBookentry :: getPhonenumber (void) const {return mphonenumber;}

Bool Operator == (PhoneBookenTry Const & r Hs) {Return (0 == Strcmp (lhs.mpersonname, rhs.mpersonname) && lhs.mphonenumber == rhs.mphonenumber);}

Ostream & Operator << (Ostream & Out, PhoneBookenTREAM & Entry) {OUT << "Name:" Entry.mPersonname << '/ T' << "Phone Number:" << Entry. Mphonenumber; Return Out;

Template Class List (); ~ List (); Void Add (T Const &);

// t * namefind (char * name); // t * telfind (unsigned int TL); // void printlist ();

Protected: struct node {node * next; t value;

Node (Node * anode, t avalue): Next (anode), value (avalue) {} Virtual ~ node () {}}; node * mfirst;

Virtual Node * Create (Node * Next, T Avalue); Virtual Void Delete (Node * Node);

Template List :: List (): mfirst (0) {}

Template List :: ~ List () {node * p = mfirst; while (mfirst) {mfirst = mfirst-> next; delete (p); p = mfirst;}}}

Template Void List :: Add (t const & value) {mfirst = crete (mfirst, value);

Template :: Node * List :: Create (Node * Next, T Avalue) {Return New Node (Next, Avalue);

Template Void List :: Delete (Node * Node) {delete node;}

Class Phonebook: Public List {public: typedef phonebookentry :: phonenumber phonebookentry :: namestring namestring;

PhoneBookenTry * const; phonebookentry * const findbyphonenumber (phonenumber telephone) const;

Friend Ostream & Operator << (Ostream & Out, Phonebook Const & Entry);

}

PhoneBookentry * const phonebook :: findbyName (NameString Person) const {node * p = mfirst; while (p) {if (0 == strcmp (person, p-> value-> getname ())) {Return P-> Value } P = p-> next;} returnom * const phonebook :: findbyphonumber (phonenumber telephone) const {node * p = mfirst; while (p) {if (telephone == p-> value-> getphonenumber ()) {RETURN P-> Value;} p = p-> next;} return 0;

Ostream & Operator << (Ostream & Out, Phonebook Const & Entry) {Phonebook :: Node * P = entry.mfirst; OUT << "--- Phonebook Begin - << endl;

While (p) {cout << * (p-> value) << endl; p = p-> next;} out << "--- phonebook end --- << endl; return out;}

Void test1 () {cout << Endl << "!!!!!!! TEST 1 !!!!!!" << endl; phonebook aphonebook;

Cout << Aphonebook;

Assert (0 == aphonebook.findbyName)); assert (0 == Aphonebook.findbyphonenumber (85424161));}

Void test2 () {cout << Endl << "!!!!!!! TEST 2 !!!!!!!" << Endl; Phonebook Aphonebook;

PhoneBookenTry CC ("Yang Wen", 85424161); APHONEBOOK.ADD (& CC); COUT << Aphonebook;

Assert (cc == * aphonebook.findbyname ("Yang Wen"));

Assert (& cc == Aphonebook.findbyname)); cout << * Aphonebook.FindByphonenumber (85424161) << endl;}

Void test3 () {cout << Endl << "!!!!!!! TEST 3 !!!!!!!" << endl; phonebook aphonebook;

PhoneBookEntry a1 = PhoneBookEntry ( "Yang", 85424161); PhoneBookEntry a2 = PhoneBookEntry ( "Joe Smith", 85424161); PhoneBookEntry a3 = PhoneBookEntry ( "John Doe", 85424161); aPhoneBook.Add (& a1); aPhoneBook.Add ( & a2); APHONEBOOK.ADD (& A3);

Cout << Aphonebook;

Assert (A3 == * Aphonebook.FindbyName ("Li Si")); Assert (& A3 == Aphonebook.FindbyName));

Cout << * Aphonebook.FindByphonenumber (85424161) << endl;

Void main () {TEST1 (); test2 (); test3 ();

The function is incomplete.

转载请注明原文地址:https://www.9cbs.com/read-15618.html

New Post(0)