面向对象程序设计上机练习十一(运算符重载)
Time Limit: 1000MS
Memory Limit: 65536KB
Problem Description
有两个矩阵a和b,均为2行3列,求两个矩阵之和。重载运算符“+”,使之能用于矩阵相加。如:c=a+b。
Input
第1、2行是矩阵a的值,数据以空格分开。
第3、4行是矩阵b的值,数据以空格分开。
第3、4行是矩阵b的值,数据以空格分开。
Output
2个矩阵a、b之和,以行列形式显示。
Example Input
2 3 4 3 5 3 4 3 1 5 4 3
Example Output
6 6 5 8 9 6
Hint
Author
zlh
#include <iostream>
using namespace std;
class Complex
{
private:
int x[3];
int y[3];
public:
Complex(int a=0,int b=0,int c=0,int d=0,int e=0,int f=0);
friend Complex operator+(Complex &a,Complex &b);
void show();
};
Complex::Complex(int a,int b,int c,int d,int e,int f)
{
x[0]=a;x[1]=b;x[2]=c;y[0]=d;y[1]=e;y[2]=f;
}
Complex operator+(Complex &a,Complex &b)
{
Complex c;
c.x[0]=a.x[0]+b.x[0];
c.x[1]=a.x[1]+b.x[1];
c.x[2]=a.x[2]+b.x[2];
c.y[0]=a.y[0]+b.y[0];
c.y[1]=a.y[1]+b.y[1];
c.y[2]=a.y[2]+b.y[2];
return c;
}
void Complex::show()
{
int i;
for(i=0;i<3;i++)
{
if(i==0)
cout<<x[i];
else
cout<<' '<<x[i];
}
cout<<endl;
for(i=0;i<3;i++)
{
if(i==0)
cout<<y[i];
else
cout<<' '<<y[i];
}
cout<<endl;
}
int main()
{
int a,b,c,d,e,f;
cin>>a>>b>>c;
cin>>d>>e>>f;
Complex d1(a,b,c,d,e,f);
cin>>a>>b>>c;
cin>>d>>e>>f;
Complex d2(a,b,c,d,e,f);
Complex d3;
d3=d1+d2;
d3.show();
return 0;
}