1552 简单的Matrix运算

Task

完善Matrix类的定义,实现简单的Matrix运算,类的定义,main函数已给出,你需要编写Matrix.h文件实现具体函数,记得包含类的定义。

Detail

class Matrix
    //定义Matrix类  
{
public:
    Matrix();                                           //默认构造函数
    ~Matrix();                                          //析构函数
    Matrix(const Matrix &);                             //拷贝构造函数
    Matrix(int row, int col);                           //含参数构造函数  
    Matrix operator+(const Matrix &)const;              //重载运算符“+”
    Matrix& operator=(const Matrix &);                  //重载运算符“=”
    Matrix transpose()const;                            //矩阵的转置
    void display()const;                                //输出数据函数   
private:
    int row;                                            //矩阵的行
    int col;                                            //矩阵的列
    int** mat;                                          //用于储存矩阵
};

Hint

类的构造函数与析构函数,运算符重载,new与delete

Sample Input 1

2 3
1 2 3
1 2 3
1 2 3
1 2 3

Sample Output 1


Matrix a:
1 2 3
1 2 3

Matrix b:
1 2 3
1 2 3

Matrix c = Matrix a + Matrix b :
2 4 6
2 4 6

Matrix a transpose to Matrix d:
1 1
2 2
3 3

Sample Input 2

2 2
1 2
3 4
4 3
2 1

Sample Ouput 2


Matrix a:
1 2
3 4

Matrix b:
4 3
2 1

Matrix c = Matrix a + Matrix b :
5 5
5 5

Matrix a transpose to Matrix d:
1 3
2 4

Provided Codes

Matrix.cpp

#include <iostream>
#include"Matrix.h"
using namespace std;
int main() {
    int row, col;
    cout << "input the row and the col for Matrix a, b" << endl;
    cin >> row >> col;
    Matrix a(row, col), b(row, col), c(a), d;
    cout << endl << "Matrix a:" << endl;
    a.display();
    cout << endl << "Matrix b:" << endl;
    b.display();
    c = a + b;//用重载运算符“+”实现两个矩阵相加 
    cout << endl << "Matrix c = Matrix a + Matrix b :" << endl;
    c.display();
    cout << endl << "Matrix a transpose to Matrix d:" << endl;
    d = a.transpose();
    d.display();
    return 0;
}

Submission

Matrix.h

#ifndef MATRIX_H
#define MATRIX_H
#include <iostream>
#include <assert.h>

using namespace std;

class Matrix
    //定义Matrix类  
{
public:
    Matrix();                                           //默认构造函数
    ~Matrix();                                          //析构函数
    Matrix(const Matrix &);                             //拷贝构造函数
    Matrix(int row, int col);                           //含参数构造函数  
    Matrix operator+(const Matrix &)const;              //重载运算符“+”
    Matrix& operator=(const Matrix &);                  //重载运算符“=”
    Matrix transpose()const;                            //矩阵的转置
    void display()const;                                //输出数据函数   
private:
    int row;                                            //矩阵的行
    int col;                                            //矩阵的列
    int** mat=NULL;                                          //用于储存矩阵
};

Matrix::Matrix() :
    row(0), col(0) {

}

Matrix::Matrix(int Row, int Col) {
    row = Row;
    col = Col;
    mat = new int*[row];
    for (int i = 0; i < row; i++) {
        mat[i] = new int[col];
    }
    assert(mat != NULL);
    for (int i = 0; i < row; i++) {
        for (int j = 0; j < col; j++) {
            cin>>mat[i][j];
        }
    }
}

Matrix::Matrix(const Matrix &m) {
    row = m.row;
    col = m.col;
    mat = new int*[row];
    for (int i = 0; i < row; i++) {
        mat[i] = new int[col];
    }
    assert(mat != NULL);
    for (int i = 0; i < row; i++) {
        for (int j = 0; j < col; j++) {
            mat[i][j]=m.mat[i][j];
        }
    }
}

Matrix::~Matrix() {
    for (int i = 0; i < row; i++) {
        delete[]mat[i];
    }
    delete[]mat;
}

Matrix Matrix::operator+(const Matrix &b)const{
    Matrix c;
    c.row = row;
    c.col = col;
    c.mat = new int*[row];
    for (int i = 0; i < row; i++) {
        c.mat[i] = new int[col];
    }
    assert(c.mat != NULL);
    for (int i = 0; i < row; i++) {
        for (int j = 0; j < col; j++) {
            c.mat[i][j] = mat[i][j]+b.mat[i][j];
        }
    }
    return c;
}

Matrix& Matrix::operator=(const Matrix &p) {
    if (row != p.row||col != p.col) {
        for (int i = 0; i < row; i++) {
            delete[]mat[i];
        }
        delete[]mat;
        row = p.row;
        col = p.col;
        mat = new int*[row];
        for (int i = 0; i < row; i++) {
            mat[i] = new int[col];
        }
        assert(mat != NULL);
    }   
    for (int i = 0; i < row; i++) {
        for (int j = 0; j < col; j++) {
            mat[i][j] = p.mat[i][j];
        }
    }
    return *this;
}

Matrix Matrix::transpose()const {
    Matrix a;
    a.row = col;
    a.col = row;
    a.mat = new int*[col];
    for (int i = 0; i < col; i++) {
        a.mat[i] = new int[row];
    }
    assert(a.mat != NULL);
    for (int i = 0; i < row; i++) {
        for (int j = 0; j < col; j++) {
            a.mat[j][i] = mat[i][j];
        }
    }
    return a;
}

void Matrix::display()const {
    for (int i = 0; i < row; i++) {
        for (int j = 0; j < col; j++) {
            cout << mat[i][j]<<" ";
            if (j == col - 1) {
                cout << endl;
            }
        }
    }
}

#endif

Standard Answer

Matrix.h

#include <iostream>
using namespace std;
class Matrix
    //定义Matrix类  
{
public:
    Matrix();                                           //构造函数
    ~Matrix();                                          //析构函数
    Matrix(const Matrix &);                                   //拷贝构造函数
    Matrix(int row, int col);                           //默认构造函数  
    Matrix operator+(const Matrix &)const;        //重载运算符“+”
    Matrix operator=(const Matrix &);                         //重载运算符“=”
    Matrix transpose()const;                                 //矩阵的转置
    void display()const;                                     //输出数据函数   
private:
    int row;                                            //矩阵的行
    int col;                                            //矩阵的列
    int** mat;                                          //用于储存矩阵
};
Matrix::Matrix() {
    row = 0;
    col = 0;
    mat = NULL;
}
Matrix::~Matrix() {
    if(mat != NULL){
        for (int i = 0; i < row; i++) {
            delete[]mat[i];
        }
    delete[]mat;
    mat = NULL;
    }
}
Matrix::Matrix(const Matrix& a) {
    row = a.row;
    col = a.col;
    mat = new int*[row];
    for (int i = 0; i < row; i++) {
        mat[i] = new int[col];
    }
    for (int i = 0; i < row; i++) {
        for (int j = 0; j < col; j++) {
            mat[i][j] = a.mat[i][j];
        }
    }
}
Matrix::Matrix(int row, int col)
//定义构造函数 
{
    this->row = row;
    this->col = col;
    mat = new int*[row];
    for (int i = 0; i < row; i++) {
        mat[i] = new int[col];
    }
    for (int i = 0; i<row; i++)
        for (int j = 0; j<col; j++)
            cin >> mat[i][j];
}
Matrix Matrix::operator+(const Matrix &b)const
//定义重载运算符“+”函数 
{
    Matrix c(b);
    for (int i = 0; i<b.row; i++)
        for (int j = 0; j<b.col; j++)
        {
            c.mat[i][j] = mat[i][j] + b.mat[i][j];
        }
    return c;
}
Matrix Matrix::operator=(const Matrix& a)
{
    row = a.row;
    col = a.col;
    if (mat == NULL) {
        mat = new int*[row];
        for (int i = 0; i < row; i++) {
            mat[i] = new int[col];
        }
    }
    for (int i = 0; i < row; i++) {
        for (int j = 0; j < col; j++) {
            mat[i][j] = a.mat[i][j];
        }
    }
    return *this;
}

Matrix Matrix::transpose()const {
    Matrix d;
    d.row = col;
    d.col = row;
    d.mat = new int*[col];
    for (int i = 0; i < col; i++) {
        d.mat[i] = new int[row];
    }
    for (int j = 0; j < col; j++)
        for (int i = 0; i < row; i++)
        {
            d.mat[j][i] = mat[i][j];
        }
    return d;
}

