Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.io.*;
- public class moveMatrix {
- private static final Scanner scanner = new Scanner(System.in);
- private static Scanner fileScanner;
- public static void main(String[] args) {
- int n;
- int[][] arr;
- System.out.println("Enter 1 or 0. " + '\n' + "1 is console input, 0 is file input.");
- int chose = choose();
- if (chose == 1) {
- System.out.println("Input matrix size: ");
- n = inputArraySize();
- System.out.println("Enter the numbers one by one: ");
- arr = inputArray(n);
- } else {
- String str = inputFilePath();
- n = inputSizeOfMatrixFromFile(str);
- arr = inputMatrixFile(str,n);
- }
- int starti = 0;
- int startj = starti;
- int[] bufferFor1 = transferMatrixVector(n, arr, starti, startj);
- starti = 0;
- startj = n / 2;
- int[] bufferFor2 = transferMatrixVector(n, arr, starti, startj);
- starti = n / 2;
- startj = 0;
- int[] bufferFor3 = transferMatrixVector(n, arr, starti, startj);
- starti = n / 2;
- startj = starti;
- int[] bufferFor4 = transferMatrixVector(n, arr, starti, startj);
- int i;
- int j;
- int k = 0;
- for (i = 0; i < n; i++) {
- for (j = 0; j < n; j++) {
- if ((i < n / 2) && (j < n / 2))
- arr[i][j] = bufferFor4[k];
- k = 0;
- if ((i < n / 2) && (j > n / 2 - 1))
- arr[i][j] = bufferFor3[k];
- if ((i > n / 2 - 1) && (j < n / 2))
- arr[i][j] = bufferFor1[k];
- if ((i > n / 2 - 1) && (j > n / 2 - 1))
- arr[i][j] = bufferFor2[k];
- k++;
- }
- }
- System.out.println("Enter 1 or 0.");
- System.out.println("1 is console output, 0 is file output.");
- chose = choose();
- if (chose == 1)
- outputArray(n, arr);
- else {
- String str = inputFilePath();
- fileOutput(arr, n, str);
- }
- scanner.close();
- fileScanner.close();
- }
- public static int choose() {
- int chose = 0;
- boolean isIncorrect;
- do {
- isIncorrect = false;
- try {
- chose = Integer.parseInt(scanner.nextLine());
- } catch (Exception e) {
- isIncorrect = true;
- System.out.println("Please, enter a positive integer number:");
- }
- if ((isIncorrect) || !((chose == 1) || (chose == 0))) {
- isIncorrect = true;
- System.out.println("Please, choose.");
- System.out.println("1 is console input/output, 0 is file input/output.");
- }
- } while (isIncorrect);
- return chose;
- }
- // консольный ввод и вывод. Функции для них
- public 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 positive integer number:");
- }
- } while (isIncorrect);
- return n;
- }
- public static int inputPositiveNumber () {
- int npos;
- final int MIN = 1;
- boolean isIncorrect;
- do {
- npos = inputData();
- isIncorrect = false;
- if (npos < MIN) {
- System.out.println("Please, enter a positive number.");
- isIncorrect = true;
- }
- } while (isIncorrect);
- return npos;
- }
- public static int inputArraySize() {
- boolean isIncorrect;
- int num;
- do {
- num = inputPositiveNumber();
- isIncorrect = false;
- if (num % 2 != 0) {
- System.out.println("Please, enter an even number:");
- isIncorrect = true;
- }
- } while (isIncorrect);
- return num;
- }
- public static int[][] inputArray(int num) {
- int i;
- int j;
- int[][] arr = new int[num][num];
- for (i = 0; i < num; i++)
- for (j = 0; j < num; j++)
- arr[i][j] = inputData();
- return arr;
- }
- public static int[] transferMatrixVector(int n, int[][] arr, int starti, int startj) {
- final int min = 4;
- int[] quarter = new int[(n * n) / min];
- int k = 0;
- int i;
- int j;
- int halfn = n / 2;
- for (i = starti; i < starti + halfn; i++) {
- for (j = startj; j < startj + halfn; j++) {
- quarter[k] = arr[i][j];
- k++;
- }
- }
- return quarter;
- }
- public static void outputArray(int num, int[][] arr) {
- int i;
- int j;
- for (i = 0; i < num; i++) {
- for (j = 0; j < num; j++)
- System.out.print(arr[i][j] + " ");
- System.out.print('\n');
- }
- }
- //functions for files:
- public 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;
- }
- public static int inputSizeOfMatrixFromFile(String path) {
- int n = 0;
- boolean isIncorrect;
- final int MIN = 1;
- final int MAX = 10;
- do {
- isIncorrect = false;
- try {
- fileScanner = new Scanner(new File(path));
- n = fileScanner.nextInt();
- } catch (Exception q) {
- isIncorrect = true;
- System.out.println("Check file");
- path = inputFilePath();
- }
- if (!isIncorrect && (n < MIN || n > MAX)) {
- isIncorrect = true;
- System.out.println("Matrix size should be at least 2");
- }
- } while (isIncorrect);
- return n;
- }
- public static int[][] inputMatrixFile(String path, int num) {
- boolean isIncorrect;
- int i, j;
- int[][] arrFile = new int[num][num];
- for (i = 0; i < num; i++)
- arrFile[i] = new int[num];
- do {
- isIncorrect = false;
- try {
- fileScanner = new Scanner(new File(path));
- fileScanner.nextLine();
- for (i = 0; (i < num); i++) {
- for (j = 0; (j < num); j++) {
- arrFile[i][j] = Integer.parseInt(fileScanner.next());
- }
- }
- } catch (Exception q) {
- isIncorrect = true;
- System.out.println("Reading of matrix elements failed.");
- path = inputFilePath();
- }
- } while (isIncorrect);
- return arrFile;
- }
- private static void fileOutput(int[][] arr, int num, String path) {
- boolean isIncorrect;
- do {
- isIncorrect = false;
- try {
- FileWriter writer = new FileWriter(path);
- for (int i = 0; i < num; i++){
- for (int j = 0; j < num; j++) {
- writer.write((arr[i][j]) + " ");
- }
- writer.write('\n');
- }
- writer.close();
- } catch (IOException e) {
- isIncorrect = true;
- System.out.println("Mistake of output in file. Input path.");
- path = inputFilePath();
- }
- } while(isIncorrect);
- System.out.println("Successful output in file.");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement