Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 2020/4/22(水)~26(日)
- import java.io.*;
- import java.util.Scanner;
- public class Gyoretsu6 {
- public static void main(String[] args) {
- System.out.println("2つの行列の積を求めます");
- System.out.println("\n行列Aを入力します");
- double[][] matrixA;
- if ((args.length == 3 || args.length == 6) && args[0].equals("-i")) {
- matrixA = readFile(args[1]);
- } else if (args.length == 6 && args[3].equals("-i")) {
- matrixA = readFile(args[4]);
- } else {
- matrixA = inputMatrix();
- }
- System.out.println("行列Aを出力します");
- printMatrix(matrixA);
- System.out.println("\n行列Bを入力します");
- double[][] matrixB;
- if ((args.length == 3 || args.length == 6) && args[0].equals("-i")) {
- matrixB = readFile(args[2]);
- } else if (args.length == 6 && args[3].equals("-i")) {
- matrixB = readFile(args[5]);
- } else {
- matrixB = inputMatrix();
- }
- System.out.println("行列Bを出力します");
- printMatrix(matrixB);
- System.out.println("\n積ABを計算します");
- double[][] matrixAB = multiplyMatrix(matrixA, matrixB);
- System.out.println("積ABを出力します");
- printMatrix(matrixAB);
- if ((args.length == 3 || args.length == 6) && args[0].equals("-o")) {
- writeFile(args[1], matrixAB);
- } else if (args.length == 6 && args[3].equals("-o")) {
- writeFile(args[4], matrixAB);
- } else {
- return;
- }
- System.out.println("\n積BAを計算します");
- double[][] matrixBA = multiplyMatrix(matrixB, matrixA);
- System.out.println("積BAを出力します");
- printMatrix(matrixBA);
- if ((args.length == 3 || args.length == 6) && args[0].equals("-o")) {
- writeFile(args[2], matrixBA);
- } else if (args.length == 6 && args[3].equals("-o")) {
- writeFile(args[5], matrixBA);
- } else {
- return;
- }
- }
- public static double[][] inputMatrix() {
- Scanner scanner = new Scanner(System.in);
- System.out.println("行列の行の大きさを自然数で入力して下さい");
- int row = scanner.nextInt();
- System.out.println("行列の列の大きさを自然数で入力して下さい");
- int column = scanner.nextInt();
- double[][] matrix = new double[row][column];
- for (int i = 0; i < row; i++) {
- for (int j = 0; j < column; j++) {
- System.out.println("行列の" + (i + 1) + "行" + (j + 1) + "列の要素の値(実数)を入力して下さい");
- matrix[i][j] = scanner.nextDouble();
- }
- }
- return matrix;
- }
- public static void printMatrix(double[][] matrix) {
- if (matrix == null) {
- System.out.println("表示はありません");
- return;
- }
- for (int i = 0; i < matrix.length; i++) {
- for (int j = 0; j < matrix[0].length; j++) {
- System.out.print(matrix[i][j] + " ");
- }
- System.out.println();
- }
- }
- public static double[][] multiplyMatrix(double[][] matrix1, double[][] matrix2) {
- if (matrix1[0].length != matrix2.length) {
- System.out.println("この2つの行列はこの順では積が求められません");
- return null;
- }
- double[][] product = new double[matrix1.length][matrix2[0].length];
- for (int i = 0; i < matrix1.length; i++) {
- for (int j = 0; j < matrix2[0].length; j++) {
- for (int k = 0; k < matrix1[0].length /* matrix2.lengthでも同じ */ ; k++) {
- product[i][j] += matrix1[i][k] * matrix2[k][j];
- }
- }
- }
- return product;
- }
- public static double[][] readFile(String fileName) {
- String line = null;
- int lineCount = 0;
- String[] lineArray = null;
- try(FileReader fr = new FileReader(fileName); /* FileNotFoundException */
- BufferedReader br = new BufferedReader(fr);) {
- while((line = br.readLine()) != null) { // IOException
- if (lineCount == 0) {
- lineArray = line.split("[ ]");
- }
- lineCount++;
- }
- } catch(FileNotFoundException e1) {
- e1.printStackTrace();
- } catch(IOException e2) {
- e2.printStackTrace();
- }
- double[][] matrix = new double[lineArray.length][lineCount];
- line = null;
- int lineNum = 0;
- String[][] lineTable = new String[lineArray.length][lineCount];
- try(FileReader fr = new FileReader(fileName);
- BufferedReader br = new BufferedReader(fr);) {
- while((line = br.readLine()) != null) {
- lineTable[lineNum] = line.split("[ ]");
- lineNum++;
- }
- } catch(FileNotFoundException e1) {
- e1.printStackTrace();
- } catch(IOException e2) {
- e2.printStackTrace();
- }
- for (int i = 0; i < lineArray.length; i++) {
- for (int j = 0; j < lineCount; j++) {
- matrix[i][j] = Double.parseDouble(lineTable[i][j]);
- }
- }
- System.out.println("ファイル" + fileName + "から読み込みました");
- return matrix;
- }
- public static void writeFile(String fileName, double[][] matrix) {
- if (matrix == null) {
- System.out.println("書き込みはありません");
- return;
- }
- try(FileWriter fw = new FileWriter(fileName);) { // ファイル上書き
- for (int i = 0; i < matrix.length; i++) {
- for (int j = 0; j < matrix[0].length; j++) {
- fw.write(matrix[i][j] + " ");
- }
- fw.write("\n");
- }
- fw.flush();
- } catch(IOException e) {
- e.printStackTrace();
- }
- System.out.println("ファイル" + fileName + "に書き込みました");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement