home *** CD-ROM | disk | FTP | other *** search
- // \EXAMPLES\EX0511.CPP
- // the prototype implementation of a phone book class
- // member function definitions for PhoneBk class
-
- //--------------------------------------------------------------
- // files in this example:
- // %F,15,EX0501.H%EX0501.H definition of the class PhoneBk
- // EX05011.CPP this file -- member functions of PhoneBk
- // %F,15,EX05012.CPP%EX05012.CPP main() to exercise this class
- //--------------------------------------------------------------
-
- #include "EX0501.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
- { for( int i = 0; i < numEntry; i++)
- { if( strcmp( (char*) &names [i], who ) )
- continue;
- // If name matched, set the associated telephone number = telnum
- tel[i] = telnum;
- return ( 1 );
- }
- // If the book is full, new name and number can not be added
- if( numEntry == 100 )
- return ( 0 );
- // Add new netry
- tel[numEntry] = telnum;
- strcpy( (char*) &names [numEntry++], who );
- 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
- { for( int i = 0; i < numEntry; i++)
- { if( strcmp( (char*) &names [i], who ) )
- continue;
- // entry found, delete it - do not leave gaps between entries
- // is this the only entry?
- if( numEntry > 1 )
- // if there are more than one entry, overwrite with last entry
- { tel[i] = tel[--numEntry];
- strcpy( (char*) &names [i], (char*) &names[numEntry] );
- return ( 1 );
- }
- // if the entry to delete is the only entry, just reset counter
- else
- { --numEntry;
- return ( 1 );
- }
- }
- // if name not found in book, return failure code
- 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
- { for( int i = 0; i < numEntry; i++)
- if( strcmp( (char*) &names [i], who ) == 0 )
- // return telephone number associated with matching name
- return ( tel [i] );
- // if name not found in book, return failure code
- 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
- { for( int i = 0; i < numEntry; i++)
- // display name, tab for formatting, telephone number, newline
- os << (char*) &names [i]
- << "\t"
- << tel[i]
- << endl;
- // add an end of file message
- os << "EOF"
- << endl;
- return ( os );
- };
-