home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cset21v1.zip / IBMCPP / SAMPLES / ICLCC / EVENODD.C < prev    next >
C/C++ Source or Header  |  1993-05-07  |  4KB  |  94 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. |                                                               |
  20. |  evenodd.C  -  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 Boolean 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 << "\n";
  92. }
  93.  
  94.