Advertisement
lucast0rres

Conjuntos.java

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