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

  1. /*---------------------------------------------------------*/
  2. /*                                                         */
  3. /*   Turbo Vision 1.0                                      */
  4. /*   TVGUID17.CPP 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_TNSCollection
  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. static void print( void *c, void * )
  44. {
  45.     TClient *tc = (TClient *)c;
  46.     cout << setiosflags( ios::left )
  47.          << setw(10) << tc->account
  48.          << setw(20) << tc->name
  49.          << setw(16) << tc->phone
  50.          << endl;
  51. }
  52.  
  53. void printAll( TNSCollection *c )  // print info for all clients
  54. {
  55.     cout << endl << "Client List:" << endl;
  56.     c->forEach( &print, 0 ); // call print for each client
  57. }
  58.  
  59. Boolean areaMatch( void *c, void *ph )
  60. {
  61.     char *areaToFind = (char *)ph;
  62.     TClient *tc = (TClient *)c;
  63.  
  64.     // seek match in first 5 chars: "(xxx)"
  65.     if( strncmp( areaToFind, tc->phone, 5 ) == 0 )
  66.         return True;
  67.     else
  68.         return False;
  69. }
  70.  
  71. void searchArea( TNSCollection *c, char *areaToFind )
  72. {
  73.     TClient *foundClient =
  74.         (TClient *)(c->firstThat( &areaMatch, areaToFind ));
  75.     if( !foundClient )
  76.         cout << "No client met the search requirement" << endl;
  77.     else
  78.         {
  79.         cout << "Found client:" << endl;
  80.         print( foundClient, 0 );
  81.         }
  82. }
  83.  
  84. int main()
  85. {
  86.     TNSCollection clientList( 50, 10 );    // limit is 50, delta is 10
  87.  
  88.     clientList.insert( new TClient("90-167", "Smith, Zelda",
  89.                "(800) 555-1212" ));
  90.     clientList.insert( new TClient("90-160", "Johnson, Agatha",
  91.                "(302) 139-8913" ));
  92.     clientList.insert( new TClient("90-177", "Smitty, John",
  93.                "(406) 987-4321"));
  94.     clientList.insert( new TClient("91-100", "Anders, Smitty",
  95.                "(406) 111-2222"));
  96.  
  97.     printAll( &clientList );
  98.     searchArea( &clientList, "(406)" );
  99.     return 0;
  100. };
  101.  
  102.  
  103.