您的位置:首页 > 其它

有两个矩阵a和b,均为2行3列。求两个矩阵之和。重载<< >>

2011-03-20 18:52 1376 查看
有两个矩阵a和b,均为2行3列。求两个矩阵之和。重载运算符“+”,使之能用于矩阵相加,如:c=a+b,重载流插入运算符<< 和流提取运算符>>,使之能用于矩阵的输入输出。

#include<iostream>
using namespace std;
class Arrary
{
public:
Arrary();

int a[2][3];

};

Arrary::Arrary()
{
int i,j;
for(i=0;i<2;i++)
for(j=0;j<3;j++)
a[i][j]=0;

}
istream & operator >>(istream &input,Arrary &c1)
{

cout<<"请输入数组:";
int i,j;
for(i=0;i<2;i++)
for(j=0;j<3;j++)
input>>c1.a[i][j];
return input;
}

ostream & operator <<(ostream &output,Arrary &c1)
{

int i,j;
for(i=0;i<2;i++)
{   for(j=0;j<3;j++)
output<<c1.a[i][j]<<" ";
output<<endl;
}
return output;
}
Arrary operator +(Arrary c1,Arrary c2)
{
Arrary c3;
int i,j;
for(i=0;i<2;i++)
for(j=0;j<3;j++)
{
c3.a[i][j]=c1.a[i][j]+c2.a[i][j];
}
return c3;
}

int main()
{

Arrary a,b,c;
cin>>a;
cin>>b;
c=a+b;
cout<<c;

}


本文出自 “Chaos代码空间” 博客,请务必保留此出处http://flzt5354.blog.51cto.com/1568074/520770
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