Advertisement
NeverRIEght

ConvertToDNF

Dec 17th, 2023 (edited)
821
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.88 KB | Software | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4.  
  5.     public static void main(String[] args) {
  6.         Scanner sc = new Scanner(System.in);
  7.  
  8.         printTableOfTruth(convertFromCNFToTable(sc));
  9.         //System.out.println(convertFromTableToDNF(sc));
  10.  
  11.  
  12.         sc.close();
  13.     }
  14.  
  15.     public static int[][] convertFromCNFToTable(Scanner sc) {
  16.         String input = sc.nextLine();
  17.         input = input.replaceAll(" ","");
  18.  
  19.         System.out.println(input);
  20.  
  21.         String[] arrayOfAdds = new String[32];
  22.  
  23.         if(input.contains(")(")) {
  24.             arrayOfAdds = input.split("\\)\\(");
  25.             System.out.println(arrayOfAdds.length);
  26. //            for(int i = 0; i < arrayOfAdds.length; i++) {
  27. //                arrayOfAdds[i] = arrayOfAdds[i].replaceAll("\\(","");
  28. //                arrayOfAdds[i] = arrayOfAdds[i].replaceAll("\\)","");
  29. //            }
  30.         }
  31.  
  32.  
  33.  
  34.  
  35.         int[][] tableOfTruth = new int[32][6];
  36.  
  37.         for(int i = 0; i < arrayOfAdds.length; i++) {
  38.             if(arrayOfAdds[i].contains("~a")) {
  39.                 tableOfTruth[i][0] = 1;
  40.             } else {
  41.                 tableOfTruth[i][0] = 0;
  42.             }
  43.  
  44.             if(arrayOfAdds[i].contains("~b")) {
  45.                 tableOfTruth[i][1] = 1;
  46.             } else {
  47.                 tableOfTruth[i][1] = 0;
  48.             }
  49.  
  50.             if(arrayOfAdds[i].contains("~c")) {
  51.                 tableOfTruth[i][2] = 1;
  52.             } else {
  53.                 tableOfTruth[i][2] = 0;
  54.             }
  55.  
  56.             if(arrayOfAdds[i].contains("~d")) {
  57.                 tableOfTruth[i][3] = 1;
  58.             } else {
  59.                 tableOfTruth[i][3] = 0;
  60.             }
  61.  
  62.             if(arrayOfAdds[i].contains("~e")) {
  63.                 tableOfTruth[i][4] = 1;
  64.             } else {
  65.                 tableOfTruth[i][4] = 0;
  66.             }
  67.         }
  68.  
  69.         return tableOfTruth;
  70.     }
  71.     public static void printTableOfTruth(int[][] tableOfTruth) {
  72.         System.out.println("a\tb\tc\td\te\tf");
  73.         for(int i = 0; i < tableOfTruth.length; i++) {
  74.             for(int j = 0; j < tableOfTruth[i].length; j++) {
  75.                 System.out.print(tableOfTruth[i][j] + "\t");
  76.             }
  77.             System.out.println();
  78.         }
  79.     }
  80.     public static String convertFromTableToDNF(Scanner sc) {
  81.         // Устанавливаем в качестве разделителя символ новой строки
  82.         sc.useDelimiter("\n");
  83.  
  84.         // Считываем многострочный текст
  85.         System.out.println("Введите многострочный текст (для завершения введите Ctrl+D в конце):");
  86.  
  87.         int lineCounter = 0;
  88.         String outputDNF = "";
  89.  
  90.         while (lineCounter < 32) {
  91.             String line = sc.next();
  92.             lineCounter++;
  93.             if(!convertTableStringToDNF(line).equals("")) {
  94.                 outputDNF += convertTableStringToDNF(line) + "+";
  95.             }
  96.         }
  97.  
  98.         return outputDNF.substring(0, outputDNF.length() - 1);
  99.     }
  100.  
  101.     public static String convertTableStringToDNF (String input) {
  102.         String[] arr = input.split("\t");
  103.  
  104.         if(arr[5].equals("0")) {
  105.             return "";
  106.         }
  107.  
  108.         String output = "";
  109.  
  110.         if(arr[0].equals("1")) {
  111.             output += "a";
  112.         } else {
  113.             output += "~a";
  114.         }
  115.  
  116.         if(arr[1].equals("1")) {
  117.             output += "b";
  118.         } else {
  119.             output += "~b";
  120.         }
  121.  
  122.         if(arr[2].equals("1")) {
  123.             output += "c";
  124.         } else {
  125.             output += "~c";
  126.         }
  127.  
  128.         if(arr[3].equals("1")) {
  129.             output += "d";
  130.         } else {
  131.             output += "~d";
  132.         }
  133.  
  134.         if(arr[4].equals("1")) {
  135.             output += "e";
  136.         } else {
  137.             output += "~e";
  138.         }
  139.  
  140.         return output;
  141.     }
  142. }
  143.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement