home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cset21v1.zip / IBMCPP / SAMPLES / ICLCC / WORDBAG.C < prev    next >
C/C++ Source or Header  |  1993-05-07  |  3KB  |  73 lines

  1. /******************************************************************************/
  2. /*                                                                            */
  3. /* COPYRIGHT:                                                                 */
  4. /* ----------                                                                 */
  5. /* Copyright (C) International Business Machines Corp., 1991,1992.            */
  6. /*                                                                            */
  7. /* DISCLAIMER OF WARRANTIES:                                                  */
  8. /* -------------------------                                                  */
  9. /* The following [enclosed] code is sample code created by IBM                */
  10. /* Corporation.  This sample code is not part of any standard IBM product     */
  11. /* and is provided to you solely for the purpose of assisting you in the      */
  12. /* development of your applications.  The code is provided "AS IS",           */
  13. /* without warranty of any kind.  IBM shall not be liable for any damages     */
  14. /* arising out of your use of the sample code, even if they have been         */
  15. /* advised of the possibility of such damages.                                */
  16. /*                                                                            */
  17. /******************************************************************************/
  18. /*-------------------------------------------------------------*\
  19. |  wordbag.C  -  Word Bag, example for the use of the           |
  20. |                Key Sorted Bag to perform statistics on words. |
  21. |                """"""""""""""                                 |
  22. |  The elements handled in the collections are the words of a   |
  23. |  phrase. The key of each word is the number of its letters.   |
  24. |  The Key Sorted Bag will store all occurences of each key.    |
  25. |  We use this to calculate the number of n-letter words in     |
  26. |  the phrase.                                                  |
  27. |                                                               |
  28. \*-------------------------------------------------------------*/
  29.  
  30.    #include <iostream.h>
  31.                                 // Class Word:
  32.    #include "toyword.h"
  33.                                 // Let's use the defaults:
  34.    #include <iksbag.h>
  35.  
  36.    typedef IKeySortedBag <Word, int> WordBag;
  37.  
  38.  
  39. int main() {
  40.    Word phrase[] = {"people", "who", "live", "in", "glass",
  41.                    "houses", "should", "not", "throw", "stones"};
  42.    const int phraseWords = sizeof(phrase) / sizeof(Word);
  43.  
  44.    WordBag wordbag(phraseWords);
  45.  
  46.    for (int cnt=0; cnt < phraseWords; cnt++)  {
  47.      wordbag.add(phrase[cnt]);
  48.    }
  49.  
  50.    cout << "Contents of our WordBag sorted by number of letters:\n";
  51.  
  52.    WordBag::Cursor wordBagCursor(wordbag);
  53.    forCursor(wordBagCursor)
  54.      cout << "WB: " << wordBagCursor.element().text() << "\n";
  55.  
  56.    cout << "\nOur phrase has " << phraseWords << " words.\n";
  57.    cout << "In our WordBag are " << wordbag.numberOfElements()
  58.         << " words.\n\n";
  59.  
  60.    cout << "There are " << wordbag.numberOfDifferentKeys()
  61.         << " different word lengths.\n\n";
  62.  
  63.    wordBagCursor.setToFirst();
  64.    do  {
  65.       int letters = wordbag.key(wordBagCursor.element());
  66.       cout << "There are "
  67.            << wordbag.numberOfElementsWithKey(letters)
  68.            << " words with " << letters << " letters.\n";
  69.    }  while  (wordbag.setToNextWithDifferentKey(wordBagCursor));
  70.  
  71.    return 0;
  72. }
  73.