Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.Scanner;
- public class Main {
- public static Scanner in = new Scanner(System.in);
- private static Scanner fileScanner;
- private static int inputData() {
- int n = 0;
- boolean isIncorrect;
- do {
- isIncorrect = false;
- try {
- n = Integer.parseInt(in.nextLine());
- } catch (Exception e) {
- isIncorrect = true;
- System.out.println("Please, enter a integer number:");
- }
- } while (isIncorrect);
- return n;
- }
- public static int readCountRoots() {
- final int MIN_COUNT = 2;
- final int MAX_COUNT = 10;
- int tmpCountRoots ;
- boolean isIncorrect;
- do {
- isIncorrect = false;
- System.out.println("Enter the number of unknowns of the system of equations (an integer in the range from 2 to 10:)");
- tmpCountRoots = inputData();
- if (tmpCountRoots < MIN_COUNT || tmpCountRoots > MAX_COUNT) {
- System.out.println("You are out of input range!:");
- isIncorrect = true;
- }
- } while (isIncorrect);
- return tmpCountRoots;
- }
- private static boolean choose() {
- int inputNumber;
- boolean isIncorrect;
- final int MIN_NUM = 0;
- final int MAX_NUM = 1;
- do {
- isIncorrect = false;
- inputNumber = inputData();
- if (inputNumber < MIN_NUM || inputNumber > MAX_NUM) {
- System.out.println("You are out of input range!:");
- isIncorrect = true;
- }
- } while (isIncorrect);
- if (inputNumber == 1)
- return true;
- else
- return false;
- }
- public static double inputMatrixCell() {
- return inputData();
- }
- private static double[][] inputTriangularMatrixConsole(int tmpCountRoots) {
- double[][] arrOfMatrixElements = new double[tmpCountRoots][tmpCountRoots];
- for (int i = 0; i < tmpCountRoots; i++)
- arrOfMatrixElements[i] = new double[tmpCountRoots];
- for (int i = 0; i < tmpCountRoots; i++) {
- for (int j = 0; j < tmpCountRoots; j++) {
- if (j - i > -1) {
- System.out.print("a[" + (i + 1) + "," + (j + 1) + "] = ");
- arrOfMatrixElements[i][j] = inputMatrixCell();
- }
- }
- }
- return arrOfMatrixElements;
- }
- private static double[] inputFreeMembersConsole(int tmpCountRoots) {
- double[] arrOfFreeMembers = new double[tmpCountRoots];
- for (int i = 0; i < tmpCountRoots; i++) {
- System.out.print("b[" + (i + 1) + "] = ");
- arrOfFreeMembers[i] = inputMatrixCell();
- }
- return arrOfFreeMembers;
- }
- private static double[][] randomizeOtherElements(int tmpCountRoots, double[][] arrOfMatrixElements) { //рандом элементов ниже главное диагонали
- double max = arrOfMatrixElements[0][0];
- double min = arrOfMatrixElements[0][0];
- for (int i = 0; i < tmpCountRoots; i++) {
- for (int j = 0; j < tmpCountRoots; j++) {
- if (arrOfMatrixElements[i][j] > max)
- max = arrOfMatrixElements[i][j];
- if (arrOfMatrixElements[i][j] < min)
- min = arrOfMatrixElements[i][j];
- }
- }
- double diff = max - min;
- double[][] arrOfMatrixElementsRandom = new double[tmpCountRoots][tmpCountRoots];
- for (int i = 0; i < tmpCountRoots; i++)
- arrOfMatrixElementsRandom[i] = new double[tmpCountRoots];
- for (int i = 0; i < tmpCountRoots; i++) {
- for (int j = 0; j < tmpCountRoots; j++) {
- if (j - i < 0) {
- arrOfMatrixElementsRandom[i][j] = min + Math.random() * diff;
- }
- }
- }
- return arrOfMatrixElementsRandom;
- }
- private static double[][] createMatrix(double[][] arrOfMatrixElements, int tmpCountRoots, double[][] arrOfMatrixElementsRandom, double[] arrOfFreeMembers) {
- double[][] matrix = new double[tmpCountRoots][tmpCountRoots + 1];
- for (int i = 0; i < tmpCountRoots; i++)
- matrix[i] = new double[tmpCountRoots + 1];
- for (int i = 0; i < tmpCountRoots; i++) {
- for (int j = 0; j < tmpCountRoots; j++) {
- if (j - i < 0)
- matrix[i][j] = arrOfMatrixElementsRandom[i][j];
- if (j - i > -1)
- matrix[i][j] = arrOfMatrixElements[i][j];
- }
- }
- for (int i = 0; i < tmpCountRoots; i++) {
- matrix[i][tmpCountRoots] = arrOfFreeMembers[i];
- }
- return matrix;
- }
- public static void writeMatrixInC(double[][] matrix) {
- int lastNum = matrix.length + 1;
- System.out.println("Your virgin matrix:");
- for (int i = 0; i < matrix.length; i++) {
- for (int j = 0; j < lastNum; j++) {
- System.out.print(matrix[i][j] + " ");
- }
- System.out.println();
- }
- }
- //сам алгос прямого хода ниже
- public static void moveStrings(double[][] matrix, int firstRow, int secondRow) {
- int lastNum = matrix.length + 1;
- double r;
- if ((firstRow < lastNum) && (secondRow < lastNum)) {
- for (int j = 0; j < lastNum; j++) {
- r = matrix[secondRow][j];
- matrix[secondRow][j] = matrix[firstRow][j];
- matrix[firstRow][j] = r;
- }
- }
- }
- public static double[][] newMatrix(double[][] matrix) {
- int tmpI, lastNum1, lastNum2;
- double k;
- lastNum1 = matrix.length - 1;
- lastNum2 = matrix.length + 1;
- for (int j = 0; j < matrix.length; j++) {
- if ((matrix[j][j] == 0) && (j < lastNum1)) {
- tmpI = j + 1;
- do {
- if (matrix[tmpI][j] != 0) {
- moveStrings(matrix, j, tmpI);
- }
- } while ((matrix[j][j] == 0));
- }
- for (int i = (j + 1); i < matrix.length; i++) {
- k = matrix[i][j] / matrix[j][j];
- for (int m = j; m < lastNum2; m++) {
- matrix[i][m] = matrix[i][m] - k * matrix[j][m];
- }
- }
- }
- return matrix;
- }
- public static double[] findRoots(double[][] matrix) {
- int tmpCountRoots = matrix.length;
- double[] tmpRoots = new double[tmpCountRoots];
- double sum;
- for (int i = tmpCountRoots - 1; i > -1; i--) {
- sum = 0;
- for (int j = i + 1; j < tmpCountRoots; j++) {
- sum = sum + tmpRoots[j] * matrix[i][j];
- }
- if (matrix[i][i] == 0) {
- tmpRoots[i] = 0;
- } else {
- tmpRoots[i] = (matrix[i][tmpCountRoots] - sum) /
- matrix[i][i];
- }
- }
- return tmpRoots;
- }
- public static double[] gaussM(double[][] matrix) {
- writeMatrixInC(matrix);
- newMatrix(matrix);
- return findRoots(matrix);
- }
- public static void writeRoots(double[] roots) {
- int tmpCountRoots = roots.length;
- System.out.println("Roots are: ");
- for (int i = 0; i < tmpCountRoots; i++) {
- System.out.print(roots[i] + " ");
- }
- System.out.println();
- }
- private static String inputFilePath() {
- String path;
- boolean isIncorrect;
- do {
- isIncorrect = false;
- System.out.println("Input file path:");
- path = in.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;
- }
- private static int inputSizeOfMatrixFromFile(String path) throws FileNotFoundException {
- int size = 0;
- final int MIN_VALUE = -100;
- final int MAX_VALUE = 100;
- boolean isIncorrect;
- fileScanner = new Scanner(new File(path));
- do {
- isIncorrect = false;
- try {
- size = fileScanner.nextInt();
- } catch (Exception q) {
- isIncorrect = true;
- System.out.println("Check data correctness.");
- }
- if (!isIncorrect && (size < MIN_VALUE || size > MAX_VALUE)) {
- isIncorrect = true;
- System.out.println("Invalid input range.");
- }
- } while (isIncorrect);
- return size;
- }
- private static double[][] inputTriangularMatrixFile(int num, Scanner fileScanner) {
- boolean isIncorrect = false;
- double[][] arrOfMatrixElements = new double[num][num];
- for (int i = 0; i < num; i++) {
- arrOfMatrixElements[i] = new double[num];
- }
- for (int i = 0; (i < num) && (!isIncorrect); i++) {
- for (int j = 0; (j < num) && (!isIncorrect); j++) {
- if (j - i > -1) {
- try {
- arrOfMatrixElements[i][j] = Integer.parseInt(fileScanner.next());
- } catch (Exception e) {
- System.out.println("Mistake of reading elements of matrix.");
- }
- }
- }
- }
- return arrOfMatrixElements;
- }
- private static double[] inputFreeMembersFile(int num, Scanner fileScanner) {
- boolean isIncorrect = false;
- double[] arrOfFreeMembers = new double[num];
- for (int i = 0; (i < num) && (!isIncorrect); i++) {
- try {
- arrOfFreeMembers[i] = Integer.parseInt(fileScanner.next());
- } catch (Exception e) {
- System.out.println("Mistake of reading free members.");
- }
- }
- return arrOfFreeMembers;
- }
- private static void fileOutput(double[] arrOfSystemRoots, int num, String path) {
- boolean isIncorrect;
- do {
- isIncorrect = false;
- try {
- FileWriter writer = new FileWriter(path);
- writer.write("Equations roots: " + "\n");
- for (int i = 0; i < num; i++) {
- writer.write(arrOfSystemRoots[i] + "\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.");
- }
- public static void main(String[] args) throws FileNotFoundException {
- boolean choice;
- int num;
- double[][] arrOfMatrixElements;
- double[] arrOfFreeMembers;
- double[][] arrOfMatrixElementsRandom;
- double[][] matrix;
- double[] roots;
- System.out.println("Gauss method. Input elements above main diagonal and free members.");
- System.out.println("Type 0 - console input, type 1 - file input.");
- choice = choose();
- if (!choice) {
- System.out.println("Input number of unknown equations: ");
- num = readCountRoots();
- System.out.println("Input matrix elements: ");
- arrOfMatrixElements = inputTriangularMatrixConsole(num);
- System.out.println("Input Free members: ");
- arrOfFreeMembers = inputFreeMembersConsole(num);
- } else {
- String path = inputFilePath();
- num = inputSizeOfMatrixFromFile(path);
- arrOfMatrixElements = inputTriangularMatrixFile(num, fileScanner);
- arrOfFreeMembers = inputFreeMembersFile(num, fileScanner);
- }
- arrOfMatrixElementsRandom = randomizeOtherElements(num, arrOfMatrixElements);
- matrix = createMatrix(arrOfMatrixElements, num, arrOfMatrixElementsRandom, arrOfFreeMembers);
- roots = gaussM(matrix);
- System.out.println("Type 0 - console output, type 1 - file output.");
- choice = choose();
- if (!choice) {
- writeRoots(roots);
- } else {
- String path = inputFilePath();
- fileOutput(roots, num, path);
- }
- in.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement