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

  1. // \EXAMPLES\EX08032.CPP
  2. //  member function of the PhoneBk class
  3. //--------------------------------------------------------------
  4.  
  5. //  files in this example:
  6. // %F,15,EX08031.H%EX08031.H       definition of class String
  7. // %F,15,EX08032.H%EX08032.H       definition of class PhoneBk
  8. // %F,15,EX08031.CPP%EX08031.CPP     member functions of String
  9. // EX08032.CPP     this file
  10. // %F,15,EX0803.CPP%EX0803.CPP   main() to exercise PhoneBk Class
  11.  
  12. //--------------------------------------------------------------
  13. #include "EX08032.H"          // definition of PhoneBk class
  14. #include "EX08031.H"          // definition of String class
  15.  
  16. //--------------------------------------------------------------
  17. // The function insert() adds or changes an entry in a phonebook
  18. // PARAMETERS:  char* who     the name of the new entry- if
  19. //                            already in the book, change number
  20. //              long  telnum  then new telephone number
  21. // RETURNS:     int   1       success
  22. //              int   0       failure,  phonebook full
  23. //--------------------------------------------------------------
  24. int PhoneBk::insert( char* who, long telnum)
  25. // look at each name entry in the book to see if its matches who
  26.   { for( int i = 0; i < numEntry; i++)
  27.     { if( names[i].compare( who ) )
  28.         continue;
  29. // If name matched, set the associated telephone number = telnum
  30.       tel[i] = telnum;
  31.       return ( 1 );
  32.     }
  33. // If the book is full, new name and number can not be added
  34.   if( numEntry == 100 )
  35.     return ( 0 );
  36. // Add new netry
  37.   tel[numEntry] = telnum;             // insert telephone number
  38.   String newname(who);                // convert char* to String
  39.   names[numEntry++] =String(who);        // insert name
  40.   return ( 1 );
  41. };
  42.  
  43. //--------------------------------------------------------------
  44. // The function remove() deletes an entry from a PhoneBk object
  45. // PARAMETERS:  char* who    the name of the entry to delete
  46. // RETURNS:     int   1      success
  47. //              int   0      failure,  name not in phonebook
  48. //--------------------------------------------------------------
  49. int PhoneBk::remove( char* who)
  50. // look at each name entry in the book to see if its matches who
  51. { for( int i = 0; i < numEntry; i++)
  52.    { if( names[i].compare( who ) )
  53.        continue;
  54. // entry found, delete it - do not leave gaps between entries
  55. // is this the only entry?
  56.      if( numEntry > 1 )
  57. // if there are more than one entry, overwrite with last entry
  58.      { tel[i] = tel[--numEntry];
  59.        names [i] = names[numEntry];
  60.      }
  61. // if the entry to delete is the only entry, just reset counter
  62.      else
  63.      { --numEntry;
  64.      }
  65.      return (1);
  66.    }
  67. // if name not found in book, return failure code
  68.    return ( 0 );
  69. };
  70.  
  71. //--------------------------------------------------------------
  72. // The function lookup() finds a name in a PhoneBk object
  73. //                       and prints associated telephone number
  74. // PARAMETERS:  char* who     the name of the entry
  75. // RETURNS:     long  !=0     success, the telephone number
  76. //              long  0       failure,  name not in phonebook
  77. //--------------------------------------------------------------
  78. long PhoneBk::lookup( char* who)
  79. // look at each name entry in the book to see if its matches who
  80. { for( int i = 0; i < numEntry;  i++)
  81.      if( names[i].compare( who ) == 0 )
  82. // return telephone number associated with matching name
  83.        return ( tel [i] );
  84. // if name not found in book, return failure code
  85.    return ( 0 );
  86. };
  87.  
  88. //--------------------------------------------------------------
  89. // The function print() lists every name and telephone number
  90. // PARAMETERS:  ostream& os      the output stream
  91. // RETURNS:     ostream& os      the output stream
  92. //--------------------------------------------------------------
  93. ostream& PhoneBk::print (ostream& os)
  94. // consider every entry in turn
  95. { for( int i = 0; i < numEntry;  i++)
  96. // display name, tab for formatting, telephone number, newline
  97.    names [i].print(os)
  98.       << "\t"
  99.       << tel[i]
  100.       << endl;
  101. // add an end of file message
  102.    os << "EOF"
  103.       << endl;
  104.    return ( os );
  105. };
  106. //--------------------------------------------------------------
  107.