home *** CD-ROM | disk | FTP | other *** search
/ Microsoftware Monthly 19…2 Programming Power Tools / MASO9512.ISO / cpptutor / cpptutor.arj / EXAMPLES / EX10013.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-25  |  3.8 KB  |  113 lines

  1. // \EXAMPLES\EX10013.CPP
  2. //--------------------------------------------------------------
  3. // member functions of the PhoneBk class
  4. //--------------------------------------------------------------
  5.  
  6. //  files in this example:
  7. // %F,15,EX10011.H%EX10011.H       definition of the class String
  8. // %F,15,EX10012.H%EX10012.H       definition of the classes
  9. //                                 PhoneNum, List, and ListIter
  10. // %F,15,EX10013.H%EX10013.H       definition of the class PhoneBk
  11. // %F,15,EX10011.CPP%EX10011.CPP   member functions of the class String
  12. // %F,15,EX10012.CPP%EX10012.CPP   member functions of the classes
  13. //                                 PhoneNum, List and ListIter
  14. // EX10013.CPP   this file
  15. // %F,15,EX1001.CPP%EX1001.CPP      main() to exercise this class
  16.  
  17.  
  18. #include "EX10013.h"
  19. #include <iostream.h>
  20.  
  21. //--------------------------------------------------------------
  22. // The function insert() adds or changes an entry in a phonebook
  23. // PARAMETERS:  char* who     the name of the new entry- if
  24. //                            already in the book, change number
  25. //              long  telnum  then new telephone number
  26. // RETURNS:     int   1       success
  27. //              int   0       failure,  phonebook full
  28. //--------------------------------------------------------------
  29.  
  30. int PhoneBk::insert( char* who, long telnum)
  31. // look at each name entry in the book to see if its matches who
  32. { ListIter next(list);
  33.   PhoneNum* entry;
  34.   while ( (entry  = next() ) != NULL )
  35.   {
  36.     if( entry->compare( who ) > 0 )
  37.       break;
  38. // If name matched, set the associated telephone number = telnum
  39.     if( entry->compare( who ) == 0 )
  40.     { entry->change(telnum);
  41.       return ( 1 ); }
  42.   }
  43.   if ( list.insert( who, telnum, next) == NULL )
  44.     return ( 0 );
  45.   return ( 1 );
  46. }
  47.  
  48.  
  49. //--------------------------------------------------------------
  50. // The function remove() deletes an entry from a PhoneBk object
  51. // PARAMETERS:  char* who    the name of the entry to delete
  52. // RETURNS:     int   1      success
  53. //              int   0      failure,  name not in phonebook
  54. //--------------------------------------------------------------
  55.  
  56. int PhoneBk::remove( char* who)
  57. // look at each name entry in the book to see if its matches who
  58. { ListIter next(list);
  59.   PhoneNum* entry;
  60.   while ( ( entry  = next() ) != NULL )
  61.   {
  62.     if( entry->compare( who ) > 0 )
  63.       break;
  64.     if( entry->compare( who ) == 0 )
  65.     { list.del(entry, next);
  66.       return ( 1 );
  67.     }
  68.   }
  69.   return ( 0 );
  70. }
  71.  
  72.  
  73. //--------------------------------------------------------------
  74. // The function lookup() finds a name in a PhoneBk object
  75. //                       and prints associated telephone number
  76. // PARAMETERS:  char* who     the name of the entry
  77. // RETURNS:     long  !=0     success, the telephone number
  78. //              long  0       failure,  name not in phonebook
  79. //--------------------------------------------------------------
  80.  
  81. long PhoneBk::lookup( char* who)
  82. // look at each name entry in the book to see if its matches who
  83. { ListIter next(list);
  84.   PhoneNum* entry;
  85.   while ( ( entry  = next() ) != NULL )
  86.   {
  87.     if( entry->compare( who ) > 0 )
  88.       break;
  89.     if( entry->compare( who ) == 0 )
  90.       return entry->gettel();
  91.   }
  92.   return ( 0 );
  93. }
  94.  
  95.  
  96. //--------------------------------------------------------------
  97. // The function print() lists every name and telephone number
  98. // PARAMETERS:  ostream& os      the output stream
  99. // RETURNS:     ostream& os      the output stream
  100. //--------------------------------------------------------------
  101.  
  102. ostream& PhoneBk::print (ostream& os)
  103. // consider every entry in turn
  104. { ListIter next(list);
  105.   PhoneNum* entry;
  106.   while ( ( entry  = next() ) != NULL )
  107.     entry->print(os);
  108.    os << "EOF" << endl;
  109.    return(os);
  110. }
  111.  
  112.  
  113.