Advertisement
Guest User

Untitled

a guest
Apr 25th, 2020
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.22 KB | None | 0 0
  1. // 2020/4/22(水)~26(日)
  2.  
  3. import java.io.*;
  4. import java.util.Scanner;
  5.  
  6. public class Gyoretsu6 {
  7. public static void main(String[] args) {
  8. System.out.println("2つの行列の積を求めます");
  9.  
  10. System.out.println("\n行列Aを入力します");
  11. double[][] matrixA;
  12. if ((args.length == 3 || args.length == 6) && args[0].equals("-i")) {
  13. matrixA = readFile(args[1]);
  14. } else if (args.length == 6 && args[3].equals("-i")) {
  15. matrixA = readFile(args[4]);
  16. } else {
  17. matrixA = inputMatrix();
  18. }
  19. System.out.println("行列Aを出力します");
  20. printMatrix(matrixA);
  21.  
  22. System.out.println("\n行列Bを入力します");
  23. double[][] matrixB;
  24. if ((args.length == 3 || args.length == 6) && args[0].equals("-i")) {
  25. matrixB = readFile(args[2]);
  26. } else if (args.length == 6 && args[3].equals("-i")) {
  27. matrixB = readFile(args[5]);
  28. } else {
  29. matrixB = inputMatrix();
  30. }
  31. System.out.println("行列Bを出力します");
  32. printMatrix(matrixB);
  33.  
  34. System.out.println("\n積ABを計算します");
  35. double[][] matrixAB = multiplyMatrix(matrixA, matrixB);
  36. System.out.println("積ABを出力します");
  37. printMatrix(matrixAB);
  38. if ((args.length == 3 || args.length == 6) && args[0].equals("-o")) {
  39. writeFile(args[1], matrixAB);
  40. } else if (args.length == 6 && args[3].equals("-o")) {
  41. writeFile(args[4], matrixAB);
  42. } else {
  43. return;
  44. }
  45.  
  46. System.out.println("\n積BAを計算します");
  47. double[][] matrixBA = multiplyMatrix(matrixB, matrixA);
  48. System.out.println("積BAを出力します");
  49. printMatrix(matrixBA);
  50. if ((args.length == 3 || args.length == 6) && args[0].equals("-o")) {
  51. writeFile(args[2], matrixBA);
  52. } else if (args.length == 6 && args[3].equals("-o")) {
  53. writeFile(args[5], matrixBA);
  54. } else {
  55. return;
  56. }
  57. }
  58.  
  59. public static double[][] inputMatrix() {
  60. Scanner scanner = new Scanner(System.in);
  61. System.out.println("行列の行の大きさを自然数で入力して下さい");
  62. int row = scanner.nextInt();
  63. System.out.println("行列の列の大きさを自然数で入力して下さい");
  64. int column = scanner.nextInt();
  65. double[][] matrix = new double[row][column];
  66. for (int i = 0; i < row; i++) {
  67. for (int j = 0; j < column; j++) {
  68. System.out.println("行列の" + (i + 1) + "行" + (j + 1) + "列の要素の値(実数)を入力して下さい");
  69. matrix[i][j] = scanner.nextDouble();
  70. }
  71. }
  72. return matrix;
  73. }
  74.  
  75. public static void printMatrix(double[][] matrix) {
  76. if (matrix == null) {
  77. System.out.println("表示はありません");
  78. return;
  79. }
  80. for (int i = 0; i < matrix.length; i++) {
  81. for (int j = 0; j < matrix[0].length; j++) {
  82. System.out.print(matrix[i][j] + " ");
  83. }
  84. System.out.println();
  85. }
  86. }
  87.  
  88. public static double[][] multiplyMatrix(double[][] matrix1, double[][] matrix2) {
  89. if (matrix1[0].length != matrix2.length) {
  90. System.out.println("この2つの行列はこの順では積が求められません");
  91. return null;
  92. }
  93. double[][] product = new double[matrix1.length][matrix2[0].length];
  94. for (int i = 0; i < matrix1.length; i++) {
  95. for (int j = 0; j < matrix2[0].length; j++) {
  96. for (int k = 0; k < matrix1[0].length /* matrix2.lengthでも同じ */ ; k++) {
  97. product[i][j] += matrix1[i][k] * matrix2[k][j];
  98. }
  99. }
  100. }
  101. return product;
  102. }
  103.  
  104. public static double[][] readFile(String fileName) {
  105. String line = null;
  106. int lineCount = 0;
  107. String[] lineArray = null;
  108. try(FileReader fr = new FileReader(fileName); /* FileNotFoundException */
  109. BufferedReader br = new BufferedReader(fr);) {
  110. while((line = br.readLine()) != null) { // IOException
  111. if (lineCount == 0) {
  112. lineArray = line.split("[ ]");
  113. }
  114. lineCount++;
  115. }
  116. } catch(FileNotFoundException e1) {
  117. e1.printStackTrace();
  118. } catch(IOException e2) {
  119. e2.printStackTrace();
  120. }
  121. double[][] matrix = new double[lineArray.length][lineCount];
  122.  
  123. line = null;
  124. int lineNum = 0;
  125. String[][] lineTable = new String[lineArray.length][lineCount];
  126. try(FileReader fr = new FileReader(fileName);
  127. BufferedReader br = new BufferedReader(fr);) {
  128. while((line = br.readLine()) != null) {
  129. lineTable[lineNum] = line.split("[ ]");
  130. lineNum++;
  131. }
  132. } catch(FileNotFoundException e1) {
  133. e1.printStackTrace();
  134. } catch(IOException e2) {
  135. e2.printStackTrace();
  136. }
  137. for (int i = 0; i < lineArray.length; i++) {
  138. for (int j = 0; j < lineCount; j++) {
  139. matrix[i][j] = Double.parseDouble(lineTable[i][j]);
  140. }
  141. }
  142. System.out.println("ファイル" + fileName + "から読み込みました");
  143. return matrix;
  144. }
  145.  
  146. public static void writeFile(String fileName, double[][] matrix) {
  147. if (matrix == null) {
  148. System.out.println("書き込みはありません");
  149. return;
  150. }
  151. try(FileWriter fw = new FileWriter(fileName);) { // ファイル上書き
  152. for (int i = 0; i < matrix.length; i++) {
  153. for (int j = 0; j < matrix[0].length; j++) {
  154. fw.write(matrix[i][j] + " ");
  155. }
  156. fw.write("\n");
  157. }
  158. fw.flush();
  159. } catch(IOException e) {
  160. e.printStackTrace();
  161. }
  162. System.out.println("ファイル" + fileName + "に書き込みました");
  163. }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement