Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.Laba2_3;
- import java.io.BufferedReader;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.Scanner;
- public class Main {
- public static Scanner in = new Scanner(System.in);
- static double[][] inputArrFromFile() {
- boolean isInvalidInput;
- double[][] matrice = new double[0][0];
- do {
- isInvalidInput = false;
- System.out.println("Enter the path to the file, for example 'C:\\Users\\Think\\Desktop\\Input.txt' ");
- String fileName = in.nextLine();
- try {
- BufferedReader input = new BufferedReader(new FileReader(fileName));
- String text = input.readLine();
- int number = Integer.parseInt(text);
- matrice = new double[number + 1][number + 1];
- for (int i = 0; i < number; i++) {
- text = input.readLine();
- String[] arrFilling = text.split(" ");
- for (int j = 0; j < number + 1; j++) {
- matrice[i][j] = Double.parseDouble(arrFilling[j]);
- }
- }
- for (int i = 0; i < number; i++) {
- for (int j = 0; j < number + 1; j++) {
- System.out.printf(" " + matrice[i][j]);
- }
- System.out.println();
- }
- input.close();
- } catch (FileNotFoundException e) {
- System.out.println("A file with the same name was not found, try again");
- isInvalidInput = true;
- } catch (IOException e) {
- System.out.println("This file cannot be opened, try again");
- } catch (NumberFormatException e) {
- System.out.println("Error! The file contains invalid data. Please check the file and ad text.");
- isInvalidInput = true;
- }
- } while (isInvalidInput);
- return matrice;
- }
- static double[][] gauss(double matrice[][], int length) {
- for (int i = 0; i < length + 1; i++) {
- matrice[1][i] = (matrice[0][i] * 1.5) + matrice[1][i];
- matrice[2][i] = (matrice[0][i] + matrice[2][i]);
- matrice[2][i] = (matrice[2][i] - matrice[1][i] * 4);
- matrice[1][i] = matrice[1][i] * 2;
- }
- return matrice;
- }
- static void output(double[][] matrice, int length) {
- System.out.println("The result is");
- for (int i = 0; i < length - 1; i++) {
- for (int j = 0; j < length; j++) {
- System.out.print(" " + matrice[i][j]);
- }
- System.out.println();
- }
- }
- public static void saveToFile(double[][] matrice, int length) {
- System.out.println("Enter the path to the output file, for example 'C:\\Users\\Think\\Desktop\\Output.txt' ");
- String fileName = in.nextLine();
- try (FileWriter writer = new FileWriter(fileName, false)) {
- writer.write("The result is ");
- int i, j;
- for (i = 0; i < length - 1; i++) {
- writer.append("\n");
- for (j = 0; j < length; j++) {
- String str = Double.toString(matrice[i][j]);
- writer.write(" " + str);
- }
- }
- } catch (IOException e) {
- System.out.println("File couldn`t be opened");
- }
- }
- public static void main(String[] args) {
- System.out.println("This program performs a direct move by the Gauss method for a system of linear equations");
- double[][] arrA = inputArrFromFile();
- double[][] arrB = gauss(arrA, arrA.length - 1);
- saveToFile(arrB, arrB.length);
- output(arrB, arrB.length);
- }
- }
Add Comment
Please, Sign In to add comment