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

  1. /*---------------------------------------------------------*/
  2. /*                                                         */
  3. /*   Turbo Vision 1.0                                      */
  4. /*   TVGUID19.CPP Demo Source File                         */
  5. /*   Copyright (c) 1991 by Borland International           */
  6. /*                                                         */
  7. /*---------------------------------------------------------*/
  8.  
  9. /*
  10.    Read a line and add each unique word to a sorted
  11.    string collection. Use the forEach iterator to
  12.    traverse the collection and print out each word.
  13. */
  14.  
  15. #include <stdio.h>              // temp for gets
  16. #include <stdlib.h>             // for exit()
  17. #include <iostream.h>           // for << etc
  18. #include <string.h>             // for strlen etc
  19. #include <ctype.h>              // for ispunct
  20.  
  21. #define Uses_TStringCollection
  22. #include <tv.h>
  23.  
  24. const int maxLine = 81;
  25. char line[maxLine];
  26.  
  27. class TWordCollection : public TStringCollection
  28. {
  29.  
  30. public:
  31.  
  32.     TWordCollection( short aLimit, short aDelta ) :
  33.        TStringCollection( aLimit, aDelta) {}
  34.     virtual void print();
  35. };
  36.  
  37. /* Iterator */
  38. static void printWord( void *w, void * )
  39. {
  40.     char *s = (char *)w;
  41.     cout << s << endl;
  42. }
  43.  
  44. void TWordCollection::print()
  45. {
  46.     forEach( &printWord, 0 );
  47. }
  48.  
  49. // Enter a line of text, extract words and insert them in coll
  50. static void insertWord( char *line, TStringCollection *sc )
  51. {
  52.     char *curr;
  53.     char *first;
  54.  
  55.     char wstr[maxLine];
  56.     char ch = EOS;
  57.     Boolean inword;
  58.  
  59.     curr = first = line;
  60.     inword = False;
  61.  
  62.     while( (ch = *curr) != EOS )
  63.         {
  64.         if( !isalnum( ch ) && inword )
  65.             {
  66.             inword = False;
  67.             strncpy( wstr, first, size_t(curr - first) );
  68.             wstr[size_t(curr-first)]=EOS;
  69.             sc->insert( newStr( wstr ) );
  70.             }
  71.         else if( isalnum( ch ) && !inword )
  72.             {
  73.             inword = True;
  74.             first = curr;
  75.             }
  76.         curr++;
  77.         }
  78.  
  79.     if( inword )
  80.         // grab last word if final char before null is alfa-num
  81.         {
  82.         strncpy( wstr, first, size_t(curr - first) );
  83.         wstr[size_t(curr-first)]=EOS;
  84.         sc->insert( newStr( wstr ));
  85.         }
  86. }
  87.  
  88. int main()
  89. {
  90.     TWordCollection *wordCollection = new TWordCollection( 20, 5 );
  91.     // in production code, check lowMemory here
  92.  
  93.     cout << "Enter a line of text: " << endl;
  94.     cin.get( line, maxLine, '\n' );
  95.  
  96.     // parse line, insert unique words into sorted collection
  97.     insertWord( line, wordCollection );
  98.     if( wordCollection->getCount() > 0 )
  99.         {
  100.         wordCollection->print();
  101.         cout << "Total word count = " << wordCollection->getCount() << endl;
  102.         }
  103.     else
  104.         cout << "No words in WordCollection!" << endl;
  105.  
  106.     return 0;
  107. }
  108.  
  109.