Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.HashSet;
- import java.util.Scanner;
- import java.io.*;
- import java.util.Set;
- public class Main {
- private static final Scanner scanner = new Scanner(System.in);
- static Scanner fileScanner;
- public static void main (String[] args) {
- int num;
- int[] arr;
- System.out.println("Program generates all subsets of the set of n elements.");
- System.out.println("Enter type of input." + '\n' + "1 is console input, 0 is file input.");
- String path;
- boolean chose = choose();
- if (chose) {
- System.out.println("Input size of plurality:");
- num = inputArraySize();
- System.out.println("Input plurality elements:");
- arr = inputArray(num);
- } else {
- path = inputFilePath();
- num = inputSizeOfArrayFromFile(path);
- arr = inputArrayFile(path, num);
- }
- ArrayList <ArrayList<Integer>> subsets = getSubsets(arr);
- output(subsets);
- }
- private static boolean choose() {
- int inputNumber = 0;
- boolean isIncorrect;
- final int MIN_NUM = 0;
- final int MAX_NUM = 1;
- do {
- isIncorrect = false;
- try {
- inputNumber = Integer.parseInt(scanner.nextLine());
- } catch (Exception e) {
- isIncorrect = true;
- System.out.println("Please, enter a number.");
- }
- if (!isIncorrect && (inputNumber < MIN_NUM || inputNumber > MAX_NUM)) {
- System.out.println("You are out of input range!");
- isIncorrect = true;
- }
- } while (isIncorrect);
- return inputNumber == 1;
- }
- private static int inputData() {
- int n = 0;
- boolean isIncorrect;
- do {
- isIncorrect = false;
- try {
- n = Integer.parseInt(scanner.nextLine());
- } catch (Exception e) {
- isIncorrect = true;
- System.out.println("Please, enter a integer number:");
- }
- } while (isIncorrect);
- return n;
- }
- private static int inputArraySize() {
- boolean isIncorrect;
- final int MIN_SIZE = 1;
- final int MAX_SIZE = 10;
- int num;
- do {
- num = inputData();
- isIncorrect = false;
- if ((num < MIN_SIZE) || (num > MAX_SIZE)) {
- System.out.println("Array size should be in the range from 1 to 10:");
- isIncorrect = true;
- }
- } while (isIncorrect);
- return num;
- }
- private static int[] inputArray(int num) {
- int[] arr = new int[num];
- for (int i = 0; i < arr.length; i++)
- arr[i] = inputData();
- return arr;
- }
- private static String inputFilePath() {
- String path;
- boolean isIncorrect;
- do {
- isIncorrect = false;
- System.out.println("Input file path:");
- path = scanner.nextLine();
- File file = new File(path);
- if (!file.exists()) {
- System.out.println("Wrong way to file.");
- isIncorrect = true;
- }
- if (!file.canRead() && file.exists()) {
- System.out.println("Impossible to read a file.");
- isIncorrect = true;
- }
- if (!file.canWrite() && file.canRead()) {
- System.out.println("Impossible to write a file.");
- isIncorrect = true;
- }
- } while (isIncorrect);
- return path;
- }
- private static int inputSizeOfArrayFromFile(String path) {
- int num = 0;
- boolean isIncorrect;
- final int MIN = 1;
- final int MAX = 10;
- do {
- isIncorrect = false;
- try {
- fileScanner = new Scanner(new File(path));
- num = fileScanner.nextInt();
- } catch (Exception q) {
- isIncorrect = true;
- System.out.println("Check file.");
- path = inputFilePath();
- }
- if (!isIncorrect && ((num < MIN) || (num > MAX))) {
- isIncorrect = true;
- System.out.println("Array size should be in the range from 1 to 10:");
- path = inputFilePath();
- }
- } while (isIncorrect);
- return num;
- }
- private static int[] inputArrayFile(String path, int num) {
- boolean isIncorrect;
- int[] arr = new int[num];
- do {
- isIncorrect = false;
- try {
- fileScanner = new Scanner(new File(path));
- fileScanner.nextLine();
- for (int i = 0; (i < arr.length); i++) {
- arr[i] = fileScanner.nextInt();
- }
- } catch (Exception q) {
- isIncorrect = true;
- System.out.println("Reading of array elements failed.");
- path = inputFilePath();
- }
- } while (isIncorrect);
- return arr;
- }
- public static int[] removeRepetitions (int[] set) {
- Set<Integer> integerSet = new HashSet<>();
- for (int i: set) {
- integerSet.add(i);
- }
- int i = 0;
- int[] newSet = new int[integerSet.size()];
- for (int element: integerSet)
- newSet[i++] = element;
- return newSet;
- }
- public static ArrayList <ArrayList<Integer>> getSubsets (int[] set) {
- set = removeRepetitions(set);
- ArrayList<ArrayList<Integer>> allSubsets = new ArrayList<>();
- int max = 1 << set.length;
- for (int i = 0; i < max; i++) {
- ArrayList<Integer> subset = new ArrayList<>();
- int k = i;
- int index = 0;
- while (k > 0) {
- if ((k & 1) == 1) {
- subset.add(set[index]);
- }
- k >>= 1;
- index++;
- }
- allSubsets.add(subset);
- }
- return allSubsets;
- }
- public static void output (ArrayList<ArrayList<Integer>> allSubsets) {
- System.out.println("All plurality's subsets:");
- for (ArrayList<Integer> subset : allSubsets) {
- System.out.println(subset);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement