Java课程设计 矩阵类 实现加、减、乘法

矩阵类:

// Filename: Matrix.java

class Matrix {

	Matrix(){}

    Matrix(int r, int c)
    {
    	row = r;
    	col = c;
    	matrix = new double[r][c];
    }
    
    Matrix(double [][] m, int r, int c)
    {
    	row = r;
    	col = c;
    	
    	matrix = m;
    }
    
    Matrix(double [][] m) 
    {
    	row = m.length;
    	col = m[0].length;
    	
    	matrix = m;
    }
    
    Matrix add(Matrix b)
    {
    	if((row != b.row) && (col != b.col))
    	{
    		System.out.println("Matrix A can't add Matri B!");
    		System.exit(1);
    	}
    	
    	Matrix tmp = new Matrix(row, col);
    	
    	for(int i = 0; i < row; i++)
    		for(int j = 0; j < col; j++)
    		{
    			tmp.matrix[i][j] = matrix[i][j] + b.matrix[i][j];
    		}
    		
    	return tmp;
    }
    
    Matrix sub(Matrix b)
    {
    	if((row != b.row) && (col != b.col))
    	{
    		System.out.println("Matrix A can't subtract Matri B!");
    		System.exit(1);
    	}
    	
    	Matrix tmp = new Matrix(row, col);
    	
    	for(int i = 0; i < row; i++)
    		for(int j = 0; j < col; j++)
    		{
    			tmp.matrix[i][j] = matrix[i][j] - b.matrix[i][j];
    		}
    		
    	return tmp;
    }
    
    Matrix mul(Matrix b)
    {
    	if(col != b.row)
    	{
    		System.out.println("Matrix A can't multiply Matrix B!");
    		System.exit(1);
    	}
    	
    	Matrix tmp = new Matrix(row, b.col);
    	for(int i = 0; i < row; i++)
    		for(int j = 0; j < b.col; j++)
    		{
    			tmp.matrix[i][j] = 0.0;
    			for(int k = 0; k < b.row; k++)
    			{
    				tmp.matrix[i][j] += matrix[i][k] * b.matrix[k][j];
    			}
    		}
    	return tmp;
    }
    
    void print()
    {
    	for(int i = 0; i < row; i++)
    	{
    		System.out.print(matrix[i][0]);
    		for(int j = 1; j < col; j++)
    		{
    			System.out.print(" " + matrix[i][j]);
    		}
    		System.out.println("");
    	}
    }
    
    private int col;	//列数
    private int row;	//行数
    private double [][] matrix;	//保存矩阵
}

测试程序:

// Filename: MatrixDriveTest

import java.io.*;
import java.util.*;

public class MatrixDriveTest {

    public static void main(String [] args) throws Exception
    {
    	
    	int m, n;	//m保存行数, n保存列数
    	double [][] matrix;	//保存矩阵
    	
    	Scanner in = new Scanner(System.in);
    	
    	System.out.println("Input a m×n matrix...");	
    	
    	m = in.nextInt();
    	n = in.nextInt();
    	
    	matrix = new double[m][n];
    	
    	//System.out.println("m = " + matrix.length);
    	//System.out.println("n = " + matrix[0].length);
    	
    	for(int i = 0; i < m; i++)
    		for(int j = 0; j < n; j++)
    		{
    			matrix[i][j] = in.nextDouble();
    		}
    	
    	Matrix A = new Matrix(matrix);
    	//A.print();
    	
    	System.out.println("Input a m×n matrix...");
    	
    	m = in.nextInt();
    	n = in.nextInt();
    	
    	matrix = new double[m][n];
    	
    	//System.out.println("m = " + matrix.length);
    	//System.out.println("n = " + matrix[0].length);
    	
    	for(int i = 0; i < m; i++)
    		for(int j = 0; j < n; j++)
    		{
    			matrix[i][j] = in.nextDouble();
    		}
    	
    	Matrix B = new Matrix(matrix);
    	
    	//Matrix C = A.add(B);
    	//C.print();
    	//Matrix D = A.sub(B);
    	//D.print();
    	//Matrix E = A.mul(B);
    	//E.print();
    	
    	System.out.println("1. A + b");
		System.out.println("2. A - B");
		System.out.println("3. A * B");
		
		int ch = 0;
		
		System.out.print("Choose your operation: ");
		try{
			BufferedReader br = new BufferedReader( new InputStreamReader(System.in) );
			ch = Integer.parseInt(br.readLine());
		}catch(IOException ex){}
		
		Matrix C = new Matrix();
		
		switch(ch)
			{
				case 1:C = A.add(B);C.print();break;
				case 2:C = A.sub(B);C.print();break;
				case 3:C = A.mul(B);C.print();break;
				default:System.out.println("Choice Error!!");break;
			}
    }
    
    
}


你可能感兴趣的:(Java)