SDUT-1273 面向对象程序设计上机练习十一(运算符重载)

面向对象程序设计上机练习十一(运算符重载)

Time Limit: 1000MS Memory Limit: 65536KB
Problem Description
有两个矩阵a和b,均为2行3列,求两个矩阵之和。重载运算符“+”,使之能用于矩阵相加。如:c=a+b。
Input
第1、2行是矩阵a的值,数据以空格分开。
第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;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值