C++实现简单的矩阵运算

利用C++实现矩阵的构造,通过运算符的重载实现矩阵的乘法、加法等。并且实现矩阵形状的打印,矩阵的打印。

#include<iostream>
#include<memory>
#include<assert.h>
#include<stdlib.h>
using namespace std;

class Matrix{
    public:
        Matrix(int row, int col);   //构造函数
        Matrix(int row, int col, int num);//构造函数重载
        ~Matrix();                  //析构函数
        Matrix(const Matrix & other); //赋值函数
        Matrix operator*(const Matrix& other);      //矩阵相乘
        Matrix operator+(const Matrix& other);      //矩阵相加
        Matrix operator-(const Matrix& other);      //矩阵相减
        int **a = nullptr;          //初始化一共空指针
        int row, col;
        void shape();               //打印矩阵形状
        void Ma_pri();              //打印矩阵
};

int main(){
    Matrix a(2,1);      //构造一个(2,1)矩阵
    Matrix b(1,2);      //构造一个(1,2)矩阵
    a.a[0][0] = 4;      //初始化矩阵
    a.a[1][0] = 2;
    b.a[0][0] = 3;
    b.a[0][1] = 5;
    a.shape();          //矩阵形状打印
    b.shape();
    Matrix c = a*b;     //矩阵相乘
    c.shape();
    c.Ma_pri();         //矩阵打印
    Matrix d(3,3,1);
    d.Ma_pri();
    system("pause");
    return 0;
}


Matrix::Matrix(int row, int col){
    this->row = row;
    this->col = col;
    this->a = new int*[row];
    for(int i=0;i<this->row;i++){
        a[i] = new int[this->col];
    }
}


Matrix::Matrix(int row, int col, int num){
    this->row = row;
    this->col = col;
    this->a = new int*[row];
    for(int i=0;i<this->row;i++){
        a[i] = new int[this->col];
    }
    for(int i = 0; i < this->row; i++){
        for(int j =0; j <this->row; j++){
            this->a[i][j] = num;
        }
    }
}



Matrix::~Matrix(){
    for(int i=0;i<this->row;i++){
        if(a[i] != nullptr){
            delete[] a[i];
            a[i] = nullptr;
        }
    }
    if(a != nullptr){
        delete[] a;
        a = nullptr;
    }
}

Matrix::Matrix(const Matrix& other){
    row = other.row;
    col = other.col;
    a = new int*[row];
    for(int i=0;i<row;i++){
        a[i] = new int[col];
        memcpy(a[i], other.a[i],sizeof(int)*col);
    }
}

Matrix Matrix::operator*(const Matrix& other){
    if(this->col != other.row){
        cout<<"shape error"<<endl;
        exit(0);
    }
    Matrix m(this->row,other.col);
    for(int i=0; i<this->row; i++){
        for(int j=0;j<other.col;j++){
            int sum = 0;
            for(int k=0;k<this->col;k++){
                sum += this->a[i][k] * other.a[k][j];
            }
            m.a[i][j] = sum;
        }
    }
    return m;
}

Matrix Matrix::operator+(const Matrix& other){
    if(this->col != other.col or this->row != other.row){
        cout<<"shape error"<<endl;
        exit(0);
    }
    Matrix m(this->row,this->col);
    for(int i = 0;i < this->row; i++){
        for(int j = 0; j < this-> col; j++){
            m.a[i][j] = this->a[i][j] + other.a[i][j];
        }
    }
    return m;
}
Matrix Matrix::operator-(const Matrix& other){
    if(this->col != other.col or this->row != other.row){
        cout<<"shape error"<<endl;
        exit(0);
    }
    Matrix m(this->row,this->col);
    for(int i = 0;i < this->row; i++){
        for(int j = 0; j < this-> col; j++){
            m.a[i][j] = this->a[i][j] - other.a[i][j];
        }
    }
    return m;
}

void Matrix::shape(){
    cout<<"("<<this->row<<","<<this->col<<")"<<endl;
}

void Matrix::Ma_pri(){
    for(int i = 0; i < this->row; i++){
        for(int j =0; j <this->row; j++){
            cout<<this->a[i][j]<<" ";
        }
        cout<<endl;
    }
}
  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值