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

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