编译器:C++ (g++)根据main函数中矩阵对象的定义与使用,定义相关的矩阵类Array,并利用运算符重载的方法实现矩阵的加法与输入输出操作。

编译器:C++ (g++)

根据main函数中矩阵对象的定义与使用,定义相关的矩阵类Array,并利用运算符重载的方法实现矩阵的加法与输入输出操作。(为简化问题,矩阵中元素为2位以内整数,要求矩阵按照行列的格式输出,每个元素占3位宽度)

类定义:

class Array

/* 请在这里填写答案 */

测试程序样例:

int main()

{

    Array arr1,arr2,arr3;

    cin>>arr1;

    cin>>arr2; 

    cout<<arr1<<endl;

    cout<<arr2<<endl;

    arr3=arr1+arr2;

    cout<<arr3;

    return 0;    

}

输入样例:

1 2 3 4 5 6

7 8 9 1 11 12

输出样例:

  1  2  3

  4  5  6

  7  8  9

  1 11 12

  8 10 12

  5 16 18


#include <iostream>
#include <iomanip>
using namespace std;

class Array
{
public:
    friend istream &operator>>(istream &in, Array &a);
    friend ostream &operator<<(ostream &out, Array &a);
    friend Array operator+(Array &a1, Array &a2);
private:
    int x1,x2,x3;
    int y1,y2,y3;
};

istream &operator>>(istream &in, Array &a)
{
    in>>a.x1>>a.x2>>a.x3>>a.y1>>a.y2>>a.y3;
    return in;
}

ostream &operator<<(ostream &out, Array &a)
{
    out<<setw(3)<<a.x1<<setw(3)<<a.x2<<setw(3)<<a.x3<<endl;
    out<<setw(3)<<a.y1<<setw(3)<<a.y2<<setw(3)<<a.y3<<endl;
    return out;
}

Array operator+(Array &a1, Array &a2)
{
    Array a;
    a.x1 = a1.x1 + a2.x1;
    a.x2 = a1.x2 + a2.x2;
    a.x3 = a1.x3 + a2.x3;
    a.y1 = a1.y1 + a2.y1;
    a.y2 = a1.y2 + a2.y2;
    a.y3 = a1.y3 + a2.y3;
    return a;
}

int  main()
{
        Array  arr1,arr2,arr3;
        cin>>arr1;
        cin>>arr2;
        cout<<arr1<<endl;
        cout<<arr2<<endl;
        arr3=arr1+arr2;
        cout<<arr3;
        return  0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

那不勒斯的萤火丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值