当前位置: 首页 > news >正文

C++——有两个矩阵a和b,均为2行3列。求两个矩阵之和。重载运算符“+”,使之能用于矩阵相加。(如c=a+b)

没注释的源代码

#include <iostream>

using namespace std;

class matrix

{

private:

    int m[2][3];

public:

    matrix();

    void input();

    void display();

    friend matrix operator+(matrix&,matrix&);

};

matrix::matrix()

{

    for(int i=0;i<2;i++)

    {

        for(int j=0;j<3;j++)

        {

            m[i][j]==0;

        }

    }

}

void matrix::input()

{

    cout<<"the matrix:"<<endl;

    for(int i=0;i<2;i++)

    {

        for(int j=0;j<3;j++)

        {

            cin>>m[i][j];

        }

    }

}

void matrix::display()

{

    for(int i=0;i<2;i++)

    {

        for(int j=0;j<3;j++)

        {

            cout<<m[i][j]<<" ";

        }

        cout<<endl;

    }

}

matrix operator+(matrix&a,matrix&b)

{

    matrix c;

    for(int i=0;i<2;i++)

    {

        for(int j=0;j<3;j++)

        {

            c.m[i][j]=a.m[i][j]+b.m[i][j];

        }

    }

    return c;

}

int main()

{

    matrix a,b,c;

    a.input();

    b.input();

    cout<<"the matrix a:"<<endl;

    a.display();

    cout<<"the matrix b:"<<endl;

    b.display();

    c=a+b;

    cout<<"the matrix c:"<<endl;

    c.display();

    return 0;

}


http://www.mrgr.cn/news/53572.html

相关文章:

  • 动销方案在合作伙伴场地执行,好处多多!
  • S7-1500 通过PN/PN Coupler 通信
  • Python | Leetcode Python题解之第493题翻转对
  • UniHttp 框架,请求http接口
  • 文章解读与仿真程序复现思路——电网技术EI\CSCD\北大核心《基于AGCN-LSTM模型的海上风电场功率概率预测 》
  • 15分钟学Go 第8天:控制结构 - 循环
  • 请问平库管理系统有哪些功能流程?
  • 高职单招如何报考与备考?这份指南为你解惑
  • 使用django-simple-captcha遇到的坑
  • 杨氏矩阵(有一个数字矩阵,矩阵的每行从左到右的递增的,矩阵从上到下是递增的请编写一个程序,在这样的矩阵中查找某个数字是否存在)
  • 2023年华为杯数学建模竞赛题F论文和代码
  • Python库matplotlib之十二
  • 【大模型】AI视频课程制作工具开发
  • Python 网络编程:端口检测与IP解析
  • 查缺补漏----Cache命中率与缺失率的判断
  • SpringCloud学习:Seata分布式事务处理
  • ICM20948 DMP代码详解(88)
  • 研报复现连载
  • WebForms Hashtable
  • ICM20948 DMP代码详解(87)