Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.io.*;
- public class Laba2_3 {
- static Scanner scan = new Scanner(System.in);
- final static char MIN_SIZE = 2;
- final static char MAX_SIZE = 5;
- final static int MIN_VALUE = -100;
- final static int MAX_VALUE = 100;
- static char chooseIO(String text){
- boolean isIncorrect;
- String choice;
- System.out.println("Выберите способ " + text + ": \nf - file\nc - console");
- do{
- isIncorrect = false;
- choice = scan.nextLine();
- if (choice.length() != 1){
- System.err.println("Введите корректные данные!");
- isIncorrect = true;
- }
- if (!isIncorrect && !(choice.charAt(0) == 'f' || choice.charAt(0) == 'c')){
- System.err.println("Введите корректные данные!");
- isIncorrect = true;
- }
- } while (isIncorrect);
- return choice.charAt(0);
- }
- static String takePath(){
- boolean isIncorrect;
- String path;
- System.out.println("Введите путь к файлу");
- do {
- isIncorrect = false;
- path = scan.nextLine();
- if (!path.endsWith(".txt")) {
- System.err.println("Введите путь с расширением .txt");
- isIncorrect = true;
- }
- } while (isIncorrect);
- return path;
- }
- static String inputPath(){
- String path;
- boolean isIncorrect;
- do{
- isIncorrect = false;
- path = takePath();
- File file = new File(path);
- if (!file.exists()) {
- isIncorrect = true;
- System.err.println("Введите существующий файл!");
- }
- } while (isIncorrect);
- return path;
- }
- static int inputSizeConsole(){
- System.out.println("Введите порядок матрицы");
- boolean isIncorrect;
- int size = 0;
- do{
- isIncorrect = false;
- try {
- size = Integer.parseInt(scan.nextLine());
- }
- catch (NumberFormatException e) {
- System.err.println("Введите натуральное число!");
- isIncorrect = true;
- }
- if (!isIncorrect && (size < MIN_SIZE || size > MAX_SIZE)){
- System.err.println("Введите число от 2 до 5!");
- isIncorrect = true;
- }
- } while (isIncorrect);
- return size;
- }
- static double inputElements(){
- boolean isIncorrect;
- double element = 0;
- do{
- isIncorrect = false;
- try {
- element = Integer.parseInt(scan.nextLine());
- }
- catch (NumberFormatException e) {
- System.err.println("Введите натуральное число!");
- isIncorrect = true;
- }
- if (!isIncorrect && (element < MIN_VALUE || element > MAX_VALUE)){
- isIncorrect = true;
- System.err.println("Введите число в диапазоне от -100 до 100");
- }
- } while (isIncorrect);
- return element;
- }
- static double[][] inputMatrixConsole(int matrixSize){
- double[][] matrix = new double[matrixSize][matrixSize];
- System.out.println("Введите элементы матрицы");
- for(int i = 0; i < matrixSize; i++){
- for(int j = i; j < matrixSize; j++){
- matrix[i][j] = inputElements();
- }
- }
- return matrix;
- }
- static double[] inputVectorConsole(int size) {
- double[] vectorLine = new double[size];
- System.out.println("Введите вектор свободных членов");
- for (int i = 0; i < size; i++) {
- vectorLine[i] = inputElements();
- }
- return vectorLine;
- }
- static int inputSizeFile(String path) {
- boolean isIncorrect;
- int matrixSize = 0;
- do {
- isIncorrect = false;
- try {
- BufferedReader reader = new BufferedReader(new FileReader(path));
- matrixSize = Integer.parseInt(reader.readLine());
- if (matrixSize < MIN_SIZE || matrixSize > MAX_SIZE) {
- System.err.println("Порядок матрицы должен находиться в диапазоне от 2 до 6!");
- isIncorrect = true;
- path = inputPath();
- }
- reader.close();
- } catch (Exception e) {
- System.err.println("Произошла ошибка");
- isIncorrect = true;
- path = inputPath();
- }
- } while (isIncorrect);
- return matrixSize;
- }
- static double[][] inputMatrixFile(int matrixSize, String path){
- double[][] matrix = new double[matrixSize][matrixSize];
- boolean isIncorrect;
- do{
- isIncorrect = false;
- try {
- Scanner filereader = new Scanner(new File(path));
- filereader.nextLine();
- try {
- for (int i = 0; i < matrixSize; i++) {
- for (int j = i; j < matrixSize; j++) {
- matrix[i][j] = filereader.nextInt();
- }
- filereader.nextLine();
- }
- } catch (Exception e){
- isIncorrect = true;
- System.err.println("Ошибка");
- path = inputPath();
- }
- } catch (IOException e) {
- System.err.println("Ошибка");
- isIncorrect = true;
- }
- }while(isIncorrect);
- return matrix;
- }
- static double[] inputVectorFile(int size, String path){
- double[] vectorLine = new double[size];
- boolean isIncorrect;
- do{
- isIncorrect = false;
- try{
- Scanner filereader = new Scanner(new File(path));
- for (int k = 0; k < size + 1; k++){
- filereader.nextLine();
- }
- for (int i = 0; i < size; i++){
- try{
- vectorLine[i] = filereader.nextInt();
- } catch(Exception e){
- isIncorrect = true;
- }
- }
- if(isIncorrect) {
- System.err.println("Ошибка");
- path = inputPath();
- }
- } catch(IOException e){
- System.err.println("Ошибка");
- isIncorrect = true;
- }
- }while(isIncorrect);
- return vectorLine;
- }
- static boolean checkZero(int matrixSize, double[][] matrix){
- for(int i = 0; i < matrixSize; i++){
- for(int j = i; j < matrixSize; j++){
- if (matrix[i][j] == 0) {
- return true;
- }
- }
- }
- return false;
- }
- static double[] findRoots(int size, double[] vectorLine, double[][] matrix){
- double[] roots = new double[size];
- for (int i = size - 1; i > -1; i--) {
- double sum = 0;
- for (int j = i + 1; j < size; j++) {
- sum = (matrix[i][j] * roots[j]) + sum;
- }
- roots[i] = (vectorLine[i] - sum) / matrix[i][i];
- }
- return roots;
- }
- static void outputRootsConsole(int size, double[] roots){
- for(int i = 0; i < size; i++){
- double root = roots[i];
- System.out.println("x" + (i + 1) + " = " + root);
- }
- }
- static void outputRootsFile(int size, double[] roots, String path){
- boolean isIncorrect;
- do{
- isIncorrect = false;
- try {
- FileWriter writer = new FileWriter(path);
- for(int i = 0; i < size; i++){
- writer.write("x" + (i+1) + "=" + roots[i] + "\n");
- }
- writer.close();
- } catch (IOException e) {
- isIncorrect = true;
- System.err.println("Ошибка");
- path = takePath();
- }
- } while(isIncorrect);
- }
- public static void main(String[] args) {
- String path;
- int size;
- double[][] matrix;
- double[] vectorLine;
- System.out.println("Программа выполняет обратный ход Гаусса. Вводить только элементы матрицы системы, стоящие выше главной диагонали и вектор свободных членов.");
- char choice = chooseIO("ввода матрицы и вектора свободных членов");
- if (choice == 'c') {
- size = inputSizeConsole();
- matrix = inputMatrixConsole(size);
- vectorLine = inputVectorConsole(size);
- }
- else {
- path = inputPath();
- size = inputSizeFile(path);
- matrix = inputMatrixFile(size, path);
- vectorLine = inputVectorFile(size, path);
- }
- if(checkZero(size, matrix))
- System.out.println("Нет решений");
- else {
- double[] roots = findRoots(size, vectorLine, matrix);
- choice = chooseIO("вывода корней");
- if (choice == 'c') {
- outputRootsConsole(size, roots);
- } else {
- path = takePath();
- outputRootsFile(size, roots, path);
- }
- }
- scan.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement