Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.io.*;
- public class Permutations {
- private static final Scanner scanner = new Scanner(System.in);
- static Scanner fileScanner;
- static int num = 1; // номер перестановки
- public static void main (String[] args) {
- int num;
- int[] arr;
- System.out.println("the program generates all permutations of n elements in lexicographic order.");
- 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);
- }
- output(num, arr);
- }
- 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 = 2;
- final int MAX_SIZE = 7;
- 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 2 to 7:");
- 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 = 7;
- 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 2 to 7:");
- 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 void swap(int[] arr, int i, int j)
- {
- int temp = arr[i];
- arr[i] = arr[j];
- arr[j] = temp;
- }
- public static boolean nextSet(int[] arr, int n)
- {
- int j = n - 2;
- while (j != -1 && arr[j] >= arr[j + 1])
- j--;
- if (j == -1)
- return false; //закончились
- int k = n - 1;
- while (arr[j] >= arr[k])
- k--;
- swap(arr, j, k);
- int l = j + 1;
- int r = n - 1;
- while (l < r)
- swap(arr, l++, r--);
- return true;
- }
- public static void print (int[] arr, int n)
- {
- System.out.print((num++) + ": ");
- for (int i = 0; i < n; i++)
- System.out.print(arr[i] + " ");
- System.out.println();
- }
- public static void output (int num, int[] arr) {
- System.out.println(num + " elements:");
- print(arr, num);
- while (nextSet(arr, num))
- print(arr, num);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement