Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.ArrayList;
  3.  
  4. public class Logic{
  5. static boolean isFNC(String exp) {
  6. for(int i = 0; i < exp.length(); i++){
  7. if(exp.charAt(i) == '~' && exp.charAt(i+1) == '(') return false;
  8. if(exp.charAt(i) == '&' && (exp.charAt(i-1) != ')' || exp.charAt(i+1) != '(')) return false;
  9. if(exp.charAt(i) == '>' || exp.charAt(i) == '<') return false;
  10. }
  11. return true;
  12. }
  13. static boolean isHorn(String exp){
  14. int contador = 0; int aux = 0;
  15.  
  16. for(int i = 0; i < exp.length(); i++){
  17. if(exp.charAt(i) == '(') contador++;
  18. if(contador == 1 && exp.charAt(i) == 'v' && (exp.charAt(i+1) >= 'A' && exp.charAt(i+1) <= 'Z')) aux++;
  19. if(aux > 1) return false;
  20. if(exp.charAt(i) == ')') {
  21. contador = 0;
  22. aux = 0;
  23. }
  24. }
  25. return true;
  26. }
  27. boolean SAT(String exp) {
  28. ArrayList<String> clausulas = new ArrayList<String>();
  29. ArrayList<String> unity = new ArrayList<String>();
  30.  
  31. int aux1 = 0, aux2 = 0;
  32. String auux = " ";
  33.  
  34. for(int i = 0; i < exp.length(); i++) {
  35. if(exp.charAt(i-1) == '(') aux1 = i;
  36. if(exp.charAt(i+1) == ')') aux2 = i;
  37.  
  38. auux = exp.substring(aux1, aux2);
  39.  
  40. if(auux.length() > 2) clausulas.add(auux);
  41. else unity.add(auux);
  42. }
  43.  
  44. for(int i = 0; i < unity.size(); i++) {
  45. //BUSCA ENTRE AS CLAUSULAS UNITARIAS
  46. boolean neg = false;
  47. if(unity.get(i).length() > 1) neg = true;
  48.  
  49. for(int j = 0; j < unity.size(); j++) {
  50. if(j != i) {
  51. //Positio e negativo
  52. if(neg == false && unity.get(j).length() == 2 && (unity.get(j).charAt(1) == unity.get(i).charAt(0)))
  53. return false;
  54.  
  55. //Negativo e negativo
  56. else if(neg == true && unity.get(j).length() == 2 && (unity.get(j).charAt(1) == unity.get(i).charAt(1)))
  57. unity.remove(j);
  58.  
  59. //Positivo e positivo
  60. else if(neg == false && unity.get(j).length() == 1 && (unity.get(j).charAt(0) == unity.get(i).charAt(0)))
  61. unity.remove(j);
  62. }
  63. }
  64. //BUSCA ENTRE AS CLAUSULAS
  65. for(int k = 0; k < clausulas.size(); k++) {
  66. for(int l = 0; l < clausulas.get(k).length(); l++) {
  67. if(neg = false && (clausulas.get(k).charAt(l) == unity.get(i).charAt(0))) {
  68. if(clausulas.get(k).charAt(l-1) == '~') clausulas.remove(k);
  69. }
  70. }
  71. }
  72. }
  73. }
  74. public static void main(String args[]) {
  75. Scanner in = new Scanner(System.in);
  76.  
  77. int qtd_exp = in.nextInt();
  78. in.nextLine();
  79.  
  80. for(int i = 0; i < qtd_exp; i++){
  81. String exp = in.nextLine();
  82. String newExp = " ";
  83.  
  84. for(int j = 0; j < exp.length(); j++)
  85. if(exp.charAt(j) != ' ') newExp += exp.charAt(j);
  86. exp = newExp;
  87.  
  88. if(!isFNC(exp)) {
  89. System.out.printf("Problema #%d\n",i+1);
  90. System.out.println("Não está na FNC.\n");
  91. }
  92. else if(!isHorn(exp)) {
  93. System.out.printf("Problema #%d\n",i+1);
  94. System.out.println("Nem todas as cláusulas são de Horn.\n");
  95. }
  96. else {
  97. if(SAT(exp)) {
  98.  
  99. }
  100. else {
  101.  
  102. }
  103. }
  104. }
  105. }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement