lucast0rres

Conjuntos.java v4

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