home *** CD-ROM | disk | FTP | other *** search
- // \EXAMPLES\EX10013.CPP
- //--------------------------------------------------------------
- // member functions of the PhoneBk class
- //--------------------------------------------------------------
-
- // files in this example:
- // %F,15,EX10011.H%EX10011.H definition of the class String
- // %F,15,EX10012.H%EX10012.H definition of the classes
- // PhoneNum, List, and ListIter
- // %F,15,EX10013.H%EX10013.H definition of the class PhoneBk
- // %F,15,EX10011.CPP%EX10011.CPP member functions of the class String
- // %F,15,EX10012.CPP%EX10012.CPP member functions of the classes
- // PhoneNum, List and ListIter
- // EX10013.CPP this file
- // %F,15,EX1001.CPP%EX1001.CPP main() to exercise this class
-
-
- #include "EX10013.h"
- #include <iostream.h>
-
- //--------------------------------------------------------------
- // The function insert() adds or changes an entry in a phonebook
- // PARAMETERS: char* who the name of the new entry- if
- // already in the book, change number
- // long telnum then new telephone number
- // RETURNS: int 1 success
- // int 0 failure, phonebook full
- //--------------------------------------------------------------
-
- int PhoneBk::insert( char* who, long telnum)
- // look at each name entry in the book to see if its matches who
- { ListIter next(list);
- PhoneNum* entry;
- while ( (entry = next() ) != NULL )
- {
- if( entry->compare( who ) > 0 )
- break;
- // If name matched, set the associated telephone number = telnum
- if( entry->compare( who ) == 0 )
- { entry->change(telnum);
- return ( 1 ); }
- }
- if ( list.insert( who, telnum, next) == NULL )
- return ( 0 );
- return ( 1 );
- }
-
-
- //--------------------------------------------------------------
- // The function remove() deletes an entry from a PhoneBk object
- // PARAMETERS: char* who the name of the entry to delete
- // RETURNS: int 1 success
- // int 0 failure, name not in phonebook
- //--------------------------------------------------------------
-
- int PhoneBk::remove( char* who)
- // look at each name entry in the book to see if its matches who
- { ListIter next(list);
- PhoneNum* entry;
- while ( ( entry = next() ) != NULL )
- {
- if( entry->compare( who ) > 0 )
- break;
- if( entry->compare( who ) == 0 )
- { list.del(entry, next);
- return ( 1 );
- }
- }
- return ( 0 );
- }
-
-
- //--------------------------------------------------------------
- // The function lookup() finds a name in a PhoneBk object
- // and prints associated telephone number
- // PARAMETERS: char* who the name of the entry
- // RETURNS: long !=0 success, the telephone number
- // long 0 failure, name not in phonebook
- //--------------------------------------------------------------
-
- long PhoneBk::lookup( char* who)
- // look at each name entry in the book to see if its matches who
- { ListIter next(list);
- PhoneNum* entry;
- while ( ( entry = next() ) != NULL )
- {
- if( entry->compare( who ) > 0 )
- break;
- if( entry->compare( who ) == 0 )
- return entry->gettel();
- }
- return ( 0 );
- }
-
-
- //--------------------------------------------------------------
- // The function print() lists every name and telephone number
- // PARAMETERS: ostream& os the output stream
- // RETURNS: ostream& os the output stream
- //--------------------------------------------------------------
-
- ostream& PhoneBk::print (ostream& os)
- // consider every entry in turn
- { ListIter next(list);
- PhoneNum* entry;
- while ( ( entry = next() ) != NULL )
- entry->print(os);
- os << "EOF" << endl;
- return(os);
- }
-
-
-