home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 17.ddi / TVDOCDEM.ZIP / TVGUID18.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  3.7 KB  |  147 lines

  1. /*---------------------------------------------------------*/
  2. /*                                                         */
  3. /*   Turbo Vision 1.0                                      */
  4. /*   TVGUID18 Demo Source File                             */
  5. /*   Copyright (c) 1991 by Borland International           */
  6. /*                                                         */
  7. /*---------------------------------------------------------*/
  8.  
  9. #include <iostream.h>           // for cout, <<
  10. #include <iomanip.h>            // for setw
  11. #include <string.h>             // for strncmp
  12.  
  13. #define Uses_TNSSortedCollection
  14. #include <tv.h>
  15.  
  16. struct TClient : public TObject
  17. {
  18.     // all members public by default
  19.  
  20.     const char *account;
  21.     const char *name;
  22.     const char *phone;
  23.  
  24.     TClient( char *newAccount, char *newName, char *newPhone);
  25.     ~TClient();
  26.  
  27. };
  28.  
  29. TClient::TClient( char *newAccount, char *newName, char *newPhone)
  30. {
  31.     account = newStr( newAccount );
  32.     name = newStr( newName );
  33.     phone = newStr( newPhone );
  34. };
  35.  
  36. TClient::~TClient()
  37. {
  38.     delete (char *)account;
  39.     delete (char *)name;
  40.     delete (char *)phone;
  41. }
  42.  
  43. class TSortedClientCollection : public TNSSortedCollection
  44. {
  45.  
  46. public:
  47.  
  48.     TSortedClientCollection (ccIndex aLimit, ccIndex aDelta ) :
  49.         TNSSortedCollection( aLimit, aDelta ) {}
  50.     virtual ccIndex insert( TClient *tc )
  51.         { return TNSSortedCollection::insert( tc ); }
  52.         // overrides TNSCollection::insert( void *item )
  53.         // In a more complete application, you would also override
  54.         // at, indexOf, remove, free,... so that they all
  55.         // handle TClient pointers
  56.  
  57.     virtual void *keyOf(void *item);
  58.  
  59.     void printAll();
  60.     void searchArea( char *areaToFind );
  61.  
  62. private:
  63.  
  64.     virtual int compare( void *key1, void *key2);
  65.     virtual void freeItem( void *item );
  66.  
  67. };
  68.  
  69. void *TSortedClientCollection::keyOf(void *item)
  70. {
  71.  
  72.     return (void *)((TClient*)item)->name;
  73. }
  74.  
  75. int TSortedClientCollection::compare(void *key1, void *key2)
  76. {
  77.    return strcmp ( (char *)(key1), (char *)(key2) );
  78. }
  79.  
  80. static void print( void *c, void * )
  81. {
  82.     TClient *tc = (TClient *)c;
  83.     cout << setiosflags( ios::left )
  84.          << setw(10) << tc->account
  85.          << setw(20) << tc->name
  86.          << setw(16) << tc->phone
  87.          << endl;
  88. }
  89.  
  90. void TSortedClientCollection::printAll()  // print info for all clients
  91. {
  92.     cout << endl << "Client List:" << endl;
  93.     forEach( &print, 0 ); // call print for each client
  94. }
  95.  
  96. void TSortedClientCollection::freeItem( void *item )
  97. {
  98.     delete ( TClient * ) item;
  99. }
  100.  
  101. Boolean areaMatch( void *c, void *ph )
  102. {
  103.     char *areaToFind = (char *)ph;
  104.     TClient *tc = (TClient *)c;
  105.     if( strncmp( areaToFind, tc->phone, 5 ) == 0 )
  106.         return True;
  107.     else
  108.         return False;
  109. }
  110.  
  111. void TSortedClientCollection::searchArea( char *areaToFind )
  112. {
  113.     TClient *foundClient =
  114.         (TClient *)firstThat( &areaMatch, areaToFind );
  115.     if( !foundClient )
  116.         cout << "No client met the search requirement" << endl;
  117.     else
  118.         {
  119.         cout << "Found client:" << endl;
  120.         print( foundClient, 0 );
  121.         }
  122. }
  123.  
  124. int main()
  125. {
  126.  
  127.     TSortedClientCollection clientList( 50, 10 );
  128.     // limit is 50, delta is 10
  129.  
  130.     clientList.insert( new TClient("90-167", "Smith, Zelda",
  131.                "(800) 555-1212" ));
  132.     clientList.insert( new TClient("90-160", "Johnson, Agatha",
  133.                "(302) 139-8913" ));
  134.     clientList.insert( new TClient("90-177", "Smitty, John",
  135.                "(406) 987-4321"));
  136.     clientList.insert( new TClient("91-100", "Anders, Smitty",
  137.                "(406) 111-2222"));
  138.  
  139.     clientList.printAll();
  140.     clientList.searchArea( "(406)" );
  141.     clientList.freeAll();
  142.     return 0;
  143.  
  144. };
  145.  
  146.  
  147.