Egor_Vakar

(Java) lab 4.2 Main

Feb 23rd, 2022 (edited)
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.04 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.ArrayList;
  3. import java.util.Scanner;
  4. import java.util.Iterator;
  5.  
  6. public class lab4_2 {
  7.     static Scanner scan = new Scanner(System.in);
  8.  
  9.     static int counter = 0;
  10.  
  11.     public static void main(String[] args) {
  12.         System.out.print("Welcome to the program\nSelect the source for entering numbers:\n1:Console\n2:File\nEnter 1 or 2: ");
  13.         int inputSource = takeSource();
  14.         String path = "";
  15.         if (inputSource == 2){
  16.             path = takeInPath();
  17.         }
  18.         int n = takeNum(inputSource, path, 1);
  19.         int[] arr = fillArr(n);
  20.         int sum = takeNum(inputSource,path, 2);
  21.         System.out.print("Select the source for output:\n1:Console\n2:File\nEnter 1 or 2: ");
  22.         int outSource = takeSource();
  23.         int[][] answer = new int[1000][n];
  24.         answer = findAllSubsets(arr, n, sum, null, answer);
  25.         output(answer, outSource, n);
  26.     }
  27.  
  28.     static void output (final int[][] answer, int source, int n){
  29.         String outPath;
  30.         if (source == 1) {
  31.             consoleOutput(answer, n);
  32.         } else {
  33.             outPath = takeOutPath();
  34.             FileOutput(outPath, answer, n);
  35.         }
  36.     }
  37.  
  38.     static void consoleOutput(final int[][] answer,int n){
  39.         int i = 0;
  40.         int j = 0;
  41.         System.out.println("Answer:");
  42.         while ((j < n) && (answer[i][j] != 0)){
  43.             while ((j < n) && (answer[i][j] != 0)){
  44.                 System.out.print("[" + answer[i][j]+"] ");
  45.                 j++;
  46.             }
  47.             i++;
  48.             j = 0;
  49.             System.out.println();
  50.         }
  51.     }
  52.  
  53.     static void FileOutput(final String path, final int[][] answer, int n) {
  54.         try (FileWriter fw = new FileWriter(path)) {
  55.             int i = 0;
  56.             int j = 0;
  57.             fw.write("Answer:\n");
  58.             while ((i < n) && (answer[i][j] != 0)){
  59.                 while ((i < n) && (answer[i][j] != 0)){
  60.                     fw.write("[" + answer[i][j]+"] ");
  61.                     j++;
  62.                 }
  63.                 i++;
  64.                 j = 0;
  65.                 fw.write("\n");
  66.             }
  67.             System.out.println("Done");
  68.         } catch (Exception e) {
  69.             System.out.println("File error!");
  70.         }
  71.     }
  72.  
  73.     static int takeSource() {
  74.         final byte CONSOLE = 1;
  75.         final byte FILE = 2;
  76.         boolean isIncorrect;
  77.         byte choice = 0;
  78.         do {
  79.             isIncorrect = false;
  80.             try {
  81.                 choice = Byte.parseByte(scan.nextLine());
  82.             } catch (Exception e) {
  83.                 System.out.print("Incorrect input!!!\nSelect the source:\n1:Console\n2:File\nEnter 1 or 2: ");
  84.                 isIncorrect = true;
  85.             }
  86.             if (!isIncorrect && (choice != CONSOLE) && (choice != FILE)) {
  87.                 System.out.print("Incorrect input!!!\nSelect the source:\n1:Console\n2:File\nEnter 1 or 2: ");
  88.                 isIncorrect = true;
  89.             }
  90.         } while (isIncorrect);
  91.         return choice;
  92.     }
  93.  
  94.     static String takeInPath() {
  95.         String path;
  96.         boolean isIncorrect;
  97.         System.out.print("Enter file path: ");
  98.         do {
  99.             isIncorrect = false;
  100.             path = scan.nextLine();
  101.             File file = new File(path);
  102.             if (!file.exists()) {
  103.                 System.out.print("File is not found\nEnter file path: ");
  104.                 isIncorrect = true;
  105.             }
  106.             if (!isIncorrect && (!path.endsWith(".txt"))) {
  107.                 System.out.print("File is found, but it is not \".txt\" type file\nEnter file path: ");
  108.                 isIncorrect = true;
  109.             }
  110.         } while (isIncorrect);
  111.         return path;
  112.     }
  113.  
  114.     static String takeOutPath() {
  115.         String path;
  116.         boolean isIncorrect;
  117.         System.out.print("Enter file path: ");
  118.         do {
  119.             isIncorrect = false;
  120.             path = scan.nextLine();
  121.             if (!path.endsWith(".txt")) {
  122.                 System.out.print("It should be a \".txt\" file\nEnter file path: ");
  123.                 isIncorrect = true;
  124.             }
  125.         } while (isIncorrect);
  126.         return path;
  127.     }
  128.  
  129.     private static int[] fillArr(final int n) {
  130.         int[] arr = new int[n];
  131.         for (int i = 0; i < n; i++) {
  132.             arr[i] = i + 1;
  133.         }
  134.         return arr;
  135.     }
  136.  
  137.     private static int takeNum(final int inputSource, String path, final int goal) {
  138.         int num;
  139.         if (inputSource == 1) {
  140.             num = takeInt(1, (goal == 1) ? 15 : 100, goal);
  141.         } else {
  142.             num = takeNumFromFile(path, goal);
  143.             while (num == 0) {
  144.                 path = takeInPath();
  145.                 num = takeNumFromFile(path, goal);
  146.             }
  147.         }
  148.         return num;
  149.     }
  150.  
  151.     private static int takeNumFromFile(final String path, final int goal) {
  152.         final int SET_LINE_N = 1;
  153.         int lineCounter = 0;
  154.         String line;
  155.         boolean isCorrect = true;
  156.         int num = 0;
  157.         try (BufferedReader reader = new BufferedReader(new FileReader(path))) {
  158.             while ((isCorrect) && ((line = reader.readLine()) != null)) {
  159.                 lineCounter++;
  160.                 if ((lineCounter == SET_LINE_N) && (goal == 1)){
  161.                     try {
  162.                         num = Integer.parseInt(line);
  163.                     } catch (NumberFormatException e) {
  164.                         System.out.println("Incorrect file content!!! Incorrect information in the set line!!!");
  165.                         isCorrect = false;
  166.                     }
  167.                 }
  168.                 if ((lineCounter == SET_LINE_N + 1) && (goal == 2)){
  169.                     try {
  170.                         num = Integer.parseInt(line);
  171.                     } catch (NumberFormatException e) {
  172.                         System.out.println("Incorrect file content!!! Incorrect information in the set line!!!");
  173.                         isCorrect = false;
  174.                     }
  175.                 }
  176.             }
  177.         } catch (IOException e) {
  178.             System.out.println("Input/Output error!!!");
  179.             isCorrect = false;
  180.         }
  181.         if ((isCorrect) && (lineCounter > SET_LINE_N + 1)) {
  182.             System.out.println("Incorrect file content!!!");
  183.             isCorrect = false;
  184.         }
  185.         if (isCorrect) {
  186.             return num;
  187.         } else {
  188.             return 0;
  189.         }
  190.     }
  191.  
  192.     private static int takeInt(final int min, final int max, final int goal) {
  193.         boolean isIncorrect;
  194.         int num = -1;
  195.         do {
  196.             isIncorrect = false;
  197.             System.out.printf("Enter " + (goal == 1? "n" : "A") + " value in range (%s, %s): \n", min, max);
  198.             try {
  199.                 num = Integer.parseInt(scan.nextLine());
  200.             } catch (NumberFormatException e) {
  201.                 isIncorrect = true;
  202.             }
  203.             if (!isIncorrect) {
  204.                 isIncorrect = num > max || num < min;
  205.             }
  206.             System.out.print(isIncorrect ? "Incorrect input, try again!\n" : "");
  207.         } while (isIncorrect);
  208.         return num;
  209.     }
  210.  
  211.  
  212.     static void findSubsetsRec(int[] arr, int i, int sum, ArrayList<Integer> indexes, boolean[][] isFine, int[][] ans) {
  213.         if (i == 0 && sum != 0 && isFine[0][sum]) {
  214.             indexes.add(arr[i]);
  215.             Iterator<Integer> iterator = indexes.iterator();
  216.             for (int k = 0; k < indexes.size(); k++) {
  217.                 ans[counter][k] = indexes.get(k);
  218.             }
  219.             counter++;
  220.             indexes.clear();
  221.             return;
  222.         }
  223.         if (i == 0 && sum == 0) {
  224.             Iterator<Integer> iterator = indexes.iterator();
  225.             for (int k = 0; k < indexes.size(); k++) {
  226.                 ans[counter][k] = indexes.get(k);
  227.             }
  228.             counter++;
  229.             indexes.clear();
  230.             return;
  231.         }
  232.         if (isFine[i - 1][sum]) {
  233.             ArrayList<Integer> b = new ArrayList<>(indexes);
  234.             findSubsetsRec(arr, i - 1, sum, b, isFine, ans);
  235.         }
  236.         if (sum >= arr[i] && isFine[i - 1][sum - arr[i]]) {
  237.             indexes.add(arr[i]);
  238.             findSubsetsRec(arr, i - 1, sum - arr[i], indexes, isFine, ans);
  239.  
  240.         }
  241.     }
  242.  
  243.     static int[][] findAllSubsets(int[] arr, int n, int sum, boolean[][] isFine, int[][] ans) {
  244.         isFine = new boolean[n][sum + 1];
  245.         for (int i = 0; i < n; ++i) {
  246.             isFine[i][0] = true;
  247.         }
  248.         if (arr[0] <= sum)
  249.             isFine[0][arr[0]] = true;
  250.         ArrayList<Integer> p = new ArrayList<>();
  251.         for (int i = 1; i < n; ++i)
  252.             for (int j = 0; j < sum + 1; ++j) {
  253.                 if (arr[i] <= j)
  254.                     isFine[i][j] = isFine[i - 1][j] || isFine[i - 1][j - arr[i]];
  255.                 else isFine[i][j] = isFine[i - 1][j];
  256.             }
  257.         if (!isFine[n - 1][sum]) {
  258.             findAllSubsets(arr, n, sum - 1, isFine, ans);
  259.             return ans;
  260.         }
  261.         findSubsetsRec(arr, n - 1, sum, p, isFine, ans);
  262.         return ans;
  263.     }
  264. }
Add Comment
Please, Sign In to add comment