Advertisement
lucast0rres

Conjuntos.java v2

Apr 3rd, 2017
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.54 KB | None | 0 0
  1. class Main {
  2.   public static void main (String args[]) {
  3.  
  4.       Conjunto n1 = new Conjunto(5);
  5.       n1.inserirValor(10);
  6.       n1.inserirValor(2);
  7.       n1.inserirValor(2);
  8.       n1.inserirValor(14);
  9.       n1.inserirValor(15);
  10.       //n1.inserirValor(6);
  11.       //n1.verificarValor(0);
  12.  
  13.       Conjunto n2 = new Conjunto(4);
  14.       n2.inserirValor(1);
  15.       n2.inserirValor(7);
  16.       n2.inserirValor(8);
  17.       n2.inserirValor(4);
  18.  
  19.       //n1.verificarSubconjunto(n2);
  20.  
  21.       Conjunto union = n1.uniaoConjunto(n2);
  22.       Conjunto inter = n1.interConjunto(n2);
  23.       Conjunto difer = n1.diferencaConjunto(n2);
  24.  
  25.       n1.imprimirVetor();
  26.       n2.imprimirVetor();
  27.       System.out.println("Uniao:"); union.imprimirVetor();
  28.       System.out.println("Interseccao:"); inter.imprimirVetor();
  29.       System.out.println("Diferenca:"); difer.imprimirVetor();
  30.       //System.out.println(n.getLength());
  31.     }
  32. }
  33.  
  34. class Conjunto {
  35.     int[] vetor;
  36.     int ponteiro;
  37.     int quantidade;
  38.  
  39.     Conjunto (int tamanho) {
  40.       this.vetor = new int[tamanho];
  41.       this.quantidade = 0;
  42.       this.ponteiro = -1;
  43.     }
  44.  
  45.     void inserirValor(int valor) {
  46.       if (this.ponteiro < (this.vetor.length-1)) {
  47.         this.quantidade++;
  48.         this.ponteiro++;
  49.         this.vetor[this.ponteiro] = valor;
  50.         System.out.println ("Valor adicionado.");
  51.       } else {
  52.         System.out.println ("Conjunto cheio.");
  53.       }
  54.     }
  55.  
  56.     boolean verificarValor(int valor) {
  57.       int verificador = 0;
  58.       while (verificador <= this.ponteiro) {
  59.         if (this.vetor[verificador] == valor) {
  60.           verificador = -1;
  61.           break;
  62.         }
  63.         verificador++;
  64.       }
  65.  
  66.       if (verificador == -1) {
  67.         System.out.println ("O valor pertence ao conjunto.");
  68.         return true;
  69.       } else {
  70.         System.out.println ("O valor nao pertence ao conjunto.");
  71.         return false;
  72.       }
  73.     }
  74.  
  75.     int verificarSubconjunto(Conjunto c) {
  76.  
  77.       if (c.quantidade > this.quantidade) {
  78.         System.out.println ("O subconjunto e maior que o conjunto comparado.");
  79.         return 0;
  80.       } else {
  81.  
  82.         int check = c.ponteiro;
  83.         int flag = c.quantidade;
  84.         int contador = 0;
  85.         while (flag > 0) {
  86.           if (this.verificarValor(c.vetor[check])) {
  87.             contador++;
  88.           }
  89.           flag--;
  90.           check--;
  91.         }
  92.  
  93.         if (contador == c.quantidade) {
  94.           System.out.println ("E subconjunto.");
  95.           return 1;
  96.         } else {
  97.           System.out.println ("Nao e subconjunto.");
  98.           return 0;
  99.         }
  100.       }
  101.     }
  102.  
  103.     Conjunto uniaoConjunto(Conjunto c) {
  104.       if (this.verificarSubconjunto(c) == 0) {
  105.  
  106.           Conjunto uniao = new Conjunto(this.quantidade + c.quantidade);
  107.  
  108.           int t1 = this.ponteiro;
  109.           int t2 = c.ponteiro;
  110.  
  111.           for (int x = this.quantidade; x > 0; x--) {
  112.             uniao.inserirValor(this.vetor[t1]);
  113.             t1--;
  114.           }
  115.  
  116.           for (int x = c.quantidade; x > 0; x--) {
  117.             if(!uniao.verificarValor(c.vetor[t2])) {
  118.               uniao.inserirValor(c.vetor[t2]);
  119.             }
  120.             t2--;
  121.           }
  122.  
  123.           quickSort(uniao.vetor,0,uniao.vetor.length-1);
  124.           return uniao;
  125.       } else {
  126.         return this;
  127.       }
  128.     }
  129.  
  130.  
  131.     Conjunto interConjunto(Conjunto c) {
  132.  
  133.       int contador = 0;
  134.  
  135.       for (int i = 0; i < this.quantidade; i++) {
  136.         for (int j = 0; j < c.quantidade; j++) {
  137.           if (this.vetor[i] == c.vetor[j]) {
  138.             contador++;
  139.           }
  140.         }
  141.       }
  142.      
  143.       if (contador == 0) {
  144.           contador++;
  145.       }
  146.      
  147.       Conjunto intersec = new Conjunto(contador);
  148.  
  149.       for (int i = 0; i < this.quantidade; i++) {
  150.         for (int j = 0; j < c.quantidade; j++) {
  151.           if (this.vetor[i] == c.vetor[j]) {
  152.             intersec.inserirValor(this.vetor[i]);
  153.           }
  154.         }
  155.       }
  156.  
  157.       quickSort(intersec.vetor,0,intersec.vetor.length-1);
  158.       return intersec;
  159.  
  160.     }
  161.  
  162.     Conjunto diferencaConjunto(Conjunto c) {
  163.  
  164.       int tamanho;
  165.       int contador = 0;
  166.  
  167.       for (int i = 0; i < this.quantidade; i++) {
  168.         for (int j = 0; j < c.quantidade; j++) {
  169.           if (this.vetor[i] == c.vetor[j]) {
  170.             contador++;
  171.             break;
  172.           }
  173.         }
  174.       }
  175.  
  176.       if (this.quantidade >= c.quantidade) {
  177.         tamanho = this.quantidade;
  178.       } else {
  179.         tamanho = c.quantidade;
  180.       }
  181.  
  182.       if (contador == tamanho) {
  183.  
  184.         Conjunto diferenca = new Conjunto(1);
  185.         diferenca.inserirValor(0);
  186.  
  187.         quickSort(diferenca.vetor,0,diferenca.vetor.length-1);
  188.         return diferenca;
  189.  
  190.       } else {
  191.  
  192.         tamanho -= contador;
  193.  
  194.         Conjunto diferenca = new Conjunto(tamanho);
  195.  
  196.         for (int i = 0; i < this.quantidade; i++) {
  197.           for (int j = 0; j < c.quantidade; j++) {
  198.             int flag = -1;
  199.             if (this.vetor[i] == c.vetor[j]) {
  200.               flag += 1;
  201.               if (flag > 0 && j == c.quantidade) {
  202.                 for (int k = 0; k < flag; k++) {
  203.                   diferenca.inserirValor(this.vetor[i]);
  204.                 }
  205.               }
  206.             }
  207.             else {
  208.               if (j == c.quantidade) {
  209.               diferenca.inserirValor(this.vetor[i]);
  210.               }
  211.             }
  212.           }
  213.         }
  214.  
  215.         quickSort(diferenca.vetor,0,diferenca.vetor.length-1);
  216.         return diferenca;
  217.       }
  218.  
  219.     }
  220.  
  221.     int getLength() {
  222.       return this.vetor.length;
  223.     }
  224.  
  225.     void imprimirVetor() {
  226.       String msg = "[";
  227.       for (int x = 0; x <= getLength()-1; x++) {
  228.         if (x == getLength()-1)
  229.           msg += this.vetor[x] + "]";
  230.         else
  231.           msg += this.vetor[x] + ",";
  232.       }
  233.  
  234.       System.out.println(msg);
  235.     }
  236.  
  237.     void quickSort(int[] vetor, int inicio, int fim) {
  238.       if (inicio < fim) {
  239.         int posicaoPivo = separar(vetor, inicio, fim);
  240.         quickSort(vetor, inicio, posicaoPivo - 1);
  241.         quickSort(vetor, posicaoPivo + 1, fim);
  242.       }
  243.     }
  244.  
  245.     int separar(int[] vetor, int inicio, int fim) {
  246.       int pivo = vetor[inicio];
  247.       int i = inicio + 1, f = fim;
  248.       while (i <= f) {
  249.         if (vetor[i] <= pivo)
  250.           i++;
  251.         else if (pivo < vetor[f])
  252.           f--;
  253.         else {
  254.           int troca = vetor[i];
  255.           vetor[i] = vetor[f];
  256.           vetor[f] = troca;
  257.           i++;
  258.           f--;
  259.         }
  260.       }
  261.       vetor[inicio] = vetor[f];
  262.       vetor[f] = pivo;
  263.       return f;
  264.     }
  265.  
  266. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement