Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.42 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <assert.h>
  4.  
  5. typedef uint64_t set;
  6.  
  7. #define MAX_SET_MEMBER ((int)(8 * sizeof (set) - 1))
  8. #define EMPTY_SET 0
  9.  
  10. void print_bits_hex(char *description, short value);
  11. set set_add(int x, set a);
  12. set set_union(set a, set b);
  13. set set_intersection(set a, set b);
  14. int set_member(int x, set a);
  15. int set_cardinality(set a);
  16. set set_read(char *prompt);
  17. void set_print(char *description, set a);
  18.  
  19. int main(void) {
  20.     printf("Set members can be 0-%d, negative number to finish\n", MAX_SET_MEMBER);
  21.     set a = set_read("Enter set a: ");
  22.     set b = set_read("Enter set b: ");
  23.     print_bits_hex("a = ", a);
  24.     print_bits_hex("b = ", b);
  25.     set_print("a = ", a);
  26.     set_print("b = ", b);
  27.     set_print("a union b = ", set_union(a, b));
  28.     set_print("a intersection b = ", set_intersection(a, b));
  29.     printf("cardinality(a) = %d\n", set_cardinality(a));
  30.     printf("is_member(42, a) = %d\n", set_member(42, a));
  31.     return 0;
  32. }
  33.  
  34. set set_add(int x, set a) {
  35.     return a | ((set)1 << x);
  36. }
  37.  
  38. set set_union(set a, set b) {
  39.     return a|b;
  40. }
  41.  
  42. set set_intersection(set a, set b) {
  43.     return a|b;
  44. }
  45.  
  46. // return a non-zero value iff x is a member of a
  47. int set_member(int x, set a) {
  48.     assert(x >= 0 && x < MAX_SET_MEMBER);
  49.     return a & ((set)1 << x);
  50. }
  51.  
  52. // return size of set
  53. int set_cardinality(set a) {
  54.     int n_members = 0;
  55.     while (a != 0) {
  56.         n_members += a & 1;
  57.         a >>= 1;
  58.     }
  59.     return n_members;
  60. }
  61.  
  62. set set_read(char *prompt) {
  63.     printf("%s", prompt);
  64.     set a = EMPTY_SET;
  65.     int x;
  66.     while (scanf("%d", &x) == 1 && x >= 0) {
  67.         a = set_add(x, a);
  68.     }
  69.     return a;
  70. }
  71.  
  72. // print out member of the set in increasing order
  73. // for example {5,11,56}
  74. void set_print(char *description, set a) {
  75.     printf("%s", description);
  76.     printf("{");
  77.     int n_printed = 0;
  78.     for (int i = 0; i < MAX_SET_MEMBER; i++) {
  79.         if (set_member(i, a)) {
  80.             if (n_printed > 0) {
  81.                 printf(",");
  82.             }
  83.             printf("%d", i);
  84.             n_printed++;
  85.         }
  86.     }
  87.     printf("}\n");
  88. }
  89.  
  90. void print_bits_hex(char *description, short value) {
  91.     printf("%s", description);
  92.     int which_bit = 8 * (sizeof value);
  93.     while (which_bit > 0) {
  94.         which_bit = which_bit - 1;
  95.         printf("%d", (value >> which_bit) & 1);
  96.     }
  97.     printf(" = 0x%04x = %d\n", value & 0xFFFF, value);
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement