Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.io.*;
- import java.util.Scanner;
- public class Main {
- static Scanner scan = new Scanner(System.in);
- static byte chooseConsole = 1;
- static byte chooseFile = 2;
- static byte maxSize = 6;
- static byte minSize = 1;
- static int maxElement = 256;
- static int takeIntValue(int maxValue, int minValue){
- int value = 0;
- boolean isCorrect;
- do {
- isCorrect = true;
- try{
- value = Integer.parseInt(scan.nextLine());
- }
- catch(Exception e){
- isCorrect = false;
- System.out.print("Произошла ошибка! Введите значение ещё раз: ");
- }
- if(isCorrect && ((value > maxValue) || (value < minValue))){
- isCorrect = false;
- System.out.print("Значение выходит за пределы! Введите число ещё раз: ");
- }
- }while(!isCorrect);
- return value;
- }
- static int[][] takeMatrix(int size){
- int[][] matrix = new int[size][size];
- for(byte i = 0;i < size;i++){
- for(byte j = 0;j < size;j++){
- System.out.print("Введите элемент " + (i + 1) + "го стольбца " + (j + 1) + "й строки: ");
- matrix[i][j] = takeIntValue(maxElement,maxElement * -1);
- }
- }
- return matrix;
- }
- static int[][] takeAddition(int[][] matrix, int i, int j){
- int[][] addition = new int[matrix.length - 1][matrix.length - 1];
- int counterI = 0;
- int counterJ = 0;
- for(int k = 0;k < matrix.length; k++){
- for(int l = 0; l < matrix.length; l++){
- if(k != i && l != j){
- addition[counterI][counterJ] = matrix[k][l];
- counterJ++;
- }
- }
- if(k != i)
- counterI++;
- counterJ = 0;
- }
- return addition;
- }
- static int calcDeterminator(int[][] matrix){
- int determinator = 0;
- if(matrix.length == 1)
- determinator = matrix[0][0];
- else if(matrix.length == 2)
- determinator = matrix[0][0] * matrix[1][1] - matrix[1][0] * matrix[0][1];
- else{
- for(int j = 0; j < matrix.length; j++){
- if(j % 2 == 0)
- determinator += matrix[0][j] * calcDeterminator(takeAddition(matrix, 0, j));
- else
- determinator += -1 * matrix[0][j] * calcDeterminator(takeAddition(matrix, 0, j));
- }
- }
- return determinator;
- }
- static void showMatrix(int[][] matrix){
- System.out.println("Исходная матрица:");
- for(int i=0;i < matrix.length;i++){
- for (int j=0;j < matrix.length;j++){
- System.out.print(matrix[i][j] + " ");
- }
- System.out.println();
- }
- }
- static String takePath(){
- String path = "";
- boolean isCorrect;
- do{
- isCorrect = true;
- path = scan.nextLine();
- try{
- FileReader reader = new FileReader(path);
- reader.close();
- }
- catch(IOException e){
- System.out.print("Произошла ошибка при попытке найти данный файл! Введите путь заново: ");
- isCorrect = false;
- }
- }while(!isCorrect);
- return path;
- }
- static boolean checkFile(String path) throws IOException{
- Scanner scanFile = new Scanner(new File(path));
- boolean isCorrect = true;
- int sizeTemp = 0;
- int temp;
- int counter = 0;
- try{
- sizeTemp = scanFile.nextInt();
- }
- catch(Exception e){
- System.out.println("Произошла ошибка при попытке прочитать размер матрицы! Введите данные вручную.");
- isCorrect = false;
- }
- while(isCorrect && scanFile.hasNext()){
- try{
- temp = scanFile.nextInt();
- }
- catch(Exception e){
- System.out.println("Произошла ошибка при попытке прочитать матрицу! Введите данные вручную.");
- isCorrect = false;
- }
- counter++;
- }
- if(isCorrect && counter != sizeTemp * sizeTemp) {
- System.out.println("Размер не соответствует количеству элементов матрицы! Введите данные вручную.");
- isCorrect = false;
- }
- scanFile.close();
- return isCorrect;
- }
- static int[][] takeMatrixFromFile(String path) throws IOException{
- Scanner scanFile = new Scanner(new File(path));
- int size = scanFile.nextInt();
- int[][] matrix = new int[size][size];
- for(int i = 0;i < size;i++)
- for(int j = 0;j < size;j++)
- matrix[i][j] = scanFile.nextInt();
- scanFile.close();
- return matrix;
- }
- static boolean checkOutput(String path){
- boolean isCorrect = true;
- try{
- FileWriter writer = new FileWriter(path, false);
- writer.write("");
- writer.close();
- }
- catch(IOException e){
- System.out.println("Произошла ошибка при попытке записать данные в файл!");
- isCorrect = false;
- }
- return isCorrect;
- }
- static void outputToFile(String path, int[][] matrix, int answer) throws IOException{
- FileWriter writer = new FileWriter(path, true);
- writer.write("Исходная матрица:\n");
- for(int i = 0; i < matrix.length;i++) {
- for (int j = 0; j < matrix.length; j++)
- writer.write(matrix[i][j] + " ");
- writer.write("\n");
- }
- writer.write("Определитель данной матрицы равен " + answer);
- writer.close();
- }
- public static void main(String[] args) throws IOException{
- System.out.println("Данная программа считает определитель матрицы.\n1.Ввести вручную.\n2.Ввести из файла.");
- int choice = takeIntValue(chooseFile, chooseConsole);
- int[][] matrix;
- if(choice == chooseConsole){
- System.out.print("Введите размер матрицы: ");
- int size = takeIntValue(maxSize, minSize);
- System.out.println("Заполните матрицу.");
- matrix = takeMatrix(size);
- }
- else{
- System.out.println("Введите путь к файлу:");
- String path = takePath();
- if(checkFile(path)) {
- matrix = takeMatrixFromFile(path);
- }
- else{
- System.out.print("Введите размер матрицы: ");
- int size = takeIntValue(maxSize, minSize);
- System.out.println("Заполните матрицу.");
- matrix = takeMatrix(size);
- }
- }
- int determinator = calcDeterminator(matrix);
- System.out.println("1.Вывести в консоль.\n2.Вывести в файл.");
- choice = takeIntValue(chooseFile, chooseConsole);
- if(choice == chooseConsole){
- showMatrix(matrix);
- System.out.println("Определитель матрицы равен: " + determinator);
- }
- else{
- System.out.println("Введите путь к файлу:");
- String path = takePath();
- if (checkOutput(path)) {
- outputToFile(path, matrix, determinator);
- System.out.print("Результаты успешно сохранены!");
- }
- else{
- showMatrix(matrix);
- System.out.println("Определитель матрицы равен: " + determinator);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment