Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Conjunto {
- int[] vetor;
- int ponteiro;
- int quantidade;
- Conjunto (int tamanho) {
- this.vetor = new int[tamanho];
- this.quantidade = 0;
- this.ponteiro = -1;
- }
- void inserirValor(int valor) {
- if (this.ponteiro < (this.vetor.length-1)) {
- this.quantidade++;
- this.ponteiro++;
- this.vetor[this.ponteiro] = valor;
- System.out.println ("Valor adicionado.");
- } else {
- System.out.println ("Conjunto cheio.");
- }
- }
- boolean verificarValor(int valor) {
- int verificador = 0;
- while (verificador <= this.ponteiro) {
- if (this.vetor[verificador] == valor) {
- verificador = -1;
- break;
- }
- verificador++;
- }
- if (verificador == -1) {
- System.out.println ("O valor pertence ao conjunto.");
- return true;
- } else {
- System.out.println ("O valor nao pertence ao conjunto.");
- return false;
- }
- /*
- for (int verificador = 0; verificador < tamanho; verificador++) {
- if (this.vetor[verificador] == valor) {
- System.out.println ("O valor pertence ao conjunto.");
- } else {
- System.out.println ("O valor nao pertence ao conjunto.");
- }
- }
- */
- }
- int verificarSubconjunto(Conjunto c) {
- if (c.quantidade > this.quantidade) {
- System.out.println ("O subconjunto e maior que o conjunto comparado.");
- return 0;
- } else {
- int check = c.ponteiro;
- int flag = c.quantidade;
- int contador = 0;
- while (flag > 0) {
- if (this.verificarValor(c.vetor[check])) {
- contador++;
- }
- flag--;
- check--;
- }
- if (contador == c.quantidade) {
- System.out.println ("E subconjunto.");
- return 1;
- } else {
- System.out.println ("Nao e subconjunto.");
- return 0;
- }
- }
- }
- Conjunto uniaoConjunto(Conjunto c) {
- if (this.verificarSubconjunto(c) == 0) {
- Conjunto uniao = new Conjunto(this.quantidade + c.quantidade);
- int t1 = this.ponteiro;
- int t2 = c.ponteiro;
- for (int x = this.quantidade; x > 0; x--) {
- uniao.inserirValor(this.vetor[t1]);
- t1--;
- }
- for (int x = c.quantidade; x > 0; x--) {
- if(!uniao.verificarValor(c.vetor[t2])) {
- uniao.inserirValor(c.vetor[t2]);
- }
- t2--;
- }
- quickSort(uniao.vetor,0,uniao.vetor.length-1);
- return uniao;
- } else {
- return this;
- }
- }
- Conjunto interConjunto(Conjunto c) {
- int contador = 0;
- for (int i = 0; i < this.quantidade; i++) {
- for (int j = 0; j < c.quantidade; j++) {
- if (this.vetor[i] == c.vetor[j]) {
- contador++;
- }
- }
- }
- Conjunto intersec = new Conjunto(contador);
- for (int i = 0; i < this.quantidade; i++) {
- for (int j = 0; j < c.quantidade; j++) {
- if (this.vetor[i] == c.vetor[j]) {
- intersec.inserirValor(this.vetor[i]);
- }
- }
- }
- quickSort(intersec.vetor,0,intersec.vetor.length-1);
- return intersec;
- }
- Conjunto diferencaConjunto(Conjunto c) {
- int tamanho;
- int contador = 0;
- for (int i = 0; i < this.quantidade; i++) {
- for (int j = 0; j < c.quantidade; j++) {
- if (this.vetor[i] == c.vetor[j]) {
- contador++;
- break;
- }
- }
- }
- if (this.quantidade >= c.quantidade) {
- tamanho = this.quantidade;
- } else {
- tamanho = c.quantidade;
- }
- if (contador == tamanho) {
- Conjunto diferenca = new Conjunto(1);
- diferenca.inserirValor(0);
- quickSort(diferenca.vetor,0,diferenca.vetor.length-1);
- return diferenca;
- } else {
- tamanho -= contador;
- Conjunto diferenca = new Conjunto(tamanho);
- for (int i = 0; i < this.quantidade; i++) {
- for (int j = 0; j < c.quantidade; j++) {
- int flag = -1;
- if (this.vetor[i] == c.vetor[j]) {
- flag += 1;
- if (flag > 0 && j == c.quantidade) {
- for (int k = 0; k < flag; k++) {
- diferenca.inserirValor(this.vetor[i]);
- }
- }
- }
- else {
- if (j == c.quantidade) {
- diferenca.inserirValor(this.vetor[i]);
- }
- }
- }
- }
- quickSort(diferenca.vetor,0,diferenca.vetor.length-1);
- return diferenca;
- }
- }
- int getLength() {
- return this.vetor.length;
- }
- void imprimirVetor() {
- String msg = "[";
- for (int x = 0; x <= getLength()-1; x++) {
- if (x == getLength()-1)
- msg += this.vetor[x] + "]";
- else
- msg += this.vetor[x] + ",";
- }
- System.out.println(msg);
- }
- void quickSort(int[] vetor, int inicio, int fim) {
- if (inicio < fim) {
- int posicaoPivo = separar(vetor, inicio, fim);
- quickSort(vetor, inicio, posicaoPivo - 1);
- quickSort(vetor, posicaoPivo + 1, fim);
- }
- }
- int separar(int[] vetor, int inicio, int fim) {
- int pivo = vetor[inicio];
- int i = inicio + 1, f = fim;
- while (i <= f) {
- if (vetor[i] <= pivo)
- i++;
- else if (pivo < vetor[f])
- f--;
- else {
- int troca = vetor[i];
- vetor[i] = vetor[f];
- vetor[f] = troca;
- i++;
- f--;
- }
- }
- vetor[inicio] = vetor[f];
- vetor[f] = pivo;
- return f;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement