Last active
January 8, 2018 02:58
-
-
Save henrycunh/316aa1975ababe03054be31b7212c56b to your computer and use it in GitHub Desktop.
Exercicio de Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.Scanner; | |
/** Os conjuntos possuem termos, que por sua vez são | |
* apenas um Array de Strings, contendo a quantidade | |
* de items determinada | |
* */ | |
public class ExclusaoMult { | |
public static ArrayList<String[]> termos = new ArrayList<String[]> (); // Lista que contém os termos | |
public static ArrayList<String> itens = new ArrayList<String>(); | |
public static ArrayList<String> naoItens = new ArrayList<String>(); | |
public static Scanner sc = new Scanner(System.in); | |
public static int numItens = 0; | |
public static int numTermos = 0; | |
public static void main(String[] args) { | |
lerTermos(); | |
print( calcular() ? "É tautologia" : "Não é tautologia"); | |
} | |
private static void lerTermos() { | |
print( "Quantidade de itens em um termo? (e.g. o termo [A, !B] possui dois itens, A e !B)\n" ); | |
print( "\t> " ); | |
numItens = sc.nextInt(); | |
print ( "Quantos termos você quer ler?\n" ); | |
print( "\t> " ); | |
numTermos = sc.nextInt(); | |
print("Digite os itens dos termos separados por espaço (e.g. A !B C para um termo [A, !B, C])\n"); | |
for( int i = 0; i < numTermos; i++ ) { | |
print("\t> "); | |
String termo[] = new String[numItens]; | |
for( int j = 0; j < numItens; j++ ) termo[j] = sc.next(); // Entrada de itens para o termo | |
// Verifica se o termo é inválido | |
while( isInvalid( termo ) ) { | |
print("\tTermo inválido, tente novamente\n\t> "); | |
for( int j = 0; j < numItens; j++ ) termo[j] = sc.next(); // Entrada de itens para o termo | |
} | |
termos.add( termo ); // Adiciona termo na lista | |
for (int j = 0; j < termo.length; j++) | |
if( termo[j].length() == 2) | |
naoItens.add(termo[j].substring(1)); | |
else | |
itens.add( termo[j] ); | |
} | |
} | |
private static void print(Object o) { System.out.print( o ); } | |
/** | |
* Verifica se o termo é inválido, ou seja, se todos os items não são diferentes | |
* | |
* @param arr termo a ser análisado | |
* @return boolean indicando se o termo é válido | |
*/ | |
private static boolean isInvalid(String[] arr) { | |
// Verifica se todos são iguais | |
int numIguais = 0; | |
for (int i = 0; i < numItens - 1; i++) | |
if( arr[i].equals( arr[i + 1] ) ) // Se um item for igual ao próximo, registra incidência | |
numIguais++; | |
if( numIguais == numItens ) return true; // Se o número de incidências for igual ao número de itens, é inválido | |
// Verifica se existe algum item anulando outro item dentro do termo | |
for( int i = 0; i < numItens; i++ ) { | |
String item = arr[i].length() == 2 ? arr[i].substring(1) : arr[i]; // Item atual | |
for( int j = 0; j < numItens; j++ ) { | |
if(i == j) continue; // Verifica se o item está se comparando a si mesmo | |
String adv = arr[j].length() == 2 ? arr[j].substring(1) : arr[j]; // Item atual | |
if( item.equals( adv ) ) return true; // Compara com outro item, e retorna true se for igual | |
} | |
} | |
// Verifica se o item já existe | |
for (int i = 0; i < arr.length; i++) | |
if( arr[i].length() == 2 ) { | |
if( naoItens.contains( arr[i].substring(1) ) ) return true; | |
} else { | |
if( itens.contains( arr[i] ) ) return true; | |
} | |
return false; | |
} | |
/** | |
* Verifica se a lista de termos é tautologia | |
* | |
* @return boolean indicando se a lista de termos é uma tautologia */ | |
private static boolean calcular() { | |
// Verifica se possue tantos itens quanto não itens, se for diferente, retorna falso | |
if( itens.size() != naoItens.size() ) return false; | |
int anulamentos = 0; | |
// Remove todos os não itens que coincidem com um item, e aumenta o contador do ocorrido | |
for( int i = 0; i < itens.size(); i++ ) if( naoItens.remove( itens.get( i ) ) ) anulamentos++; | |
// Se a quantidade de anulamentos for igual à quantidade de itens, e não existirem mais negatórias, | |
// é uma tautologia | |
return itens.size() - anulamentos == 0 && naoItens.isEmpty(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment