Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.Scanner;
- public class Main {
- private static final int MAX_SIZE_VALUE = 20;
- private static final int MIN_SIZE_VALUE = 1;
- private static final int MAX_ELEMENT_VALUE = 1000;
- private static final int MIN_ELEMENT_VALUE = -1000;
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int[][]Matrix = new int[0][0];
- int matrixSize = 0;
- System.out.println("This program finds matrix determinant.");
- int inputMethodChoice = chooseInputMethod();
- if (inputMethodChoice == 1) {
- matrixSize = setMatrixSize(scanner);
- Matrix = new int[matrixSize][matrixSize];
- fillMatrixManually(Matrix);
- } else if (inputMethodChoice == 2) {
- String filePath = findFile();
- matrixSize = getMatrixSize(scanner, filePath);
- Matrix = new int[matrixSize][matrixSize];
- Matrix = fillMatrixFromFile(filePath);
- }
- int det = calculateDeterminant(Matrix, matrixSize);
- System.out.println("Determinant = " + (det) + ".");
- writeToFile(det);
- }
- private static int[][] fillMatrixManually(int[][] matrix) {
- Scanner scanner = new Scanner(System.in);
- System.out.println("Enter the elements for the matrix:");
- for (int i = 0; i < matrix.length; i++) {
- for (int j = 0; j < matrix.length; j++) {
- boolean isCorrect;
- do {
- System.out.print("Element at position (" + (i + 1) + ", " + (j + 1) + "): ");
- isCorrect = true;
- try {
- matrix[i][j] = scanner.nextInt();
- } catch (Exception e) {
- System.out.println("Invalid input.");
- isCorrect = false;
- scanner.nextLine();
- }
- if (isCorrect && (matrix[i][j] < MIN_ELEMENT_VALUE || matrix[i][j] > MAX_ELEMENT_VALUE)) {
- System.out.println("Please enter a value from " + MIN_ELEMENT_VALUE + " to " + MAX_ELEMENT_VALUE + "!");
- isCorrect = false;
- }
- } while (!isCorrect);
- }
- }
- return matrix;
- }
- private static int[][] fillMatrixFromFile(String path) {
- int[][] Matrix;
- Matrix = new int[0][0];
- System.out.println("Reading matrix...");
- try (Scanner fileScanner = new Scanner(new File(path))) {
- System.out.println("Reading file...");
- String line = fileScanner.nextLine();
- String[] parts = line.split(" ");
- int matrixSize = Integer.parseInt(parts[0]);
- Matrix = new int[matrixSize][matrixSize];
- for (int i = 0; i < matrixSize; i++) {
- // String[] integerInString = fReader.readLine().split(" ");
- line = fileScanner.nextLine();
- parts = line.split(" ");
- for (int j = 0; j < matrixSize; j++) {
- // Matrix[i][j] = Integer.parseInt(integerInString[j]);
- Matrix[i][j] = Integer.parseInt(parts[j]);
- if (Matrix[i][j] < MIN_ELEMENT_VALUE || Matrix[i][j] > MAX_ELEMENT_VALUE) {
- System.out.println("The number entered is out of range! Enter the matrix from the console!");
- return fillMatrixManually(Matrix);
- }
- }
- }
- } catch (IOException e) {
- System.out.println("Error reading file!");
- }
- return Matrix;
- }
- private static int getMatrixSize(Scanner scanner, String path) {
- int matrixSize = 0;
- try (Scanner fileScanner = new Scanner(new File(path))) {
- String line = fileScanner.nextLine();
- String[] parts = line.split(" ");
- matrixSize = Integer.parseInt(parts[0]);
- if (matrixSize < MIN_SIZE_VALUE || matrixSize > MAX_SIZE_VALUE) {
- System.out.println("The number entered is out of range! Enter the matrix size from the console!");
- matrixSize = setMatrixSize(scanner);
- }
- } catch (IOException e) {
- System.out.println("Error reading file!");
- }
- return matrixSize;
- }
- private static String findFile() {
- Scanner scanner = new Scanner(System.in);
- String filePath;
- boolean isCorrect;
- do {
- isCorrect = true;
- System.out.println("Enter path to the file: ");
- filePath = scanner.nextLine();
- File file = new File(filePath);
- if (!file.exists()) {
- isCorrect = false;
- System.out.println("File not found at the specified path.");
- } else if (!filePath.toLowerCase().endsWith(".txt")) {
- isCorrect = false;
- System.out.println("Error, wrong file type! File should have .txt extension.");
- }
- } while (!isCorrect);
- System.out.println("Matrix file found.");
- return filePath;
- }
- private static void writeToFile(int determinant) {
- System.out.print("Enter file name: ");
- Scanner scanner = new Scanner(System.in);
- String fileName = scanner.nextLine() + ".txt";
- try (FileWriter writer = new FileWriter(fileName)) {
- writer.write("Determinant = " + determinant + ".");
- System.out.println("Writing to the file is complete.");
- writer.close();
- } catch (IOException e) {
- System.out.println("Error writing to file!");
- }
- System.out.println();
- }
- private static int chooseInputMethod() {
- int choice;
- boolean isCorrect;
- choice = -1;
- do {
- System.out.println("Choose an option:");
- System.out.println("1. Input matrix manually");
- System.out.println("2. Read matrix from a text file");;
- isCorrect = true;
- try {
- Scanner scanner = new Scanner(System.in);
- choice = scanner.nextInt();
- } catch (NumberFormatException e) {
- System.out.println("Invalid input.");
- isCorrect = false;
- continue;
- }
- if (!isCorrect || !(choice == 1 || choice == 2)) {
- System.out.println("Error! You have to input either 1 or 2.");
- isCorrect = false;
- }
- } while (!isCorrect);
- return choice;
- }
- private static int setMatrixSize(Scanner scanner) {
- boolean isCorrect;
- int rows = 0;
- do {
- System.out.print("Enter the order of the square matrix A: ");
- isCorrect = true;
- try {
- rows = scanner.nextInt();
- } catch (Exception e) {
- System.out.println("Invalid input.");
- isCorrect = false;
- continue;
- }
- if (isCorrect && (rows < MIN_SIZE_VALUE || rows > MAX_SIZE_VALUE)) {
- System.out.println("Please enter a value from " + MIN_SIZE_VALUE + " to " + MAX_SIZE_VALUE + "!");
- isCorrect = false;
- }
- } while (!isCorrect);
- return rows;
- }
- public static int calculateDeterminant(int[][] matrix, int size) {
- if (size == 1) {
- return matrix[0][0];
- } else {
- int result = 0;
- int sign = 1;
- int[][] minor = new int[size - 1][size - 1];
- for (int i = 0; i < size - 1; i++) {
- minor[i] = new int[size - 1];
- }
- for (int k = 0; k < size; k++) {
- int minorRow = 0;
- for (int i = 1; i < size; i++) {
- int minorCol = 0;
- for (int j = 0; j < size; j++) {
- if (j != k) {
- minor[minorRow][minorCol] = matrix[i][j];
- minorCol++;
- }
- }
- minorRow++;
- }
- result += sign * matrix[0][k] * calculateDeterminant(minor, size - 1);
- sign = -sign;
- }
- for (int i = 0; i < size - 1; i++) {
- // Clean up memory by releasing the minor matrix rows
- minor[i] = null;
- }
- // Clean up memory by releasing the minor matrix itself
- minor = null;
- return result;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement