标题:矩阵相加
取消只看楼主
cwl168
Rank: 1
等 级:新手上路
帖 子:48
专家分:0
注 册:2012-12-14
结帖率:8.33%
 问题点数:0 回复次数:0 
矩阵相加
#include<iostream.h>

class Matrix
{
public:
    Matrix();
    void input();
    void display();
    friend Matrix operator +(Matrix &,Matrix &);
protected:
    int mat[2][3];
};
Matrix::Matrix()
{
    for(int i=0;i<2;i++)
        for(int j=0;i<3;j++)
            mat[i][j]=0;
}
void Matrix::input()
{
    cout<<"input value of matrix:"<<endl;
    for(int i=0;i<2;i++)
        for(int j=0;j<3;j++)
            cin>>mat[i][j];
}
void Matrix::display()
{
    for(int i=0;i<2;i++)
        for(int j=0;j<3;j++)
        {
            cout<<mat[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.mat[i][j]=a.mat[i][j]+b.mat[i][j];
        return c;
}
int main()
{
    Matrix a,b,c;
    cout<<"input a Matrix:"<<endl;
    a.input();
    b.input();
   
    cout<<endl<<"intput a Matrix:"<<endl;
    a.display();
    cout<<endl<<"intput b Matrix:"<<endl;

    b.display();
    c=a+b;
    cout<<endl<<"Matrix c=Matrix a+Matrix b:"<<endl;
    c.display;

    return 0;
}
搜索更多相关主题的帖子: public include display mat void 
2013-01-11 15:36



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-396250-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 1.935756 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved