Algebra zbiorów

/* SumaZbiorow.java */

import java.util.*;

public class SumaZbiorow
{
  public static void main(String[] args)
  {
    String[] a = {"a", "b", "c"};
    String[] b = {"b", "c", "d"};
    
    Object[] suma = suma(a,b);
    
    wypisz(a);
    
    System.out.println("+");
    
    wypisz(b);
    
    System.out.println("=");
    
    wypisz(suma);
    
    System.out.println();
  }
  
  static Object[] suma(Object[] a, Object[] b)
  {
    Object[] suma = new Object[a.length + b.length];

    System.arraycopy(a, 0, suma, 0, a.length);
    
    int k = a.length;
    
    for (int j = 0; j < b.length; j++)
    {
      int i;
      
      for (i = 0; i < a.length; i++)
      {
        if (b[j].equals(a[i])) break;
      }
      
      if (i == a.length) 
      {
        suma[k] = b[j];
        k++;
      }
    }
    
    if (k < a.length + b.length) 
    {
      Object[] pom = new Object[k];
      
      System.arraycopy(suma, 0, pom, 0, k);
      
      suma = pom;
    }  

    return suma;
  }
  
  static void wypisz(Object[] t)
  {
    System.out.print("{ ");
    
    for (int i = 0; i < t.length; i++) System.out.print(t[i] + " ");
    
    System.out.println("}");
  }
}
{ a b c }
+
{ b c d }
=
{ a b c d }

Press any key to continue...

Zadanie Dokonaj analizy programu SumaZbiorow.

a = {"a", "b", "c"};
b = {"b", "c", "d"};

suma = suma(a,b) =  {"a", "b", "c", "d"}

* suma(a,b) = {"a", "b", "c", "d"}
  
  a = {"a", "b", "c"}
  b = {"b", "c", "d"}

  suma = {null, null, null, null, null, null}

  suma = {"a", "b", "c", null, null, null}
  
  k = 3
  
  j = 0
  0 < 3   i
          i = 0
          0 < 3   "b" = b[0].equals(a[0] = "a")   false   i = 1
          1 < 3   "b" = b[0].equals(a[1] = "b")   break
  
          1 == 3   false
  j = 1
  1 < 3   i
          i = 0                                                 
          0 < 3   "c" = b[1].equals(a[0] = "a")   false   i = 1
          1 < 3   "c" = b[1].equals(a[1] = "b")   false   i = 2
          2 < 3   "c" = b[1].equals(a[2] = "c")   break

          2 == 3   false
  j = 2
  2 < 3   i
          i = 0                                                 
          0 < 3   "d" = b[2].equals(a[0] = "a")   false   i = 1
          1 < 3   "d" = b[2].equals(a[1] = "b")   false   i = 2
          2 < 3   "d" = b[2].equals(a[2] = "c")   false   i = 3
          3 < 3   false

          3 == 3   suma[3] = b[2] = "d"   // suma = {"a", "b", "c", "d", null, null}
                   k = 4
  j = 3
  3 < 3   false

  4 < 6   pom = {null, null, null, null}

          pom = {"a", "b", "c", "d"}

          suma = pom

  return sum = {"a", "b", "c", "d"}

"{ a b c }
 +
 { b c d }
 =
 { a b c d }"
/* RoznicaZbiorow.java */

import java.util.*;

public class RoznicaZbiorow
{
  public static void main(String[] args)
  {
    String[] a = {"a", "b", "c"};
    String[] b = {"c", "d", "e"};
    
    Object[] roznica = roznica(a,b);
    
    wypisz(a);
    
    System.out.println("\\");
    
    wypisz(b);
    
    System.out.println("=");
    
    wypisz(roznica);
    
    System.out.println();
  }
  
  static Object[] roznica(Object[] a, Object[] b)
  {
    Object[] roznica = new Object[a.length];
    
    int k = 0;
    
    for (int i = 0; i < a.length; i++)
    {
      int j;
      
      for (j = 0; j < b.length; j++)
      {
        if (a[i].equals(b[j])) break;
      }
      
      if (j == b.length)
      {
        roznica[k] = a[i];
        k++;
      }
    }
    
    if (k < a.length) 
    {
      Object[] pom = new Object[k];
      
      System.arraycopy(roznica, 0, pom, 0, k);
      
      roznica = pom;
    }  

    return roznica;
  }
  
  static void wypisz(Object[] t)
  {
    System.out.print("{ ");
    
    for (int i = 0; i < t.length; i++) System.out.print(t[i] + " ");
    
    System.out.println("}");
  }
}
{ a b c }
\
{ c d e }
=
{ a b }

Press any key to continue...

