donmike25

tuto 7 q2

Jan 8th, 2014
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.28 KB | None | 0 0
  1. #include<stdio.h>
  2. #define SETSIZE 8
  3.  
  4. typedef unsigned int Set;
  5. void printSet(Set s);
  6.  
  7. int main()
  8. {
  9.     Set a,b;
  10.     a = (1<<3)|(1<<4)|(1<<5)|(1<<7);                     // create set A
  11.     printf("A = ");printSet(a);                          // 10111000
  12.     b = (1<<1)|(1<<3)|(1<<5);                            // create set B
  13.     printf("B= ");printSet(b);
  14.     printf("A union B = ");printSet(a|b);                // find union of And B
  15.     printf("A intersect B = ");printSet(a&b);            // find intersection of A and B
  16.     printf("insert 6 into A.");                          // insert 6 into A
  17.     a = (1<<3)|(1<<4)|(1<<5)|(1<<6)|(1<<7);  
  18.     printf("A =");printSet(a);               // print the new value of the set after element 6 is inserted
  19.     printf("Is 3 an element of B? ");                    // to check if 3 is an element of B
  20.     if ( b & (1<<3))
  21.         printf("yes\n");
  22.     else
  23.         printf("No\n");
  24.  
  25.     return 0;
  26. }
  27.  
  28. void printSet(Set s)
  29. {
  30.     int j;
  31.     printf("{ ");
  32.     for (j=0; j<SETSIZE; j++)
  33.     if ((1<<j)&s)printf("%d ",j);
  34.     printf("}\n");
  35.     }
  36.  
  37.  
  38.  
  39.  
  40. /*
  41. Example:
  42.  
  43. write set A= {1,3} and insert element 2 into A:
  44.  
  45. set A initially = 0000 0000
  46. so,
  47. (1<<1) will be    0000 0010
  48. (1<<3) will be    0000 1000
  49. then,
  50. (1<<1) | (1<<3) is 0000 1010
  51.  
  52. to insert:
  53. (1<<2) | A      is 0000 1110
  54.  
  55. */
Advertisement
Add Comment
Please, Sign In to add comment