home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / trialva / ibmcppw / samples / ioc / evenodd / evenodd.cpp next >
Encoding:
C/C++ Source or Header  |  1996-02-22  |  3.6 KB  |  94 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. |                                                                |
  20. |  evenodd.CPP  -  Even and Odd numbers are handled in different |
  21. |                  Sets do demonstrate using Sets.               |
  22. |                                            """                 |
  23. \*--------------------------------------------------------------*/
  24.  
  25. #include <iostream.h>
  26.  
  27. #include <iset.h>        // Take the defaults for the Set and for
  28.                          // the required functions for integer
  29. typedef ISet <int> IntSet;
  30.  
  31. /*-------------------------------------------------------------*\
  32. | For iteration we want to use an object of an iterator class   |
  33. \*-------------------------------------------------------------*/
  34. class PrintClass : public IIterator<int>  {
  35.   public:
  36.     virtual IBoolean applyTo(int& i)
  37.       { cout << " " << i << " "; return True;}
  38. };
  39.  
  40.  
  41. /*-------------------------------------------------------------*\
  42. | Local prototype for the function to display an IntSet.        |
  43. \*-------------------------------------------------------------*/
  44. void    List(char *, IntSet &);
  45.  
  46.  
  47. /*-------------------------------------------------------------*\
  48. | Main program                                                  |
  49. \*-------------------------------------------------------------*/
  50. int main ()  {
  51.    IntSet odd, prime;
  52.    IntSet oddPrime, evenPrime;
  53.  
  54.    int One = 1, Two = 2, Three = 3, Five = 5, Seven = 7, Nine = 9;
  55.  
  56. // Fill odd set with odd integers < 10
  57.    odd.add( One );
  58.    odd.add( Three );
  59.    odd.add( Five );
  60.    odd.add( Seven );
  61.    odd.add( Nine );
  62.    List("Odds less than 10:  ", odd);
  63.  
  64. // Fill prime set with primes < 10
  65.    prime.add( Two );
  66.    prime.add( Three );
  67.    prime.add( Five );
  68.    prime.add( Seven );
  69.    List("Primes less than 10:  ", prime);
  70.  
  71. // Intersect 'Odd' and 'Prime' to give 'OddPrime'
  72.    oddPrime.addIntersection( odd, prime);
  73.    List("Odd primes less than 10:  ", oddPrime);
  74.  
  75. // Subtract all 'Odd' from 'Prime' to give 'EvenPrime'
  76.    evenPrime.addDifference( prime, oddPrime);
  77.    List("Even primes less than 10:  ", evenPrime);
  78.  
  79.    return(0);
  80. }
  81.  
  82. /*-------------------------------------------------------------*\
  83. | Local function to display an IntSet.                          |
  84. \*-------------------------------------------------------------*/
  85.  
  86. void List(char *Message, IntSet &anIntSet)  {
  87.    PrintClass Print;
  88.  
  89.    cout << Message;
  90.    anIntSet.allElementsDo(Print);
  91.    cout << endl;
  92. }
  93.  
  94.