Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package lab2_3_java;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.util.NoSuchElementException;
- import java.util.Objects;
- import java.util.Scanner;
- public class Main {
- private static void PrintCondition() {
- System.out.println("\nGoal: Input Matrix And add Rows into position 0 And n+1 with elements equal To 1 (use shift Rows).\n");
- }
- private static void PrintMatrix(int[][] matrix, int rows, int columns, String header) {
- System.out.println(header);
- for (int row = 0; row < rows; row++) {
- for (int column = 0; column < columns; column++) {
- System.out.print(matrix[row][column]);
- System.out.print(" ");
- }
- System.out.print("\n");
- }
- }
- private static int InputNumUnlimited() {
- int number;
- number = 0;
- boolean isValid;
- isValid = false;
- do {
- String line = ReadLine();
- try {
- number = Integer.parseInt(line);
- isValid = true;
- }
- catch (NumberFormatException e) {
- System.err.println("Unexpected symbol entered.");
- }
- } while (!isValid);
- return number;
- }
- private static int InputNumber(int min, int max) {
- int number = 0;
- boolean isValid = false;
- do {
- number = InputNumUnlimited();
- if (number < min) {
- System.err.println(String.format("Minimal allowed value is %d", min));
- }
- else if (number > max) {
- System.err.println(String.format("Maximum allowed value is %d", max));
- }
- else {
- isValid = true;
- }
- } while (!isValid);
- return number;
- }
- private static int[][] CreateMatrix(int rows, int columns, int defaultValue) {
- int[][] matrix;
- matrix = new int[rows][columns];
- for (int row = 0; row < rows; row++) {
- for (int column = 0; column < columns; column++) {
- matrix[row][column] = defaultValue;
- }
- }
- return matrix;
- }
- private static int[][] ResizeMatrix(int[][] matrix, int rows, int columns, int etraRows, int defaultValue) {
- int[][] outMatrix;
- int matrixRange;
- int outMatrixRows;
- outMatrixRows = rows + 2;
- outMatrix = CreateMatrix(outMatrixRows, columns, defaultValue); // выделение памяти и заполняет 1ми
- CopyMatrix(matrix, outMatrix, rows, columns);
- return outMatrix;
- }
- private static String ReadLine() {
- String line = null;
- try {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- line = br.readLine();
- }
- catch (IOException e) {
- System.err.println("Сonsole read operation failed.");
- }
- return line;
- }
- private static String ReadCondition() {
- String condition;
- boolean selected = false;
- System.out.println("Would you like to enter matrix manually [m] or get from file [f]?");
- do {
- condition = ReadLine();
- if (!Objects.equals(condition, "m") && !Objects.equals(condition, "f")) {
- System.out.println("Please type [m] or [f]\n");
- }
- else {
- selected = true;
- }
- } while (!selected);
- return condition;
- }
- private static int InputMatrixRange() {
- int matrixRange;
- System.out.println("Input matrix range (1...5):");
- matrixRange = InputNumber(1, 5);
- return matrixRange;
- }
- private static int[][] InputSourceMatrixManually() {
- int[][] matrix;
- int matrixRange = InputMatrixRange();
- matrix = CreateMatrix(matrixRange, matrixRange, 0);
- System.out.println("Input matrix values:");
- for (int row = 0; row < matrixRange; row++) {
- for (int column = 0; column < matrixRange; column++) {
- System.out.println(String.format("Input value for Row: %s Column: %s: ",row,column));
- matrix[row][column] = InputNumber(Integer.MIN_VALUE, Integer.MAX_VALUE);
- }
- }
- return matrix;
- }
- private static boolean IsFileExist(String fileName) {
- File f = new File(fileName);
- if (f.exists() && !f.isDirectory()) {
- return true;
- }
- return false;
- }
- private static boolean HasTxtExtension(String fileName) {
- int length = fileName.length();
- if (length < 5) {
- return false;
- }
- if (fileName.charAt(length - 4) != '.') {
- return false;
- }
- if (fileName.charAt(length - 3) != 't') {
- return false;
- }
- if (fileName.charAt(length - 2) != 'x') {
- return false;
- }
- if (fileName.charAt(length - 1) != 't') {
- return false;
- }
- return true;
- }
- private static String InputFilePath(boolean checkExistence) {
- String fileName = null;
- boolean isCorrect = false;
- do {
- System.out.println("\nInput file path (*.txt):\n");
- try {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); // чтение из файла
- fileName = br.readLine();
- if (fileName.length() < 5) {
- System.err.println("\ninvalid file name\n");
- }
- else if (!HasTxtExtension(fileName)) {
- System.err.println("\ninvalid file extension\n");
- }
- else if (checkExistence && !IsFileExist(fileName)) {
- System.err.println("\nFile not found\n");
- }
- else {
- isCorrect = true;
- }
- }
- catch (IOException e) {
- System.err.println("console read operation failed.");
- }
- } while (!isCorrect);
- return fileName;
- }
- private static int GetMatrixRange(Scanner scan) {
- int num;
- String temp;
- num = 0;
- try {
- temp =scan.next();
- num = Integer.parseInt(temp);
- }
- catch (NumberFormatException e) {
- System.err.print("Symbols have been entered or exceeding permissible limits.");
- return 0;
- }
- if (num < 0) {
- System.err.print("A number less than zero was entered.");
- return 0;
- }
- return num;
- }
- private static int[][] ReadMatrix(Scanner scan, int matrixRange) {
- int[][] matrix = null;
- try {
- matrix = new int[matrixRange][matrixRange];
- for (int rowIndex = 0; rowIndex < matrixRange; rowIndex++) {
- for (int columnIndex = 0; columnIndex < matrixRange; columnIndex++) {
- matrix[rowIndex][columnIndex] = Integer.parseInt(scan.next());
- }
- }
- if(scan.hasNext()) {
- System.err.println("Matrix data exceed the specified matrix range.");
- return null;
- }
- }
- catch (NoSuchElementException e)
- {
- System.err.println("Matrix lack data for the specified matrix range.");
- return null;
- }
- catch (NumberFormatException e) {
- System.err.println("Symbols have been entered or exceeding permissible limits.");
- return null;
- }
- return matrix;
- }
- private static int[][] GetSourceMatrixFromFile() {
- int matrixRange;
- int[][] matrix;
- String filename;
- filename = InputFilePath(true);
- File file = new File(filename);
- try {
- Scanner scan = new Scanner(file);
- matrixRange = GetMatrixRange(scan);
- if (matrixRange == 0) {
- System.err.println("\nMatrix size error\n");
- scan.close();
- return null;
- }
- System.out.println(String.format("\nMatrix dimension: %d x %d", matrixRange, matrixRange));
- matrix = ReadMatrix(scan, matrixRange);
- scan.close();
- return matrix;
- }
- catch (FileNotFoundException e) {
- System.err.println("\nFile not found\n");
- return null;
- }
- }
- private static int[][] GetSourceMatrix() {
- String condition;
- condition = ReadCondition();
- if (Objects.equals(condition, "m")) {
- return InputSourceMatrixManually();
- }
- else {
- return GetSourceMatrixFromFile();
- }
- }
- private static void CopyRow(int[] sourceRow, int[] destRow, int columns) {
- for (int column = 0; column < columns; column++) {
- destRow[column] = sourceRow[column];
- }
- }
- private static void CopyMatrix(int[][] sourceMatrix, int[][] destMatrix, int rows, int columns) {
- for (int row = 0; row < rows; row++) {
- CopyRow(sourceMatrix[row], destMatrix[row], columns);
- }
- }
- private static void ShiftMatrix(int[][] matrix, int rows, int columns) {
- int[] tempRow = new int[columns];
- CopyRow(matrix[rows - 1], tempRow, columns);
- for (int row = rows - 1; row > 0; row--) {
- CopyRow(matrix[row - 1], matrix[row], columns);
- }
- CopyRow(tempRow, matrix[0], columns);
- }
- private static boolean SaveMatrix(String filename, int[][] matrix, int rows, int columns) {
- try (PrintWriter out = new PrintWriter(filename)) {
- for (int row = 0; row < rows; row++) {
- for (int column = 0; column < columns; column++) {
- out.print(matrix[row][column]);
- out.print(" ");
- }
- out.print("\n");
- }
- return true;
- }
- catch (IOException e) {
- System.err.println(String.format("%s could not be opened for writing!", filename));
- return false;
- }
- }
- private static void InputFilePathAndSaveMatrix(int[][] matrix, int rows, int columns) {
- String path;
- boolean isCorrect = false;
- do {
- path = InputFilePath(false);
- isCorrect = SaveMatrix(path, matrix, rows, columns);
- } while (!isCorrect);
- System.out.println("Completed!");
- }
- private static int GetMatrixLength (int[][] matrix) {
- return matrix.length;
- }
- public static void main(String[] args) {
- int[][] matrix;
- int matrixRange;
- int extraRows;
- int defaultValue;
- defaultValue = 1;
- extraRows = 2;
- PrintCondition();
- matrix = GetSourceMatrix();
- if (matrix == null) {
- return;
- }
- matrixRange = GetMatrixLength(matrix);
- PrintMatrix(matrix, matrixRange, matrixRange,"Source Matrix:");
- matrix = ResizeMatrix(matrix, matrixRange, matrixRange, extraRows, defaultValue);
- ShiftMatrix(matrix, matrixRange + extraRows, matrixRange);
- PrintMatrix(matrix, matrixRange + extraRows, matrixRange,"Shifted Matrix:");
- InputFilePathAndSaveMatrix(matrix, matrixRange + extraRows, matrixRange);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement