Advertisement
lpjakewolfskin

lab_1 (sets)

Oct 2nd, 2018
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.99 KB | None | 0 0
  1. class Sets {
  2.     public static void main(String... args) {
  3.         String[] a = {"1", "2", "3", "4", "5"};
  4.         String[] b = {"1", "3", "6", "7"};
  5.         print_set(a, "a");
  6.         print_set(b, "b");
  7.         print_set(union(a, b), "union");
  8.         print_set(intersection(a, b), "intersection");
  9.         print_set(difference(a, b), "difference a/b");
  10.         print_set(difference(b, a), "difference b/a");
  11.         print_set(sim_difference(a, b), "sim_difference");
  12.     }
  13.  
  14. // start of the interface:
  15.  
  16.     public static String[] extend(String[] set) { // returns extended set with length set.length*2+1
  17.         String[] new_set = new String[set.length*2+1];
  18.         for (int i = 0; i < new_set.length; i++){
  19.             if (i < set.length)
  20.                 new_set[i] = set[i];
  21.         }
  22.         return new_set;
  23.     }
  24.  
  25.     public static String[] reduce(String[] set) { // returns reduced set with length set.length/2
  26.         String[] new_set = new String[set.length/2];
  27.         for (int i = 0; i < new_set.length; i++){
  28.             new_set[i] = set[i];
  29.         }
  30.         return new_set;
  31.     }
  32.  
  33.     public static String[] add(String[] set, String elem) { // returns "set" with added element "elem"
  34.         int end = -1;
  35.         for (int i = 0; i < set.length; i++) {
  36.             if (elem == set[i])
  37.                 return set;
  38.             if (set[i] != null)
  39.                 end = i;
  40.         }
  41.         if ((set.length == 0) || (end >= set.length/2))
  42.             set = extend(set);
  43.         set[end+1] = elem;
  44.         return set;
  45.     }
  46.  
  47.     public static String[] remove(String[] set, String elem) { // returns "set" with removed element "elem"
  48.         int end = 0;
  49.         for (int i = 0; i < set.length; i++){
  50.             if (set[i] == elem) {
  51.                 for (int j = i; j < set.length-1; j++)
  52.                     set[j] = set[j+1];
  53.                 set[set.length-1] = null;
  54.             }
  55.             if (set[i] != null)
  56.                 end = i;
  57.         }
  58.         if (end < set.length/2)
  59.             set = reduce(set);
  60.         return set;
  61.     }
  62.  
  63.     public static void print_set(String[] set, String name) { //prints the set as: "name": set[0] set[1] ... set[set.length - 1]
  64.         String s = name + ": ";
  65.         for (int i = 0; i < set.length; i++)
  66.             if (set[i] != null)
  67.                 s = s + set[i] + " ";
  68.     System.out.println(s);
  69.     }
  70.  
  71.     public static String[] union(String[] a, String[] b) { // returns the union-set of sets "a" and "b"
  72.         String[] result = a;
  73.         for (int i = 0; i < b.length; i++)
  74.             result = add(result, b[i]);
  75.         return result;
  76.     }
  77.  
  78.     public static String[] intersection(String[] a, String[] b) { // returns the intersection-set of sets "a" and "b"
  79.         String[] result = new String[0];
  80.         for (int i = 0; i < a.length; i++)
  81.             for (int j = 0; j < b.length; j++)
  82.                 if (a[i] == b[j])
  83.                     result = add(result, a[i]);
  84.            
  85.         return result;
  86.     }
  87.  
  88.     public static String[] difference(String[] a, String[] b) { // returns the difference-set of sets "a" and "b"
  89.         String[] result = new String[0];
  90.         for (int i = 0; i < a.length; i++)
  91.             result = add(result, a[i]);
  92.         for (int i = 0; i < b.length; i++)
  93.             result = remove(result, b[i]);
  94.         return result;
  95.     }
  96.  
  97.     public static String[] sim_difference(String[] a, String[] b) { // returns the simmetric-difference-set of sets "a" and "b"
  98.         return union(difference(a, b), difference(b, a));
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement