home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / trialva / ibmcppw / samples / ioc / wordseq / wordseq.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-22  |  4.5 KB  |  117 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. |  wordseq.CPP  -  Example for using the Sequence.              |
  20. |                                        """"""""               |
  21. |  This Sequence is used to handle elements of type Word.       |
  22. |                                                               |
  23. |  This example also demonstrates two different ways of         |
  24. |  iteration, using an object of an applicator class            |
  25. |  and using a cursor.                                          |
  26. \*-------------------------------------------------------------*/
  27.  
  28. #include <iostream.h>
  29.  
  30.               // Get definition and declaration of class Word:
  31. #include "toyword.h"
  32.  
  33.               // Define a compare function to be used for sort:
  34. inline long wordCompare ( Word const& w1, Word const& w2) {
  35.    return (w1.getWord() > w2.getWord());
  36. }
  37.  
  38. /*-------------------------------------------------------------*\
  39. |  We want to use the default Sequence called ISequence.        |
  40. \*-------------------------------------------------------------*/
  41. #include <iseq.h>
  42.  
  43. typedef ISequence <Word> WordSeq;
  44. typedef IApplicator <Word> WordApplicator;
  45.  
  46.  
  47. /*-------------------------------------------------------------*\
  48. |  Our Applicator class for use with allElementsDo().           |
  49. |                                                               |
  50. |  The alternative method of iteration, using a cursor, does    |
  51. |  not need such an applicator class. If you want to see        |
  52. |  this alternative, search for occurences of cursor below.     |
  53. |                                             """"""            |
  54. \*-------------------------------------------------------------*/
  55. class PrintClass : public WordApplicator
  56. {
  57. public:
  58.    IBoolean applyTo(Word &w)
  59.       {
  60.       cout << endl << w.getWord();    // Print the string
  61.       return(True);
  62.       }
  63. };
  64.  
  65.  
  66.  
  67.  
  68. /*-------------------------------------------------------------*\
  69. | Main program                                                  |
  70. \*-------------------------------------------------------------*/
  71. int main()  {
  72.  
  73.    IString wordArray [9] = {
  74.       "the",    "quick",   "brown",   "fox",   "jumps",
  75.       "over",   "a",       "lazy",    "dog"
  76.    };
  77.  
  78.    WordSeq WL;
  79.    WordSeq::Cursor cursor(WL);
  80.    PrintClass Print;
  81.  
  82.    int i;
  83.  
  84.    for (i = 0; i < 9; i ++) {     // Put all strings into Sequence
  85.       Word aWord(wordArray[i]);   // Fill object with right value
  86.       WL.addAsLast(aWord);        // Add it to the Sequence at end
  87.    }
  88.  
  89.    cout << endl << "Sequence in initial order:" << endl;
  90.    WL.allElementsDo(Print);
  91.  
  92.    WL.sort(wordCompare);       // Sort the Sequence ascending
  93.    cout << endl << endl << "Sequence in sorted order:" << endl;
  94.    WL.allElementsDo(Print);
  95.  
  96.    // Use iteration via cursor now:
  97.  
  98.    cout << endl << endl << "Look for \"fox\" in the Sequence:" << endl;
  99.    for (cursor.setToFirst();
  100.         cursor.isValid() && (WL.elementAt(cursor).getWord() != "fox");
  101.         cursor.setToNext());
  102.  
  103.    if (WL.elementAt(cursor).getWord() != "fox") {
  104.        cout << endl << "The element was not found." << endl;
  105.    }
  106.    else {
  107.        cout << endl << " The element was found." << endl;
  108.    }
  109.  
  110.    cout << endl << "The element at position 9: "
  111.         << WL.elementAtPosition(9).getWord()
  112.         << endl;
  113.  
  114.    return(0);
  115. }
  116.  
  117.