void Matrix::display()const
//定义输出数据函数 
{
    for (int i = 0; i<row; i++) {
        for (int j = 0; j<col; j++) {
            cout << mat[i][j] << " ";
        }
        cout << endl;
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现有两个类 CVector 存放数据的自定义动态数组,采用一维动态数组存储矩阵数据 CMatrix 实现的矩阵类 使用的时候包含#include "Matrix.h"就行 CMatrix的接口函数都在"Matrix.h"里面 CVector的接口函数在"Vector.h"里,"Matrix.h"里包含了"Vector.h" 具体用法与测试用例Main.cpp里有3个测试用例,分别是针对构造函数属性计算与运算符重载的 内已包含测试工程xp\vc6.0\上亲测通过,并经过BoundsChecker测试没有内存泄漏。有兴趣的童鞋可以下作参考。 注意: 1、下标都是从0开始,数学课上矩阵下标都是从1开始,但是工作后习惯0开始,矩阵M的第一个元素是M(0,0) 2、类型定死为double,原来作业是模板类,由于vc6对模版支持不好,另矩阵计算double类比较理想、整型几乎只能作加减 提供了多种初始化方式,int[]、float[]、double[]均可构造初始化,或则先构造出CVector再由CVector初始化。 3、定义了一个最大允许误差#define permit_eof (1.0e-13),判断相等使用宏 #define EQUAL(a,b) ( ((b) - (a) < permit_eof)&&((a) - (b) < permit_eof) ? (TRUE) : (FALSE) ) 正常输出的时候绝对值小于permit_eof 的时候清零处理,想要指定精度输出请用PrintOut 鸣谢:CSDN上supermegaboy君的C/C++左值性精髓,读后略有所感,空闲时重构了下大学时的作业,着重区分了函数返回值的左右值 =================================================附录:接口函数========================================================================== 开放的接口: CVector //构造函数 CVector(); virtual ~CVector(); CVector(const size_t &nSize;); CVector(const CVector & vIn);//拷贝构造函数 CVector(const double* const pData,const size_t &nSize;); CVector(const float* const pData,const size_t &nSize;); CVector(const int* const pData,const size_t &nSize;); //公开的成员函数 double at(const size_t& uIndex)const;//作右值 BOOL push_back(const double& dbIn ); BOOL resize(const size_t& nSize); size_t size()const; //重载操作符 double& operator()(const UINT& uIndex);//重载()运算符,可作左值 //重载的运算符 double& operator()(const size_t& xr,const size_t& xc);//重载()运算符,可作左值 CVector& operator=(const CVector &);//重载=运算符 double operator*(const CVector & )const;//重载*运算符,两向量相乘 CVector operator*(const double α)const;//重载*运算符,向量乘以实数alpha CVector& operator*=(const double α);//重载*=算符,向量乘以实数alpha CVector operator+(const CVector & )const;//重载+运算符,向量加上向量 CVector& operator+=(const CVector & );//重载+=算符,向量加上向量 CVector operator-(const CVector & )const;//重载+运算符,向量加上向量 CVector& operator-=(const CVector & );//重载+=算符,向量加上向量 CVector operator+(const double α)const;//重载+运算符,向量加上实数alpna CVector& operator+=(const double α);//重载+=算符,向量加上实数alpha BOOL operator==(const CVector &)const;//重载==运算符 BOOL operator!=(const CVector &)const;//重载!=运算符 CMatrix //构造函数 CMatrix(); virtual ~CMatrix(); CMatrix(const CMatrix&);//拷贝构造函数 CMatrix(const size_t& n);//产生n阶单位阵 CMatrix(const size_t& nrow, const size_t& ncol);// CMatrix(const size_t& nrow, const size_t& ncol,const CVector& xdata);//产生nrow行,ncol列矩阵数据由xdata初始化 CMatrix(const size_t& nrow, const size_t& ncol,const double*const pData); CMatrix(const size_t& nrow, const size_t& ncol,const float* const pData); CMatrix(const size_t& nrow, const size_t& ncol,const int* const pData); //公开的成员函数 double At(const size_t& xr,const size_t& xc) const;//这个只能作为右值 CMatrix Trans()const;//A.T()返回矩阵A的转置副本 CVector diag()const;//矩阵上三角化后的对角向量//以此求矩阵的秩,矩阵的行列式等 double det()const;//求矩阵行列式 size_t rank()const;//矩阵的秩 CMatrix Inv()const;//求逆矩阵 inline BOOL IsNullMatrix()const{ return (BOOL)(m_nRowlen==0 || m_nCollen == 0);};//是否是空矩阵 BOOL IsSingularMatrix()const;//是否是奇异矩阵//即行列式为0 友函数 //科学计数法输出//想看较精确的数据的时候 friend void void PrintOut(const CMatrix& M,const size_t& nprecision = 6,std::ostream& os = std::cout); //产生的随机方阵,一般会是非异阵,供测试用 friend CMatrix randMatrix(const size_t &uSize;,int MAX);//随机产生n阶的方阵 //--------------------------重载部分-Overloaded Part---------------------------------- CMatrix& operator=(const CMatrix &);//重载=赋值运算符 double& operator()(size_t xr,size_t xc);//重载()运算符,A(i,j)即矩阵A的i行j列的元素, friend std::ostream & operator<<(std::ostream & ,const CMatrix &);//重载<<,可用cout输出矩阵 friend std::ostream & operator<<(std::ostream & ,const CVector&);//重载<<,可用cout输出向量 friend std::istream & operator>>(std::istream & CMatrix &);//重载>>,可用cin输入矩阵 CMatrix operator*(const double α)const;//重载*运算符,矩阵乘以实数alpha CVector operator*(const CVector &)const;//重载*运算符,矩阵乘以向量 CMatrix operator*(const CMatrix &)const;//重载*运算符,矩阵相乘 CMatrix& operator*=(const CMatrix &);//重载*=运算符 CMatrix operator^(const int α)const;//重载^幂运算符,A^alpha,alpha可以为负整数 CMatrix operator+(const CMatrix &)const;//重载+运算符,矩阵相加 CMatrix& operator+=(const CMatrix &);//重载+=运算符 CMatrix operator-(const CMatrix &)const;//重载- CMatrix& operator-=(const CMatrix &);//重载-=运算符 BOOL operator==(const CMatrix &)const;//重载==运算符,判断矩阵是否相等 BOOL operator!=(const CMatrix &)const;//重载!=运算符,判断矩阵是否不相等 CVector operator/(const CVector &)const;//重载/除运算符,向量左除矩阵,求Ax=b的x向量
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值