7-4 矩阵类 (30 分)

7-4 矩阵类 (30 分)

利用二维数组(double[])实现一个矩阵类:Matrix。要求提供以下方法:
(1)set(int row, int col, double value):将第row行第col列的元素赋值为value;
(2)get(int row,int col):取第row行第col列的元素;
(3)width():返回矩阵的列数;
(4)height():返回矩阵的行数;
(5)Matrix add(Matrix b):返回当前矩阵与矩阵b相加后的矩阵;
(6)Matrix multiply(Matrix b):返回当前矩阵与矩阵b相乘后的矩阵。
(7)Matrix transpose():返回当前矩阵的转置矩阵;
(8)toString():以行和列的形式打印出当前矩阵。

输入格式:

矩阵的行列数 矩阵的数据 设置矩阵值的行、列和值 获取矩阵值的行、列 待相加矩阵的行列数 待相加矩阵的值 待相乘矩阵的行列数 待相乘矩阵的值

输出格式:

矩阵的行、列数 设置矩阵值后的矩阵 某行某列的矩阵值 矩阵相加结果 矩阵相乘结果 矩阵转置结果

输入样例:

在这里给出一组输入。例如:

3 3
1 2 3
4 5 6
7 8 9
2 3 8
1 3
3 3
1 2 3
4 5 6
7 8 9
3 2
1 2
1 2
1 2

输出样例:

在这里给出相应的输出。例如:

row:3 column:3
after set value:
1 2 3
4 5 8
7 8 9
value on (1,3):3
after add:
2 4 6
8 10 14
14 16 18
after multiply:
6 12
17 34
24 48
after transpose:
1 4 7
2 5 8
3 8 9

参考答案

import java.text.DecimalFormat;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        DecimalFormat d = new DecimalFormat("#"); // 控制输出格式,无小数
        Scanner cin = new Scanner(System.in);
        Matrix matrix = new Matrix(cin.nextInt(), cin.nextInt());
        Matrix t; // 暂存矩阵
        matrix.input(cin); // 输入信息
        System.out.println("row:" + matrix.height() + " " + "column:" + matrix.width());
        matrix.set(cin.nextInt(), cin.nextInt(), cin.nextDouble());
        System.out.println("after set value:");
        System.out.print(matrix.toString());
        int r = cin.nextInt();
        int c = cin.nextInt();
        System.out.println("value on (" + r + "," + c + "):" + d.format(matrix.get(r,c)));
        Matrix tmp = new Matrix(cin.nextInt(),cin.nextInt());
        tmp.input(cin);
        System.out.println("after add:");
        System.out.print(matrix.add(tmp).toString());

        System.out.println("after multiply:");
        tmp = new Matrix(cin.nextInt(),cin.nextInt());
        tmp.input(cin);
        System.out.print(matrix.multiply(tmp));

        System.out.println("after transpose:");
        System.out.print(matrix.transpose().toString());
    }
}

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

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

    public void input(Scanner cin) {
        for(int i = 1; i <= this.row; i++) {
            for(int j = 1; j <= this.col; j++) {
                double val = cin.nextDouble();
                this.set(i,j,val);
            }
        }
    }

    // 将第row行第col列的元素赋值为value
    public void set(int row, int col, double value){
        d[--row][--col] = value;
    }

    // 取第row行第col列的元素
    public double get( int row, int col ){
        return d[--row][--col];
    }

    // 返回矩阵的列数
    public int width(){
        return col;
    }

    // 返回矩阵的行数
    public int height(){
        return row;
    }

    // 返回当前矩阵与矩阵b相加后的矩阵
    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;
    }

    // 返回当前矩阵与矩阵b相乘后的矩阵
    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 ){
                double 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;
    }

    // 返回当前矩阵的转置矩阵
    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;
    }

    public String toString(){
        DecimalFormat d = new DecimalFormat("#"); // 控制输出格式,无小数
        String ret = "";
        for(int i = 1; i <= this.row; ++ i){
            for( int j = 1; j <= this.col - 1; ++ j ){
                ret += d.format(this.get(i, j)) + " ";

            }
            ret += d.format(this.get(i, col)) + "\n";
        }
        return ret;
    }
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_Hello Spring

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值