home *** CD-ROM | disk | FTP | other *** search
- // \EXAMPLES\EX08052.CPP
- // member functions of the classes PhoneNum, List, and PhoneBk
- //--------------------------------------------------------------
-
- // files in this example:
- // %F,15,EX08051.H%EX08051.H definition of class String
- // %F,15,EX08052.H%EX08052.H definition of classes PhoneBk,
- // PhoneNum and List
- // %F,15,EX08051.CPP%EX08051.CPP member functions of String
- // EX08052.CPP this file
- // %F,15,EX0805.CPP%EX0805.CPP main() function for PhoneBk class
-
- //--------------------------------------------------------------
- #include "EX08052.H"
-
- //--------------------------------------------------------------
- // member functions of the List class
- //--------------------------------------------------------------
-
- List::List(int maxsize) : last(0), current(-1)
- { list = new PhoneNum[maxsize];
- }
-
- List::~List()
- { delete[] list;
- }
-
- void List::gototop()
- { current = -1;
- }
-
- void List::add( PhoneNum& entry)
- { list[last++] = entry;
- }
-
- void List::del()
- { if( current < --last )
- list[current] =list[last];
- }
-
- PhoneNum* List::next()
- { if ( current+1 < last ) return &list[++current];
- else return &list[current=0];
- }
-
- //--------------------------------------------------------------
- // member functions of the PhoneNum class
- //--------------------------------------------------------------
-
- void PhoneNum::enter ( char* n, long tn )
- { name.append(n);
- tel = tn;
- }
-
- ostream& PhoneNum::print(ostream& os)
- { name.print(os)
- << "\t"
- << tel
- << "\t"
- << endl;
- return os;
- }
-
- //--------------------------------------------------------------
- // member functions of the PhoneBk class
- //--------------------------------------------------------------
-
- //--------------------------------------------------------------
- // 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
- //--------------------------------------------------------------
- PhoneBk::PhoneBk() : numEntry (0), maxsize (100), list (100)
- { };
-
- int PhoneBk::insert( char* who, long telnum)
- // look at each name entry in the book to see if its matches who
- { list.gototop();
- for( int i = 0; i < numEntry; i++)
- { PhoneNum* entry = list.next();
- if( entry->compare(who) )
- continue;
- // If name matched, set the associated telephone number = telnum
- entry->change(telnum);
- return (1);
- }
- // If the book is full, new name and number can not be added
- if( numEntry == 100 )
- return ( 0 );
- // Add new netry
- PhoneNum newentry( who, telnum);
- list.add( newentry);
- numEntry++;
- 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
- { list.gototop();
- for( int i = 0; i < numEntry; i++)
- { PhoneNum* entry = list.next();
- if( entry->compare(who) )
- continue;
- // entry found, delete it
- list.del();
- 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
- { list.gototop();
- for( int i = 0; i < numEntry; i++)
- { PhoneNum* entry = list.next();
- if( entry->compare(who) == 0 )
- // return telephone number associated with matching name
- return ( entry->gettel());
- }
- // 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
- { list.gototop();
- for( int i = 0; i < numEntry; i++)
- { PhoneNum* entry = list.next();
- // display name, tab for formatting, telephone number, newline
- entry->print(os);
- }
- os << "EOF" << endl;
- return(os);
- };
- //--------------------------------------------------------------
-