【程序填空题】矩阵(运算符重载)

题目描述

下面主函数和Array类完成矩阵的输入、输出。请补齐Array类中需要的成员函数。

提示:根据已给代码分析CArray需要重载哪些运算符?

输入

测试次数

每组测试数据格式如下:

正整数n、m,分表表示矩阵的行、列

n*m行整数数据

输出

每组测试数据输出矩阵数据两次,具体输出格式见样例。

//

输入样例:

2

3 3

1 2 3

4 5 6

7 8 9

2 4

10 20 30 40

50 60 70 80

输出样例:

MatrixA:

1 2 3

4 5 6

7 8 9

MatrixB:

1 2 3

4 5 6

7 8 9

MatrixA:

10 20 30 40

50 60 70 80

MatrixB:

10 20 30 40

50 60 70 80

代码板块:

#include <iostream>

using namespace std;

class CArray //矩阵类

{

int n, m; //n行,m列

int **data; //存储矩阵数据

public:

CArray():data(nullptr){}

CArray(int nValue, int mValue);

~CArray();

//补齐CArray的其它成员函数及实现

/********** Write your code here! **********/

/*******************************************/

//CArray构造函数

CArray::CArray(int nValue, int mValue) : n(nValue), m(mValue)

{

// 分配n行m列空间

data = new int *n;

for (int i = 0; i < n; i++)

{

datai = new intm;

}

}

//CArray析构函数

CArray::~CArray()

{

// 释放空间

for (int i = 0; i < n; i++)

{

delete\[\] datai;

}

delete\[\] data;

}

//主函数

int main()

{

int t;

int n, m;

int i, j;

cin >> t;

while (t--)

{

cin >> n >> m; // 输入矩阵行n, 列m

CArray matrixA(n, m); // 定义matrixA对象

for (i = 0; i < n; ++i) // 输入n行m列数据

for (j = 0; j < m; j++)

cin >> matrixAij;

cout << "MatrixA:" << endl;

for (i = 0; i < n; ++i) // 输出matrixA中的数据

{

for (j = 0; j < m; j++)

{

cout << matrixA(i,j) << " ";

}

cout << endl;

}

cout << "MatrixB:" << endl;

CArray matrixB; // 定义matrixB对象

matrixB = matrixA; // 赋值

for (i = 0; i < n; ++i) // 输出matrixB对象

{

for (j = 0; j < m; j++)

{

cout << matrixBij << " ";

}

cout << endl;

}

}

return 0;

}

AC代码:

cpp 复制代码
#include <iostream>
using namespace std;
class CArray // 矩阵类
{
    int n, m;   // n行,m列
    int **data; // 存储矩阵数据
public:
    CArray() : data(nullptr) {}
    CArray(int nValue, int mValue);
    ~CArray();
    // 补齐CArray的其它成员函数及实现
    CArray &operator=(const CArray &rhs)
    {
        if (this == &rhs)
            return *this;
        if (data)
            delete[] data;
        n = rhs.n;
        m = rhs.m;
        data = new int *[n];
        for (int i = 0; i < n; i++)
        {
            data[i] = new int[m];
        }
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++)
            {
                {
                    data[i][j] = rhs.data[i][j];
                }
            }
        return *this;
    }

    int *operator[](int index)
    {
        return data[index];
    }

    int &operator()(int i, int j) const
    {
        return data[i][j];
    }
};

// CArray构造函数
CArray::CArray(int nValue, int mValue) : n(nValue), m(mValue)
{
    // 分配n行m列空间
    data = new int *[n];
    for (int i = 0; i < n; i++)
    {
        data[i] = new int[m];
    }
}
// CArray析构函数
CArray::~CArray()
{
    // 释放空间
    for (int i = 0; i < n; i++)
    {
        delete[] data[i];
    }
    delete[] data;
}
// 主函数
int main()
{
    int t;
    int n, m;
    int i, j;
    cin >> t;
    while (t--)
    {
        cin >> n >> m;          // 输入矩阵行n, 列m
        CArray matrixA(n, m);   // 定义matrixA对象
        for (i = 0; i < n; ++i) // 输入n行m列数据
            for (j = 0; j < m; j++)
                cin >> matrixA[i][j];
        cout << "MatrixA:" << endl;
        for (i = 0; i < n; ++i) // 输出matrixA中的数据
        {
            for (j = 0; j < m; j++)
            {
                cout << matrixA(i, j) << " ";
            }
            cout << endl;
        }
        cout << "MatrixB:" << endl;
        CArray matrixB;         // 定义matrixB对象
        matrixB = matrixA;      // 赋值
        for (i = 0; i < n; ++i) // 输出matrixB对象
        {
            for (j = 0; j < m; j++)
            {
                cout << matrixB[i][j] << " ";
            }
            cout << endl;
        }
    }
    return 0;
}
相关推荐
半个落月2 小时前
从递归到快速排序:用 JavaScript 把分治思想讲明白
javascript·算法·面试
小月土星3 小时前
JavaScript 快速排序:从 pivot、双指针到分治思想
javascript·算法·面试
小月土星3 小时前
JavaScript 递归入门:从 1 到 n 求和,再到数组扁平化
javascript·算法·面试
To_OC18 小时前
LC 1 两数之和:面试第一道必考题,暴力解法直接被面试官 pass
javascript·算法·leetcode
鱼鱼不愚与1 天前
《原来如此 | 第01期:为什么导航软件能预测红绿灯倒计时?》
算法
复杂网络1 天前
论最小 Agent 计算机的形态
算法
kisshyshy2 天前
🍦 雪糕、食堂、火车厢:三幅漫画吃透栈、队列与链表
javascript·算法
猿人谷2 天前
不只是 CPU 阈值:STAR 如何用 GAT + Transformer 做容器级自动扩缩容?
人工智能·算法
复杂网络2 天前
Stable Diffusion 视觉大模型微调技术深度调研
算法