当前位置:网站首页>Matrix processing practice

Matrix processing practice

2022-07-05 10:00:00 Sunqk5665

Using two-dimensional arrays (int[]) Implement a matrix class :Matrix.
The following methods are required :
(1)set (int row, int col, int value) Will be the first row Xing di col The element of the column is assigned to value;
(2)get(int row,int col) : Take the first place row Xing di col The elements of the column ;
(3)width( ): Returns the number of columns in the matrix ;
(4)height( ): Returns the number of rows in the matrix ;
(5)Matrix add ( Matrix b) : Returns the current matrix and the matrix b The added matrix ;
(6)Matrix multiply ( Matrix b) : Returns the current matrix and the matrix b Matrix after multiplication .
(7)Matrix transpose(): Returns the transpose matrix of the current matrix ;
(8)print(): Print out the current matrix in rows and columns .

package PTAworks;
/** * @ClassName: aaa * @Description: TODO * @Author: Sunqker * @Date: 2021/10/13 15:37 */
import java.util.Scanner;

public class aaa {
    
    public static void main(String[] args) {
    

        Scanner cin = new Scanner(System.in);
        // Get the number of rows and columns of the matrix 
        Matrix matrix = new Matrix(cin.nextInt(), cin.nextInt());
        Matrix t;   //  Temporary storage matrix 
        // Matrix data 
        matrix.input(cin); //  Enter matrix information 
        System.out.println("row:" + matrix.height() + " " + "column:" + matrix.width());
        // Sets the row of the matrix value 、 Columns and values 
        matrix.set(cin.nextInt(), cin.nextInt(), cin.nextInt());
        System.out.println("after set value:");
        matrix.print();
        // Get the row of the matrix value 、 Column 
        int r = cin.nextInt();
        int c = cin.nextInt();
        System.out.println("value on (" + r + "," + c + "):" + matrix.get(r,c));

        // Get the number of rows and columns of the matrix to be added 
        Matrix tmp = new Matrix(cin.nextInt(),cin.nextInt());
        // Get the value of the matrix to be added 
        tmp.input(cin);
        System.out.println("after add:");
        t = matrix.add(tmp); // Matrix addition 
        t.print();           // Print the added matrix 

        // The number of rows and columns of the matrix to be multiplied 
        tmp = new Matrix(cin.nextInt(),cin.nextInt());
         Get the value of the matrix to be added 
        tmp.input(cin);
        System.out.println("after multiply:");
        t = matrix.multiply(tmp);// matrix multiplication 
        // The value of the matrix to be multiplied 
        t.print();               // Print the multiplication result 

        System.out.println("after transpose:");
        t = matrix.transpose();
        t.print();

    }
}
class Matrix{
    
    private int row, col;
    private int[][] d;

    public Matrix(int row, int col) {
    
        this.row = row;
        this.col = col;
        d = new int[row][col];
    }


    public void input(Scanner cin) {
    
        for(int i = 1; i <= this.row; i++) {
    
            for(int j = 1; j <= this.col; j++) {
    
                int val = cin.nextInt();
                this.set(i,j,val);
            }
        }
    }
    // 1  Will be the first row Xing di col The element of the column is assigned to value
    public void set(int row, int col, int value){
    
        d[--row][--col] = value;
    }
    // 2  Take the first place row Xing di col The elements of the column 
    public int get( int row, int col ){
    
        return d[--row][--col];
    }
    // 3  Returns the number of columns in the matrix 
    public int width(){
    
        return col;
    }
    // 4  Returns the number of rows in the matrix 
    public int height(){
    
        return row;
    }
    // 5  Returns the current matrix and the matrix b The added matrix 
    public Matrix add( Matrix b){
    
        Matrix t = new Matrix(this.row, this.col);

        for(int i = 1; i <= this.row; ++ i){
    
            for(int j = 1; j <= this.col; ++ j ){
    
                t.set(i, j, this.get(i, j) + b.get(i, j));
            }
        }
        return t;
    }
    // 6  Returns the current matrix and the matrix b Matrix after multiplication 
    public Matrix multiply( Matrix b){
    
        Matrix t = new Matrix(this.row, b.width());

        for(int i = 1; i <= this.row; ++ i){
    
            for(int j = 1; j <= b.width(); ++ j ){
    
                int tt = 0;
                for(int k = 1; k <= this.col; k ++ ){
    
                    tt += this.get(i, k) * b.get(k, j);
                }
                t.set(i, j, tt);
            }
        }
        return t;
    }

    // 7  Returns the transpose matrix of the current matrix 
    public Matrix transpose(){
    
        Matrix t = new Matrix(this.row, this.col);

        for( int i = 1; i <= this.row; i ++ ){
    
            for ( int j = 1; j <= this.col; j ++ ){
    
                t.set(j, i , this.get(i, j));
            }
        }
        return t;
    }
    // 8  Row and column print matrix 
    public void print(){
    
        //DecimalFormat d = new DecimalFormat("#"); //  Control output format , No decimal 
        for(int i = 1; i <= this.row; ++ i){
    
            for( int j = 1; j <= this.col - 1; ++ j ){
    
                System.out.print(this.get(i, j) + " ");

            }
            System.out.println(this.get(i, col));
        }
    }
}
原网站

版权声明
本文为[Sunqk5665]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140535206142.html