Zadanie Dokonaj analizy programu RoznicaZbiorow.

a = {"a", "b", "c"};
b = {"c", "d", "e"};

roznica = roznica(a,b) =  {"a", "b"}

* roznica(a,b) = {"a", "b"}
  
  a = {"a", "b", "c"}
  b = {"c", "d", "e"}

  roznica = {null, null, null}

  k = 0
  
  i = 0
  0 < 3   j
          j = 0
          0 < 3   "a" = a[0].equals(b[0] = "c")   false   j = 1
          1 < 3   "a" = a[0].equals(b[1] = "d")   false   j = 2
          2 < 3   "a" = a[0].equals(b[2] = "e")   false   j = 3
          3 < 3   false

          3 == 3   roznica[0] = a[0] = "a"   // roznica = {"a", null, null}
                   k = 1
  i = 1
  1 < 3   j
          j = 0
          0 < 3   "b" = a[1].equals(b[0] = "c")   false   j = 1
          1 < 3   "b" = a[1].equals(b[1] = "d")   false   j = 2
          2 < 3   "b" = a[1].equals(b[2] = "e")   false   j = 3
          3 < 3   false

          3 == 3   roznica[1] = a[1] = "b"   // roznica = {"a", "b", null}
                   k = 2
  i = 2
  2 < 3   j
          j = 0
          0 < 3   "c" = a[2].equals(b[0] = "c")   break

          0 == 3   false
  i = 3
  3 < 3   false

  2 < 3   pom = {null, null}

          pom = {"a", "b"}

          roznica = pom

  return roznica = {"a", "b"}

"{ a b c }
 \
 { c d e }
 =
 { a b }"
/* IloczynZbiorow.java */

import java.util.*;

public class IloczynZbiorow
{
  public static void main(String[] args)
  {
    String[] a = {"a", "b", "c"};
    String[] b = {"b", "c", "d"};
    
    Object[] iloczyn = iloczyn(a,b);
    
    wypisz(a);
    
    System.out.println("i");
    
    wypisz(b);
    
    System.out.println("=");
    
    wypisz(iloczyn);
    
    System.out.println();
  }
  
  static Object[] iloczyn(Object[] a, Object[] b)
  {
    Object[] iloczyn = new Object[a.length];
    
    int k = 0;
    
    for (int i = 0; i < a.length; i++)
    {
      int j;
      
      for (j = 0; j < b.length; j++)
      {
        if (a[i].equals(b[j])) break;
      }
      
      if (j < b.length) 
      {
        iloczyn[k] = a[i];
        k++;
      }
    }
    
    if (k < a.length) 
    {
      Object[] pom = new Object[k];
      
      System.arraycopy(iloczyn, 0, pom, 0, k);
      
      iloczyn = pom;
    }  

    return iloczyn;
  }
  
  static void wypisz(Object[] t)
  {
    System.out.print("{ ");
    
    for (int i = 0; i < t.length; i++) System.out.print(t[i] + " ");
    
    System.out.println("}");
  }
}
{ a b c }
i
{ b c d }
=
{ b c }

Press any key to continue...

Zadanie Dokonaj analizy programu IloczynZbiorow.

a = {"a", "b", "c"};
b = {"b", "c", "d"};

iloczyn = iloczyn(a,b) =  {"b", "c"}

* iloczyn(a,b) = {"b", "c"}
  
  a = {"a", "b", "c"}
  b = {"b", "c", "d"}

  iloczyn = {null, null, null}

  k = 0
  
  i = 0
  0 < 3   j
          j = 0
          0 < 3   "a" = a[0].equals(b[0] = "b")   false   j = 1
          1 < 3   "a" = a[0].equals(b[1] = "c")   false   j = 2
          2 < 3   "a" = a[0].equals(b[2] = "d")   false   j = 3
          3 < 3   false

          3 < 3   false
  i = 1
  1 < 3   j
          j = 0
          0 < 3   "b" = a[1].equals(b[0] = "b")   break

          0 < 3   iloczyn[0] = a[1] = "b"   // iloczyn = {"a", null, null}
                  k = 1
  i = 2
  2 < 3   j
          j = 0
          0 < 3   "c" = a[2].equals(b[0] = "b")   false   j = 1
          1 < 3   "c" = a[2].equals(b[1] = "c")   break

          1 < 3   iloczyn[1] = a[2] = "c"   // iloczyn = {"a", "c", null}
                  k = 2
  i = 3
  3 < 3   false

  2 < 3   pom = {null, null}

          pom = {"a", "c"}

          iloczyn = pom

  return iloczyn = {"a", "c"}

"{ a b c }
 i
 { b c d }
 =
 { b c }"

Strona główna