home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 May / Chip_2002-05_cd1.bin / chplus / cpp / 3 / stl.exe / tele.cpp < prev    next >
C/C++ Source or Header  |  1998-02-09  |  6KB  |  182 lines

  1. #include "stlexam.h"
  2. #pragma hdrstop
  3. /**************************************************************************
  4.  *
  5.  * tele.cpp - telephone directory sample program, from section 9.3.1
  6.  *
  7.  * $Id: tele.cpp,v 1.14 1996/08/28 01:19:25 smithey Exp $
  8.  *
  9.  ***************************************************************************
  10.  *
  11.  * (c) Copyright 1994, 1995 Rogue Wave Software, Inc.
  12.  * ALL RIGHTS RESERVED *
  13.  * The software and information contained herein are proprietary to, and
  14.  * comprise valuable trade secrets of, Rogue Wave Software, Inc., which
  15.  * intends to preserve as trade secrets such software and information.
  16.  * This software is furnished pursuant to a written license agreement and
  17.  * may be used, copied, transmitted, and stored only in accordance with
  18.  * the terms of such license and with the inclusion of the above copyright
  19.  * notice.  This software and information or any other copies thereof may
  20.  * not be provided or otherwise made available to any other person.
  21.  *
  22.  * Notwithstanding any other lease or license that may pertain to, or
  23.  * accompany the delivery of, this computer software and information, the
  24.  * rights of the Government regarding its use, reproduction and disclosure
  25.  * are as set forth in Section 52.227-19 of the FARS Computer
  26.  * Software-Restricted Rights clause.
  27.  * 
  28.  * Use, duplication, or disclosure by the Government is subject to
  29.  * restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in
  30.  * Technical Data and Computer Software clause at DFARS 252.227-7013.
  31.  * Contractor/Manufacturer is Rogue Wave Software, Inc.,
  32.  * P.O. Box 2328, Corvallis, Oregon 97339.
  33.  *
  34.  * This computer software and information is distributed with "restricted
  35.  * rights."  Use, duplication or disclosure is subject to restrictions as
  36.  * set forth in NASA FAR SUP 18-52.227-79 (April 1985) "Commercial
  37.  * Computer Software-Restricted Rights (April 1985)."  If the Clause at
  38.  * 18-52.227-74 "Rights in Data General" is specified in the contract,
  39.  * then the "Alternate III" clause applies.
  40.  *
  41.  **************************************************************************/
  42.  
  43. #ifndef _RWSTD_HEADER_REQUIRES_HPP
  44. # include <map>
  45. # include <algorithm>
  46. # include <vector>
  47. #else
  48. # include <map.hpp>
  49. # include <algorithm.hpp>
  50. # include <vector.hpp>
  51. #endif
  52.  
  53. #ifdef _RW_STD_IOSTREAM
  54. #include <iostream>
  55. #else
  56. #include <iostream.h>
  57. #endif
  58.    
  59. #ifndef _RWSTD_HEADER_REQUIRES_HPP
  60. # include <string>
  61. #else
  62. # include <string.hpp>
  63. #endif
  64.  
  65. #ifndef _RWSTD_NO_NAMESPACE
  66.   using namespace std;
  67. #endif
  68.  
  69. typedef map<string, long, less<string>,allocator<string>  > friendMap;
  70. typedef map<long, string, less<long>,allocator<long>  >   sortedMap;
  71.  
  72. //
  73. // Utility functions used in telephone directory.
  74. //
  75.  
  76. typedef friendMap::value_type entry_type;
  77. typedef sortedMap::value_type sorted_entry_type;
  78.  
  79. #ifndef HPPA_WA
  80. void printEntry (const entry_type & entry)
  81. #else
  82. void printEntry (const entry_type entry)
  83. #endif
  84. {
  85.     cout << entry.first << ":" << entry.second << endl;
  86. }
  87.  
  88. #ifndef HPPA_WA
  89. void printSortedEntry (const sorted_entry_type & entry)
  90. #else
  91. void printSortedEntry (const sorted_entry_type entry)
  92. #endif 
  93. {
  94.     cout << entry.first << ":" << entry.second << endl;
  95. }
  96.  
  97. int prefix (const entry_type& entry) { return entry.second / 10000; }
  98.  
  99. bool prefixCompare (const entry_type & a, const entry_type & b)
  100. {
  101.     return prefix(a) < prefix(b);
  102. }
  103.     
  104. class checkPrefix
  105. {
  106.   public:
  107.     checkPrefix (int p) : testPrefix(p) { }
  108.     int testPrefix;
  109.     bool operator () (const entry_type& entry)
  110.     {
  111.         return prefix(entry)==testPrefix;
  112.     }
  113. };
  114.             
  115. class telephoneDirectory
  116. {
  117.   public:
  118.     void addEntry (string name, long number) { database[name] = number; }
  119.         
  120.     void remove (string name) { database.erase(name); }
  121.     
  122.     void update (string name, long number)
  123.     {
  124.         remove(name);addEntry(name,number);
  125.     }
  126.         
  127.     void displayDatabase()
  128.     {
  129.         for_each(database.begin(), database.end(), printEntry);
  130.     }
  131.     
  132.     void displayPrefix(int);
  133.     
  134.     void displayByPrefix(); 
  135.     
  136. private:
  137.     friendMap database;
  138. };
  139.  
  140. void telephoneDirectory::displayPrefix (int prefix)
  141. {
  142.     cout << "Listing for prefix " << prefix << endl;
  143.     map<string, long, less<string>,allocator<string>  >::iterator where;
  144.     where = find_if(database.begin(), database.end(), checkPrefix(prefix));
  145.     while (where != database.end())
  146.     {
  147.         printEntry(*where);
  148.         where = find_if(++where, database.end(), checkPrefix(prefix));
  149.     }
  150.     cout << "end of prefix listing" << endl;
  151. }
  152.  
  153. void telephoneDirectory::displayByPrefix ()
  154. {
  155.     cout << "Display by prefix" << endl;
  156.  
  157.     sortedMap sortedData;
  158.     for (friendMap::iterator i = database.begin(); i != database.end(); i++)
  159.         sortedData.insert(sortedMap::value_type((*i).second, (*i).first));
  160.     for_each(sortedData.begin(), sortedData.end(), printSortedEntry);
  161.  
  162.     cout << "end display by prefix" << endl;
  163. }
  164.  
  165. int main ()
  166. {
  167.     cout << "Telephone Directory sample program" << endl;
  168.  
  169.     telephoneDirectory friends;
  170.     friends.addEntry("Samantha", 6342343);
  171.     friends.addEntry("Brenda", 5436546);
  172.     friends.addEntry("Fred", 7435423);
  173.     friends.addEntry("Allen", 6348723);
  174.     friends.displayDatabase();
  175.     friends.displayPrefix(634);
  176.     friends.displayByPrefix();
  177.     
  178.     cout << "End of telephone directory sample program" << endl;
  179.  
  180.     return 0;
  181. }
  182.