home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / trialva / ibmcppw / samples / ioc / wordbag / wordbag.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-22  |  3.4 KB  |  79 lines

  1. /**********************************************************************
  2. *                                                                     *
  3. *  IBM(R) VisualAge(TM) for C++ for Windows(R), Version 3.5           *
  4. *                                                                     *
  5. *  PID: 5622-880                                                      *
  6. *  - Licensed Material - Program-Property of IBM                      *
  7. *  (C) Copyright IBM Corp. 1991, 1995 - All Right Reserved.           *
  8. *                                                                     *
  9. *  US Government Users Restricted Rights - Use, duplication or        *
  10. *  disclosure restricted by GSA ADP Schedule Contract with IBM Corp.  *
  11. *                                                                     *
  12. *  VisualAge, and IBM are trademarks or registered trademarks of      *
  13. *  International Business Machines Corporation.                       *
  14. *  Windows is a registered trademark of Microsoft Corporation.        *
  15. *                                                                     *
  16. **********************************************************************/
  17.  
  18. /*---------------------------------------------------------------*\
  19. |  wordbag.CPP  -  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 <iksb.h>
  35.  
  36.    typedef IKeySortedBag <Word, unsigned> WordBag;
  37.  
  38.  
  39. int main()
  40. {
  41.    IString    phrase[10] = {"people", "who", "live", "in", "glass",
  42.                    "houses", "should", "not", "throw", "stones"};
  43.    const size_t phraseWords = sizeof(phrase) / sizeof(IString);
  44.  
  45.    WordBag wordbag(phraseWords);
  46.  
  47.    for (int cnt=0; cnt < phraseWords; cnt++)  {
  48.     unsigned keyValue = phrase[cnt].length();
  49.     Word theWord(phrase[cnt],keyValue);
  50.     wordbag.add (theWord);
  51.    }
  52.  
  53.    cout << "Contents of our WordBag sorted by number of letters:"
  54.         << endl;
  55.  
  56.    WordBag::Cursor wordBagCursor(wordbag);
  57.    forICursor(wordBagCursor)
  58.      cout << "WB: " << wordBagCursor.element().getWord() << endl;
  59.  
  60.    cout << endl << "Our phrase has " << phraseWords << " words."
  61.         << endl;
  62.  
  63.    cout << "In our WordBag are " << wordbag.numberOfElements()
  64.         << " words." << endl << endl;
  65.  
  66.    cout << "There are " << wordbag.numberOfDifferentKeys()
  67.         << " different word lengths." << endl << endl;
  68.  
  69.    wordBagCursor.setToFirst();
  70.    do  {
  71.       unsigned letters = wordbag.key(wordBagCursor.element());
  72.       cout << "There are "
  73.            << wordbag.numberOfElementsWithKey(letters)
  74.            << " words with " << letters << " letters." << endl;
  75.    }  while  (wordbag.setToNextWithDifferentKey(wordBagCursor));
  76.  
  77.    return 0;
  78. }
  79.