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

  1. // \EXAMPLES\EX08052.CPP
  2. //  member functions of the classes PhoneNum, List, and PhoneBk
  3. //--------------------------------------------------------------
  4.  
  5. //  files in this example:
  6. // %F,15,EX08051.H%EX08051.H       definition of class String
  7. // %F,15,EX08052.H%EX08052.H       definition of classes PhoneBk,
  8. //              PhoneNum and List
  9. // %F,15,EX08051.CPP%EX08051.CPP     member functions of String
  10. // EX08052.CPP     this file
  11. // %F,15,EX0805.CPP%EX0805.CPP   main() function for PhoneBk class
  12.  
  13. //--------------------------------------------------------------
  14. #include "EX08052.H"
  15.  
  16. //--------------------------------------------------------------
  17. // member functions of the List class
  18. //--------------------------------------------------------------
  19.  
  20. List::List(int maxsize) : last(0), current(-1)
  21. {  list = new PhoneNum[maxsize];
  22. }
  23.  
  24. List::~List()
  25. { delete[] list;
  26. }
  27.  
  28. void List::gototop()
  29. { current = -1;
  30. }
  31.  
  32. void List::add( PhoneNum& entry)
  33. { list[last++] = entry;
  34. }
  35.  
  36. void List::del()
  37. { if( current < --last )
  38.     list[current] =list[last];
  39. }
  40.  
  41. PhoneNum* List::next()
  42. {  if ( current+1 < last ) return &list[++current];
  43.    else return &list[current=0];
  44. }
  45.  
  46. //--------------------------------------------------------------
  47. // member functions of the PhoneNum class
  48. //--------------------------------------------------------------
  49.  
  50. void PhoneNum::enter ( char* n, long tn )
  51. { name.append(n);
  52.   tel = tn;
  53. }
  54.  
  55. ostream& PhoneNum::print(ostream& os)
  56. { name.print(os)
  57.      << "\t"
  58.      << tel
  59.      << "\t"
  60.      << endl;
  61.    return os;
  62. }
  63.  
  64. //--------------------------------------------------------------
  65. // member functions of the PhoneBk class
  66. //--------------------------------------------------------------
  67.  
  68. //--------------------------------------------------------------
  69. // The function insert() adds or changes an entry in a phonebook
  70. // PARAMETERS:  char* who     the name of the new entry- if
  71. //                            already in the book, change number
  72. //              long  telnum  then new telephone number
  73. // RETURNS:     int   1       success
  74. //              int   0       failure,  phonebook full
  75. //--------------------------------------------------------------
  76. PhoneBk::PhoneBk() : numEntry (0), maxsize (100), list (100)
  77. { };
  78.  
  79. int PhoneBk::insert( char* who, long telnum)
  80. // look at each name entry in the book to see if its matches who
  81. { list.gototop();
  82.   for( int i = 0; i < numEntry; i++)
  83.     { PhoneNum* entry = list.next();
  84.       if( entry->compare(who) )
  85.         continue;
  86. // If name matched, set the associated telephone number = telnum
  87.     entry->change(telnum);
  88.     return (1);
  89.   }
  90. // If the book is full, new name and number can not be added
  91.   if( numEntry == 100 )
  92.     return ( 0 );
  93. // Add new netry
  94.   PhoneNum newentry( who, telnum);
  95.   list.add( newentry);
  96.   numEntry++;
  97.   return ( 1 );
  98. };
  99.  
  100. //--------------------------------------------------------------
  101. // The function remove() deletes an entry from a PhoneBk object
  102. // PARAMETERS:  char* who    the name of the entry to delete
  103. // RETURNS:     int   1      success
  104. //              int   0      failure,  name not in phonebook
  105. //--------------------------------------------------------------
  106. int PhoneBk::remove( char* who)
  107. // look at each name entry in the book to see if its matches who
  108. { list.gototop();
  109.   for( int i = 0; i < numEntry; i++)
  110.   { PhoneNum* entry = list.next();
  111.     if( entry->compare(who) )
  112.       continue;
  113. // entry found, delete it
  114.     list.del();
  115.     numEntry--;
  116.     return(1);
  117.   };
  118. // if name not found in book, return failure code
  119.   return(0);
  120. };
  121.  
  122. //--------------------------------------------------------------
  123. // The function lookup() finds a name in a PhoneBk object
  124. //                       and prints associated telephone number
  125. // PARAMETERS:  char* who     the name of the entry
  126. // RETURNS:     long  !=0     success, the telephone number
  127. //              long  0       failure,  name not in phonebook
  128. //--------------------------------------------------------------
  129. long PhoneBk::lookup( char* who)
  130. // look at each name entry in the book to see if its matches who
  131. { list.gototop();
  132.   for( int i = 0; i < numEntry;  i++)
  133.   { PhoneNum* entry = list.next();
  134.     if( entry->compare(who) == 0 )
  135. // return telephone number associated with matching name
  136.        return ( entry->gettel());
  137.   }
  138. // if name not found in book, return failure code
  139.   return ( 0 );
  140. };
  141.  
  142. //--------------------------------------------------------------
  143. // The function print() lists every name and telephone number
  144. // PARAMETERS:  ostream& os      the output stream
  145. // RETURNS:     ostream& os      the output stream
  146. //--------------------------------------------------------------
  147. ostream& PhoneBk::print (ostream& os)
  148. // consider every entry in turn
  149. { list.gototop();
  150.   for( int i = 0; i < numEntry;  i++)
  151.   { PhoneNum* entry = list.next();
  152. // display name, tab for formatting, telephone number, newline
  153.     entry->print(os);
  154.    }
  155.    os << "EOF" << endl;
  156.    return(os);
  157. };
  158. //--------------------------------------------------------------
  159.