Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import java.io.File;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- public class Main {
- static Scanner scanner;
- static String getInputType() {
- String ret = "";
- boolean isIncorrect;
- String inputLine = "";
- isIncorrect = true;
- do {
- System.out.println("Выберите способ задания матрицы файл/консоль (ф/к)");
- inputLine = scanner.nextLine();
- if (inputLine.equalsIgnoreCase("файл") || inputLine.equalsIgnoreCase("ф")) {
- ret = "File";
- isIncorrect = false;
- } else if (inputLine.equalsIgnoreCase("консоль") || inputLine.equalsIgnoreCase("к")) {
- ret = "Console";
- isIncorrect = false;
- }
- } while (isIncorrect);
- return ret;
- }
- static int getMatrixItem(int i, int j) {
- int ret = 0;
- boolean isIncorrect;
- String inputLine;
- do {
- System.out.println("Введите значение элемента матрицы [" + i + "," + j + "] ");
- inputLine = scanner.nextLine();
- isIncorrect = false;
- try {
- ret = Integer.parseInt(inputLine);
- } catch (Exception ex) {
- System.out.println("Значение матрицы должно быть числом\n");
- isIncorrect = true;
- }
- } while (isIncorrect);
- return ret;
- }
- static int[][] getMatrixFromConsole(int size) {
- int[][] ret = new int[size][size];
- for (int i = 0; i < size; i++) {
- for (int j = 0; j < size; j++) {
- ret[i][j] = getMatrixItem(i, j);
- }
- }
- return ret;
- }
- static boolean isFileCorrect(String path, int size) throws IOException {
- int totalCount = 0;
- boolean isCorrect;
- FileReader reader = new FileReader(path);
- Scanner scanner = new Scanner(reader);
- isCorrect = true;
- while (scanner.hasNextLine() && isCorrect) {
- Matcher matcherDigit = Pattern.compile("\\d+").matcher(scanner.nextLine());
- Matcher matcherSymbol = Pattern.compile("\\w+").matcher(scanner.nextLine());
- int thisLineCount = 0;
- if (matcherSymbol.find())
- isCorrect = false;
- while (matcherDigit.find()) {
- thisLineCount++;
- totalCount++;
- }
- if (thisLineCount != size && totalCount != size * size) {
- isCorrect = false;
- }
- }
- scanner.close();
- return isCorrect;
- }
- static String getMatrixFilePath(int matrixSize) throws IOException {
- String ret;
- boolean isIncorrect;
- isIncorrect = true;
- do {
- System.out.println("Введите абсолютный путь к файлу ");
- ret = scanner.nextLine();
- File matrixFile = new File(ret);
- if (!matrixFile.exists()) {
- System.out.println("Файл не найден");
- } else {
- if (isFileCorrect(ret, matrixSize)) {
- isIncorrect = false;
- } else {
- System.out.println("Данные в файле некорректны");
- }
- }
- } while (isIncorrect);
- return ret;
- }
- static int[][] getMatrixFromFile(int size) throws IOException {
- int[][] ret = new int[size][size];
- String filePath;
- filePath = getMatrixFilePath(size);
- FileReader matrixFile = new FileReader(filePath);
- Scanner scanner = new Scanner(matrixFile);
- for (int i = 0; i < size; i++) {
- for (int j = 0; j < size; j++) {
- ret[i][j] = scanner.nextInt();
- }
- scanner.nextLine();
- }
- scanner.close();
- return ret;
- }
- static int getMatrixSize() {
- int ret = 0;
- boolean isIncorrect;
- String inputLine;
- do {
- System.out.println("Введите размер матрицы ");
- isIncorrect = false;
- inputLine = scanner.nextLine();
- try {
- ret = Integer.parseInt(inputLine);
- } catch (Exception ex) {
- System.out.println("Размер матрицы должен быть числом");
- isIncorrect = true;
- }
- if ((((ret < 2) || (ret > 10000)) || (ret % 2 == 1)) && !isIncorrect) {
- System.out.println("Размер матрицы должен быть кратен двум и принадлежать промежутку от 2 до 10000");
- isIncorrect = true;
- }
- } while (isIncorrect);
- return ret;
- }
- static int[][] getMatrix() throws IOException {
- int size = 0;
- String inputType;
- size = getMatrixSize();
- int[][] retMatrix = new int[size][size];
- inputType = getInputType();
- if (inputType == "Console") {
- retMatrix = getMatrixFromConsole(size);
- } else if (inputType == "File") {
- retMatrix = getMatrixFromFile(size);
- }
- return retMatrix;
- }
- static String getOutputDirectory() {
- String ret;
- boolean isIncorrect;
- isIncorrect = true;
- do {
- System.out.println("Введите директорию, в которую хотите сохранить матрицу");
- ret = scanner.nextLine();
- File outputDirectory = new File(ret);
- if (outputDirectory.isDirectory() && outputDirectory.exists()) {
- isIncorrect = false;
- } else {
- System.out.println("Такой директории не существует.Попробуйте ещё раз");
- }
- } while (isIncorrect);
- return ret;
- }
- static void printMatrix(int[][] matrixToPrint) {
- for (int i = 0; i < matrixToPrint.length; i++) {
- for (int j = 0; j < matrixToPrint.length; j++) {
- System.out.printf("%6d", matrixToPrint[i][j]);
- }
- System.out.println();
- }
- System.out.println();
- }
- static void printMatrixToFile(int[][] matrixToPrint) throws IOException {
- String path;
- path = getOutputDirectory();
- FileWriter myWriter = new FileWriter(path + "\\output.txt");
- for (int i = 0; i < matrixToPrint.length; i++) {
- for (int j = 0; j < matrixToPrint.length; j++) {
- myWriter.write(String.format("%6d", matrixToPrint[i][j]));
- }
- myWriter.write("\n");
- }
- myWriter.close();
- System.out.println("Матрица сохранена по указанному пути");
- }
- static int[][] getMatrixPart(int[][] matrix, int startI, int startJ, int size) {
- int[][] ret = new int[size][size];
- for (int i = 0; i < size; i++)
- for (int j = 0; j < size; j++)
- ret[i][j] = matrix[i + startI][j + startJ];
- return ret;
- }
- static int[][] insertMatrix(int[][] mainMatrix, int[][] toInsert, int startI, int startJ) {
- for (int i = 0; i < toInsert.length; i++) {
- for (int j = 0; j < toInsert.length; j++) {
- mainMatrix[startI + i][startJ + j] = toInsert[i][j];
- }
- }
- return mainMatrix;
- }
- static void swap(int[][] toSwap) {
- int[][] matrix1;
- int[][] matrix2;
- int[][] matrix3;
- int[][] matrix4;
- int halfSize;
- halfSize = toSwap.length / 2;
- matrix1 = getMatrixPart(toSwap, 0, 0, halfSize);
- matrix2 = getMatrixPart(toSwap, 0, halfSize, halfSize);
- matrix3 = getMatrixPart(toSwap, halfSize, 0, halfSize);
- matrix4 = getMatrixPart(toSwap, halfSize, halfSize, halfSize);
- insertMatrix(toSwap, matrix1, halfSize, halfSize);
- insertMatrix(toSwap, matrix2, halfSize, 0);
- insertMatrix(toSwap, matrix3, 0, 0);
- insertMatrix(toSwap, matrix4, 0, halfSize);
- }
- public static void main(String[] args) throws IOException {
- scanner = new Scanner(System.in);
- int[][] matrix = getMatrix();
- printMatrix(matrix);
- swap(matrix);
- printMatrix(matrix);
- printMatrixToFile(matrix);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement