Problem A: 强悍的矩阵运算来了(矩阵乘法,二维数组做类属性成员)

7 篇文章 0 订阅

 

 

int main()

{

    int cases, i;

    cin>>cases;

    for (i = 0; i < cases; i++)

    {

        Matrix A, B, C, D;

        cin>>A>>B;

        C = A + B;

        D = A * B;

        cout<<"Case "<<i + 1<<":"<<endl;

        cout<<C<<endl;

        cout<<D;

    }

    return 0;

}

写这个题的原因就是我看见矩阵,就想起来了上学期写c语言的时候的煎熬(还没学矩阵)以及当时学的二维数组指针的痛苦,于是我决定写一下二维数组指针的解法:

这里附上大神写的方法,我就不多赘述了(https://blog.csdn.net/weixin_42426940/article/details/107571118)

下面是我的代码:
 

#include<bits/stdc++.h>
using namespace std;
class Matrix
{
private:
    int **ppp;
    int hang,lie;
    bool flag;
public:
    friend istream& operator>>(istream& in,Matrix & abc)
    {
        in>>abc.hang>>abc.lie;
        abc.ppp = new int*[abc.hang];
        for(int i=0;i<abc.hang;i++)
        abc.ppp[i]=new int [abc.lie];
        for(int i=0;i<abc.hang;i++)
        {
            for(int j=0;j<abc.lie;j++)
            {
                in>>abc.ppp[i][j];
            }
        }
        return in;
    }
    friend ostream& operator<<(ostream& ou,Matrix & abc)
    {
        if(abc.flag == false){
                ou<<"Error"<<endl;
                return ou;
        }
        for(int i = 0;i<abc.hang;i++)
        {
            for(int j=0;j<abc.lie;j++)
            {
                if(j==0)
                ou<<abc.ppp[i][j];
                else ou<<" "<<abc.ppp[i][j];
            }
            ou<<endl;
        }
       // ou<<endl;
        return ou;
    }
    Matrix operator+(Matrix &another)
    {
        Matrix abc;
        abc.flag = true;
        if(another.hang != hang||another.lie != lie)
        {
            abc.flag=false;
            return abc;
        }
        abc.hang = hang;
        abc.lie = lie;
        abc.ppp = new int*[hang];
        for(int i=0;i<abc.hang;i++)
        abc.ppp[i] = new int[lie];
        for(int i=0;i<hang;i++)
        {
            for(int j=0;j<lie;j++)
            {
                abc.ppp[i][j] = ppp[i][j]+another.ppp[i][j];
            }
        }
        return abc;
    }
    Matrix operator*(Matrix &another)
    {
         Matrix abc;
        abc.flag = true;
        if(another.hang != lie)
        {
            abc.flag=false;
            return abc;
        }
        abc.hang = hang;
        abc.lie = another.lie;
        abc.ppp = new int*[hang];
        for(int i=0;i<abc.hang;i++)
        abc.ppp[i] = new int[another.lie];
        for(int i=0;i<hang;i++)
        {
            for(int j=0;j<another.lie;j++)
            {
                abc.ppp[i][j] =0;
                for(int k=0;k<lie;k++)
                    abc.ppp[i][j]+=another.ppp[k][j]*ppp[i][k];
            }
        }
        return abc;
    }
};

                                                                                                           记录当下,我的编程之路。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值