Advertisement
lucast0rres

Conjuntos v3

Apr 4th, 2017
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.26 KB | None | 0 0
  1. https://www.caelum.com.br/apostila-java-orientacao-objetos/classes-abstratas/#aumentando-o-exemplo
  2.  
  3. class Main {
  4.   public static void main (String args[]) {
  5.  
  6.       //Dez primeiros numeros naturais
  7.       Conjunto n1 = new Conjunto(10);
  8.       n1.inserirValor(0);
  9.       n1.inserirValor(1);
  10.       n1.inserirValor(2);
  11.       n1.inserirValor(3);
  12.       n1.inserirValor(4);
  13.       n1.inserirValor(5);
  14.       n1.inserirValor(6);
  15.       n1.inserirValor(7);
  16.       n1.inserirValor(8);
  17.       n1.inserirValor(9);
  18.  
  19.       //Cinco primeiros numeros pares
  20.       Conjunto n2 = new Conjunto(5);
  21.       n2.inserirValor(2);
  22.       n2.inserirValor(4);
  23.       n2.inserirValor(6);
  24.       n2.inserirValor(8);
  25.       n2.inserirValor(10);
  26.  
  27.       //Cinco primeiros numeros impares
  28.       Conjunto n3 = new Conjunto(5);
  29.       n3.inserirValor(1);
  30.       n3.inserirValor(3);
  31.       n3.inserirValor(5);
  32.       n3.inserirValor(7);
  33.       n3.inserirValor(9);
  34.  
  35.       //Dez primeiros numeros primos
  36.       Conjunto n4 = new Conjunto(10);
  37.       n4.inserirValor(2);
  38.       n4.inserirValor(3);
  39.       n4.inserirValor(5);
  40.       n4.inserirValor(7);
  41.       n4.inserirValor(11);
  42.       n4.inserirValor(13);
  43.       n4.inserirValor(17);
  44.       n4.inserirValor(19);
  45.       n4.inserirValor(23);
  46.       n4.inserirValor(29);
  47.  
  48.       System.out.println("--------------- ITEM A ---------------");
  49.       //Se o conjunto 4 eh subconjunto de si mesmo
  50.       n4.verificarSubconjunto(n4);
  51.  
  52.       System.out.println("--------------- ITEM B ---------------");
  53.       //A pertinencia dos conjuntos 2, 3 e 4 no conjunto 1
  54.       System.out.println("--------------- ITEM B.1 ---------------");
  55.       n1.verificarSubconjunto(n2);
  56.       System.out.println("--------------- ITEM B.2 ---------------");
  57.       n1.verificarSubconjunto(n3);
  58.       System.out.println("--------------- ITEM B.3 ---------------");
  59.       n1.verificarSubconjunto(n4);
  60.  
  61.       System.out.println("--------------- ITEM C ---------------");
  62.       //Se a uniao de 2 e 3 eh igual ao conjunto 1;
  63.       Conjunto union = n2.uniaoConjunto(n3);
  64.  
  65.       System.out.println("--------------- ITEM D ---------------");
  66.       //Se a interseccao dos conjuntos 1 e 2 eh vazia
  67.       Conjunto inter = n1.interConjunto(n2);
  68.  
  69.       System.out.println("--------------- ITEM E ---------------");
  70.       //Qual a diferenca entre os conjuntos 1 e 2
  71.       Conjunto difer = n1.diferencaConjunto(n2);
  72.  
  73.       System.out.println("--------------- FIM ---------------");
  74.       n1.imprimirVetor();
  75.       n2.imprimirVetor();
  76.       n3.imprimirVetor();
  77.       n4.imprimirVetor();
  78.       System.out.println("Uniao:"); union.imprimirVetor();
  79.       System.out.println("Interseccao:"); inter.imprimirVetor();
  80.       System.out.println("Diferenca:"); difer.imprimirVetor();
  81.       //System.out.println(n.getLength());
  82.     }
  83. }
  84.  
  85. class Conjunto {
  86.     int[] vetor;
  87.     int ponteiro;
  88.     int quantidade;
  89.  
  90.     Conjunto (int tamanho) {
  91.       this.vetor = new int[tamanho];
  92.       this.quantidade = 0;
  93.       this.ponteiro = -1;
  94.     }
  95.  
  96.     void inserirValor(int valor) {
  97.       if (this.ponteiro < (this.vetor.length-1)) {
  98.         this.quantidade++;
  99.         this.ponteiro++;
  100.         this.vetor[this.ponteiro] = valor;
  101.         System.out.println ("Valor adicionado.");
  102.       } else {
  103.         System.out.println ("Conjunto cheio.");
  104.       }
  105.     }
  106.  
  107.     boolean verificarValor(int valor) {
  108.       int verificador = 0;
  109.       while (verificador <= this.ponteiro) {
  110.         if (this.vetor[verificador] == valor) {
  111.           verificador = -1;
  112.           break;
  113.         }
  114.         verificador++;
  115.       }
  116.  
  117.       if (verificador == -1) {
  118.         System.out.println ("O valor pertence ao conjunto.");
  119.         return true;
  120.       } else {
  121.         System.out.println ("O valor nao pertence ao conjunto.");
  122.         return false;
  123.       }
  124.     }
  125.  
  126.     int verificarSubconjunto(Conjunto c) {
  127.  
  128.       if (c.quantidade > this.quantidade) {
  129.         System.out.println ("O subconjunto e maior que o conjunto comparado.");
  130.         return 0;
  131.       } else {
  132.  
  133.         int check = c.ponteiro;
  134.         int flag = c.quantidade;
  135.         int contador = 0;
  136.         while (flag > 0) {
  137.           if (this.verificarValor(c.vetor[check])) {
  138.             contador++;
  139.           }
  140.           flag--;
  141.           check--;
  142.         }
  143.  
  144.         if (contador == c.quantidade) {
  145.           System.out.println ("E subconjunto.");
  146.           return 1;
  147.         } else {
  148.           System.out.println ("Nao e subconjunto.");
  149.           return 0;
  150.         }
  151.       }
  152.     }
  153.  
  154.     Conjunto uniaoConjunto(Conjunto c) {
  155.       if (this.verificarSubconjunto(c) == 0) {
  156.  
  157.           Conjunto uniao_teste = new Conjunto(this.quantidade + c.quantidade);
  158.  
  159.           int t1 = this.ponteiro;
  160.           int t2 = c.ponteiro;
  161.  
  162.           for (int x = this.quantidade; x > 0; x--) {
  163.             if (!uniao_teste.verificarValor(this.vetor[t1])) {
  164.               uniao_teste.inserirValor(this.vetor[t1]);
  165.               t1--;
  166.             }
  167.           }
  168.  
  169.           for (int x = c.quantidade; x > 0; x--) {
  170.             if(!uniao_teste.verificarValor(c.vetor[t2])) {
  171.               uniao_teste.inserirValor(c.vetor[t2]);
  172.             }
  173.             t2--;
  174.           }
  175.  
  176.           int contador = 0;
  177.  
  178.           for (int t3 = uniao_teste.ponteiro; t3 >= 0; t3--) {
  179.             if (uniao_teste.vetor[t3] == 0) {
  180.               contador++;
  181.             }
  182.           }
  183.  
  184.           Conjunto uniao = new Conjunto(uniao_teste.quantidade - (contador-1));
  185.  
  186.           t1 = this.ponteiro;
  187.           t2 = c.ponteiro;
  188.  
  189.           for (int x = this.quantidade; x > 0; x--) {
  190.             if (!uniao.verificarValor(this.vetor[t1])) {
  191.               uniao.inserirValor(this.vetor[t1]);
  192.               t1--;
  193.             }
  194.           }
  195.  
  196.           for (int x = c.quantidade; x > 0; x--) {
  197.             if(!uniao.verificarValor(c.vetor[t2])) {
  198.               uniao.inserirValor(c.vetor[t2]);
  199.             }
  200.             t2--;
  201.           }
  202.  
  203.           quickSort(uniao.vetor,0,uniao.vetor.length-1);
  204.           return uniao;
  205.       } else {
  206.         return this;
  207.       }
  208.     }
  209.  
  210.  
  211.     Conjunto interConjunto(Conjunto c) {
  212.  
  213.       int contador = 0;
  214.  
  215.       for (int i = 0; i < this.quantidade; i++) {
  216.         for (int j = 0; j < c.quantidade; j++) {
  217.           if (this.vetor[i] == c.vetor[j]) {
  218.             contador++;
  219.           }
  220.         }
  221.       }
  222.      
  223.       if (contador == 0) {
  224.           contador++;
  225.       }
  226.      
  227.       Conjunto intersec = new Conjunto(contador);
  228.  
  229.       for (int i = 0; i < this.quantidade; i++) {
  230.         for (int j = 0; j < c.quantidade; j++) {
  231.           if (this.vetor[i] == c.vetor[j]) {
  232.             intersec.inserirValor(this.vetor[i]);
  233.           }
  234.         }
  235.       }
  236.  
  237.       quickSort(intersec.vetor,0,intersec.vetor.length-1);
  238.       return intersec;
  239.  
  240.     }
  241.  
  242.     Conjunto diferencaConjunto(Conjunto c) {
  243.  
  244.       int tamanho;
  245.       int contador = 0;
  246.  
  247.       for (int i = 0; i < this.quantidade; i++) {
  248.         for (int j = 0; j < c.quantidade; j++) {
  249.           if (this.vetor[i] == c.vetor[j]) {
  250.             contador++;
  251.             break;
  252.           }
  253.         }
  254.       }
  255.  
  256.       if (this.quantidade >= c.quantidade) {
  257.         tamanho = this.quantidade;
  258.       } else {
  259.         tamanho = c.quantidade;
  260.       }
  261.  
  262.       if (contador == tamanho) {
  263.  
  264.         Conjunto diferenca = new Conjunto(1);
  265.         diferenca.inserirValor(0);
  266.  
  267.         quickSort(diferenca.vetor,0,diferenca.vetor.length-1);
  268.         return diferenca;
  269.  
  270.       } else {
  271.  
  272.         tamanho -= contador;
  273.  
  274.         Conjunto diferenca = new Conjunto(tamanho);
  275.  
  276.         for (int i = 0; i < this.quantidade; i++) {
  277.           for (int j = 0; j < c.quantidade; j++) {
  278.             int flag = -1;
  279.             if (this.vetor[i] == c.vetor[j]) {
  280.               flag += 1;
  281.               if (flag > 0 && j == c.quantidade) {
  282.                 for (int k = 0; k < flag; k++) {
  283.                   diferenca.inserirValor(this.vetor[i]);
  284.                 }
  285.               }
  286.             }
  287.             else {
  288.               if (j == c.quantidade) {
  289.               diferenca.inserirValor(this.vetor[i]);
  290.               }
  291.             }
  292.           }
  293.         }
  294.  
  295.         quickSort(diferenca.vetor,0,diferenca.vetor.length-1);
  296.         return diferenca;
  297.       }
  298.  
  299.     }
  300.  
  301.     int getLength() {
  302.       return this.vetor.length;
  303.     }
  304.  
  305.     void imprimirVetor() {
  306.       String msg = "[";
  307.       for (int x = 0; x <= getLength()-1; x++) {
  308.         if (x == getLength()-1)
  309.           msg += this.vetor[x] + "]";
  310.         else
  311.           msg += this.vetor[x] + ",";
  312.       }
  313.  
  314.       System.out.println(msg);
  315.     }
  316.  
  317.     void quickSort(int[] vetor, int inicio, int fim) {
  318.       if (inicio < fim) {
  319.         int posicaoPivo = separar(vetor, inicio, fim);
  320.         quickSort(vetor, inicio, posicaoPivo - 1);
  321.         quickSort(vetor, posicaoPivo + 1, fim);
  322.       }
  323.     }
  324.  
  325.     int separar(int[] vetor, int inicio, int fim) {
  326.       int pivo = vetor[inicio];
  327.       int i = inicio + 1, f = fim;
  328.       while (i <= f) {
  329.         if (vetor[i] <= pivo)
  330.           i++;
  331.         else if (pivo < vetor[f])
  332.           f--;
  333.         else {
  334.           int troca = vetor[i];
  335.           vetor[i] = vetor[f];
  336.           vetor[f] = troca;
  337.           i++;
  338.           f--;
  339.         }
  340.       }
  341.       vetor[inicio] = vetor[f];
  342.       vetor[f] = pivo;
  343.       return f;
  344.     }
  345.  
  346. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement