• PTA: 矩阵的乘法运算


    题目

    线性代数中的矩阵可以表示为一个row*column的二维数组,当row和column均为1时,退化为一个数,当row为1时,为一个行向量,当column为1时,为一个列向量。
    建立一个整数矩阵类matrix,其私有数据成员如下:

    int row;
    int column;
    int **mat;
    
    • 1
    • 2
    • 3

    建立该整数矩阵类matrix构造函数;
    建立一个 *(乘号)的运算符重载,以便于对两个输入矩阵进行乘法运算;
    建立输出函数void display(),对整数矩阵按行进行列对齐输出,格式化输出语句如下:

    cout<<setw(10)<<mat[i][j];
    //需要#include 
    
    • 1
    • 2

    主函数里定义三个整数矩阵类对象m1、m2、m3.

    输入格式

    分别输入两个矩阵,分别为整数矩阵类对象m1和m2。
    每个矩阵输入如下:
    第一行两个整数 r c,分别给出矩阵的行数和列数
    接下来输入r行,对应整数矩阵的每一行
    每行输入c个整数,对应当前行的c个列元素

    输出格式

    整数矩阵按行进行列对齐(宽度为10)后输出
    判断m1和m2是否可以执行矩阵相乘运算。
    若可以,执行m3=m1*m2运算之后,调用display函数,对m3进行输出。
    若不可以,输出"Invalid Matrix multiplication!"
    提示:输入或输出的整数矩阵,保证满足row>=1和column>=1。

    输入样例

    4  5
    1 0 0 0 5
    0 2 0 0 0
    0 0 3 0 0
    0 0 0 4 0
    5  5
    1 2 3 4 5
    2 3 4 5 6
    3 4 5 6 7
    4 5 6 8 9
    5 6 7 8 9
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    输出样例

            26        32        38        44        50
             4         6         8        10        12
             9        12        15        18        21
            16        20        24        32        36
    
    • 1
    • 2
    • 3
    • 4

    代码

    #include 
    #include 
    using namespace std;
    class matrix{
    private:
        int row,column;
        int **mat;
    public:
        matrix(const matrix& mx){
            this->row=mx.row;
            this->column=mx.column;
            this->mat=new int*[row];
            for (int i = 0; i <row ; ++i) {
                this->mat[i]=new int[column];
                for (int j = 0; j <column ; ++j) {
                    this->mat[i][j]=mx.mat[i][j];
                }
            }
        }
        matrix(int r,int c){
            row=r;
            column=c;
                mat=new int*[row];
                for (int i = 0; i <row ; ++i) {
                    mat[i]=new int[column];
                    for (int j = 0; j <column ; ++j) {
                        mat[i][j]=0;
                    }
                }
        }
        ~matrix(){
            for (int i = 0; i <row ; ++i) {
                delete []mat[i];
            }
            delete []mat;
        }
        void read(){
            for (int j = 0; j <row ; ++j) {
                for (int i = 0; i <column ; ++i) {
                    cin>>mat[j][i];
                }
            }
        }
        matrix operator*(matrix& mt){
            if(this->row==1&&this->column==1){
                for (int i = 0; i < mt.row; ++i) {
                    for (int j = 0; j <mt.column ; ++j) {
                        mt.mat[i][j]=this->mat[0][0]*mt.mat[i][j];
                    }
                }
                return mt;
            }else{
                matrix rs(this->row,mt.column);
                for (int i = 0; i < this->row; ++i) {
                    for (int j = 0; j <mt.column ; ++j) {
                        for (int k = 0; k <mt.row ; ++k) {
                            rs.mat[i][j]+=this->mat[i][k]*mt.mat[k][j];
                        }
                    }
                }
                return rs;
            }
        }
        void display(){
            for (int i = 0; i <row ; ++i) {
                for (int j = 0; j <column ; ++j) {
                    cout<<setw(10)<<mat[i][j];
                }
                cout<<endl;
            }
        }
    };
    int main()
    {
        int r,c;
        cin>>r>>c;
        matrix m1(r,c);
        m1.read();
        int r1,c1;
        cin>>r1>>c1;
        matrix m2(r1,c1);
        m2.read();
        if(c==r1||r==1&&c==1){
            matrix m3=m1*m2;
            m3.display();
        }
        else{
            cout<<"Invalid Matrix multiplication!";
        }
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
  • 相关阅读:
    WebSocket
    07-输入输出系统
    rust 过程宏
    MySQL笔记
    MES如何做好生产过程监控,本文给出了详细解答
    uniapp升级中心的如何创建和使用
    如何成功安装Node.js并在VS Code终端使用npm
    【Docker】——容器数据卷
    Intel汇编-查看命令行参数
    解锁C语言结构体的力量(初阶)
  • 原文地址:https://blog.csdn.net/m0_74195626/article/details/134121240