Cinder1986

Untitled

Mar 13th, 2022
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.32 KB | None | 0 0
  1. package com.company;
  2. import java.io.*;
  3. import java.util.Scanner;
  4. public class Main {
  5. static Scanner scan = new Scanner(System.in);
  6.  
  7. static byte chooseConsole = 1;
  8. static byte chooseFile = 2;
  9. static byte maxSize = 6;
  10. static byte minSize = 1;
  11. static int maxElement = 256;
  12.  
  13. static int takeIntValue(int maxValue, int minValue){
  14. int value = 0;
  15. boolean isCorrect;
  16. do {
  17. isCorrect = true;
  18. try{
  19. value = Integer.parseInt(scan.nextLine());
  20. }
  21. catch(Exception e){
  22. isCorrect = false;
  23. System.out.print("Произошла ошибка! Введите значение ещё раз: ");
  24. }
  25. if(isCorrect && ((value > maxValue) || (value < minValue))){
  26. isCorrect = false;
  27. System.out.print("Значение выходит за пределы! Введите число ещё раз: ");
  28. }
  29. }while(!isCorrect);
  30. return value;
  31. }
  32. static int[][] takeMatrix(int size){
  33. int[][] matrix = new int[size][size];
  34. for(byte i = 0;i < size;i++){
  35. for(byte j = 0;j < size;j++){
  36. System.out.print("Введите элемент " + (i + 1) + "го стольбца " + (j + 1) + "й строки: ");
  37. matrix[i][j] = takeIntValue(maxElement,maxElement * -1);
  38. }
  39. }
  40. return matrix;
  41. }
  42. static int[][] takeAddition(int[][] matrix, int i, int j){
  43. int[][] addition = new int[matrix.length - 1][matrix.length - 1];
  44. int counterI = 0;
  45. int counterJ = 0;
  46. for(int k = 0;k < matrix.length; k++){
  47. for(int l = 0; l < matrix.length; l++){
  48. if(k != i && l != j){
  49. addition[counterI][counterJ] = matrix[k][l];
  50. counterJ++;
  51. }
  52. }
  53. if(k != i)
  54. counterI++;
  55. counterJ = 0;
  56. }
  57. return addition;
  58. }
  59. static int calcDeterminator(int[][] matrix){
  60. int determinator = 0;
  61. if(matrix.length == 1)
  62. determinator = matrix[0][0];
  63. else if(matrix.length == 2)
  64. determinator = matrix[0][0] * matrix[1][1] - matrix[1][0] * matrix[0][1];
  65. else{
  66. for(int j = 0; j < matrix.length; j++){
  67. if(j % 2 == 0)
  68. determinator += matrix[0][j] * calcDeterminator(takeAddition(matrix, 0, j));
  69. else
  70. determinator += -1 * matrix[0][j] * calcDeterminator(takeAddition(matrix, 0, j));
  71. }
  72. }
  73. return determinator;
  74. }
  75. static void showMatrix(int[][] matrix){
  76. System.out.println("Исходная матрица:");
  77. for(int i=0;i < matrix.length;i++){
  78. for (int j=0;j < matrix.length;j++){
  79. System.out.print(matrix[i][j] + " ");
  80. }
  81. System.out.println();
  82. }
  83. }
  84. static String takePath(){
  85. String path = "";
  86. boolean isCorrect;
  87. do{
  88. isCorrect = true;
  89. path = scan.nextLine();
  90. try{
  91. FileReader reader = new FileReader(path);
  92. reader.close();
  93. }
  94. catch(IOException e){
  95. System.out.print("Произошла ошибка при попытке найти данный файл! Введите путь заново: ");
  96. isCorrect = false;
  97. }
  98. }while(!isCorrect);
  99. return path;
  100. }
  101. static boolean checkFile(String path) throws IOException{
  102. Scanner scanFile = new Scanner(new File(path));
  103. boolean isCorrect = true;
  104. int sizeTemp = 0;
  105. int temp;
  106. int counter = 0;
  107. try{
  108. sizeTemp = scanFile.nextInt();
  109. }
  110. catch(Exception e){
  111. System.out.println("Произошла ошибка при попытке прочитать размер матрицы! Введите данные вручную.");
  112. isCorrect = false;
  113. }
  114. while(isCorrect && scanFile.hasNext()){
  115. try{
  116. temp = scanFile.nextInt();
  117. }
  118. catch(Exception e){
  119. System.out.println("Произошла ошибка при попытке прочитать матрицу! Введите данные вручную.");
  120. isCorrect = false;
  121. }
  122. counter++;
  123. }
  124. if(isCorrect && counter != sizeTemp * sizeTemp) {
  125. System.out.println("Размер не соответствует количеству элементов матрицы! Введите данные вручную.");
  126. isCorrect = false;
  127. }
  128. scanFile.close();
  129. return isCorrect;
  130. }
  131. static int[][] takeMatrixFromFile(String path) throws IOException{
  132. Scanner scanFile = new Scanner(new File(path));
  133. int size = scanFile.nextInt();
  134. int[][] matrix = new int[size][size];
  135. for(int i = 0;i < size;i++)
  136. for(int j = 0;j < size;j++)
  137. matrix[i][j] = scanFile.nextInt();
  138. scanFile.close();
  139. return matrix;
  140. }
  141. static boolean checkOutput(String path){
  142. boolean isCorrect = true;
  143. try{
  144. FileWriter writer = new FileWriter(path, false);
  145. writer.write("");
  146. writer.close();
  147. }
  148. catch(IOException e){
  149. System.out.println("Произошла ошибка при попытке записать данные в файл!");
  150. isCorrect = false;
  151. }
  152. return isCorrect;
  153. }
  154. static void outputToFile(String path, int[][] matrix, int answer) throws IOException{
  155. FileWriter writer = new FileWriter(path, true);
  156. writer.write("Исходная матрица:\n");
  157. for(int i = 0; i < matrix.length;i++) {
  158. for (int j = 0; j < matrix.length; j++)
  159. writer.write(matrix[i][j] + " ");
  160. writer.write("\n");
  161. }
  162. writer.write("Определитель данной матрицы равен " + answer);
  163. writer.close();
  164. }
  165. public static void main(String[] args) throws IOException{
  166. System.out.println("Данная программа считает определитель матрицы.\n1.Ввести вручную.\n2.Ввести из файла.");
  167. int choice = takeIntValue(chooseFile, chooseConsole);
  168. int[][] matrix;
  169. if(choice == chooseConsole){
  170. System.out.print("Введите размер матрицы: ");
  171. int size = takeIntValue(maxSize, minSize);
  172. System.out.println("Заполните матрицу.");
  173. matrix = takeMatrix(size);
  174. }
  175. else{
  176. System.out.println("Введите путь к файлу:");
  177. String path = takePath();
  178. if(checkFile(path)) {
  179. matrix = takeMatrixFromFile(path);
  180. }
  181. else{
  182. System.out.print("Введите размер матрицы: ");
  183. int size = takeIntValue(maxSize, minSize);
  184. System.out.println("Заполните матрицу.");
  185. matrix = takeMatrix(size);
  186. }
  187. }
  188. int determinator = calcDeterminator(matrix);
  189. System.out.println("1.Вывести в консоль.\n2.Вывести в файл.");
  190. choice = takeIntValue(chooseFile, chooseConsole);
  191. if(choice == chooseConsole){
  192. showMatrix(matrix);
  193. System.out.println("Определитель матрицы равен: " + determinator);
  194. }
  195. else{
  196. System.out.println("Введите путь к файлу:");
  197. String path = takePath();
  198. if (checkOutput(path)) {
  199. outputToFile(path, matrix, determinator);
  200. System.out.print("Результаты успешно сохранены!");
  201. }
  202. else{
  203. showMatrix(matrix);
  204. System.out.println("Определитель матрицы равен: " + determinator);
  205. }
  206. }
  207. }
  208. }
Advertisement
Add Comment
Please, Sign In to add comment