C++利用类实现矩阵的数乘,乘法以及点乘

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

C++利用类实现矩阵的数乘,乘法以及点乘

cc‍   2022-11-19 我要评论

 程序的探索经过如下:

 ①首先考虑到后续代码的维护及其复用性,为此考虑用类实现

 ②矩阵基本的类成员应该包括矩阵的行列数以及矩阵名以及初始化、输入输出

 ③由于需要实现三种矩阵乘法运算,为此考虑利用运算符重载使用友元函数重载* +运算符分别实   现矩阵的数乘、乘法以及点乘

源码如下:

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long ll;
class Matrix                                          //封装一个矩阵类便于后续使用
{
    int row;
    int col;                                       
    int** base;                                       //二维数组存放矩阵
public:
    Matrix(int r,int c)                               //构造函数初始化
    {
        row = r;
        col = c;
        create();
    }
    //在外部则写作:
    //Matrix::Matrix(int r, int c)
    //{
    //  row=r; col=c; create();
    //} 
    Matrix(const Matrix& e)                            //拷贝构造函数
    {
        row = e.getrow();
        col = e.getcol();
        create();
        for(int i = 0;i<row;i++)
        {
            for(int j = 0;j<col;j++)
            {
                base[i][j] = e[i][j];
            }
        }
    }
    ~Matrix()                                          //析构函数防止内存泄露
    {
        destroy();
    }
    Matrix& operator=(const Matrix& e)                //重载等于号实现深拷贝
    {
        destroy();
        row = e.getrow();
        col = e.getcol();
        create();
        for(int i = 0;i<row;i++)
        {
            for(int j = 0;j<col;j++)
            {
                base[i][j] = e[i][j];
            }
        }
        return *this;
    }
    int* operator[](int i)const                       //重载[]以方便用"变量名[][]" 的形式读写矩阵内容
    {
        return base[i];
    }
    int getrow()const                                 //对外提供矩阵信息
    {
        return row;
    }
    int getcol()const                                  //对外提供矩阵信息
    {
        return col;
    }
    friend Matrix operator*(int n,Matrix &e);        //重载 * 实现数乘 
    friend Matrix operator*(Matrix& e,Matrix& f);    //重载 * 实现矩阵乘法 
    friend Matrix operator+(Matrix& e,Matrix& f);    //重载 + 实现矩阵点乘 
private:
    void create()                                      //创建一个二维数组
    {
        base = new int*[row];
        for(int i = 0;i<row;i++)
        {
            base[i] = new int[col];
        }
    }
    void destroy()                                     //释放一个二维数组
    {
        for(int i = 0;i<row;i++)
        {
            delete[] base[i];
        }
        delete[] base;
    }
};
ostream& operator<<(ostream& cout,const Matrix& e)     //重载<<方便输出
{
    int r = e.getrow();
    int c = e.getcol();
    cout<<"row="<<r<<" col="<<c<<endl;
    for(int i = 0;i<r;i++)
    {
        for(int j = 0;j<c;j++)
        {
            cout<<e[i][j]<<" ";
        }
        cout<<endl;
    }
    return cout;
}
istream& operator>>(istream& cin,Matrix& e)           //重载>>方便输入
{ 
    int r = e.getrow();
    int c = e.getcol();
    for(int i = 0;i<r;i++)
    {
        for(int j = 0;j<c;j++)
        {
            cin>>e[i][j];
        }
    }
    return cin;
}
Matrix operator*(int n,Matrix& e)              //重载 * 实现数乘
{
    Matrix ret(e.getrow(),e.getcol()); 
    for(int i=0;i<e.getrow();++i)
     {
         for(int j=0;j<e.getcol();++j)
         {
             ret[i][j]=n*e[i][j];
         }
     }
    return ret;      
}
Matrix operator*(Matrix& e,Matrix& f)        //重载 * 实现矩阵乘法 
{
    Matrix ret(e.getrow(),f.getcol());
    for(int i = 0;i<e.getrow();++i)
    {
        for(int j = 0;j<f.getcol();++j)
        {
             ret[i][j] = 0;
             for(int k = 0;k<e.getcol();++k)
             {
                ret[i][j]+= e[i][k]*f[k][j];
             }                
        }
    }
    return ret;
}
Matrix operator+(Matrix& e,Matrix& f)      //重载 + 实现矩阵点乘 
{
    Matrix ret(e.getrow(),e.getcol());     //矩阵 e 与矩阵 f为同形矩阵 取谁的行列数都一样
    for(int i=0;i<e.getrow();++i)
    {
        for(int j=0;j<e.getcol();++j)
        {
            ret[i][j]=e[i][j]*f[i][j];
        }
     } 
    return ret;
}
int main()
{
    cout<<"请输入要进行的运算(包括数乘、乘法以及点乘)"<<endl;
    cout<<"数乘 1  乘法 2  点乘3 "<<endl; 
    int choice;
    cin>>choice;
    switch (choice)
    {
        case 1:
            { 
             int array1[2];
             cout<<"请输入矩阵的行数列数"<<endl;
             for (int i=0;i<2;++i)
                cin>>array1[i];
             int num;
             Matrix A(array1[0],array1[1]);
             cout<<"请输入乘数"<<endl;
             cin>>num;
             cout<<"请给矩阵A赋值"<<endl;
             cin>>A;
             cout<<"num与A数乘\n"; 
             cout<<num*A;
            }
        break;
        case 2: 
            {
             int array2[4];
             cout<<"请输入矩阵的行数列数"<<endl;
             for (int j=0;j<4;++j)
                cin>>array2[j];
             if(array2[1]!=array2[2])
              {
                  cout<<"第一个矩阵列数不等于第二个矩阵行数"<<"\n";
                  cout<<"请重启动再次输入"<<endl;
                }
                else
             {
                Matrix B(array2[0],array2[1]);
                 cout<<"请给矩阵B赋值"<<endl;
                cin>>B;
                Matrix C(array2[2],array2[3]);
                cout<<"请给矩阵C赋值"<<endl;
                cin>>C;
                cout<<"矩阵B与矩阵C乘法\n";
                cout<<B*C;
             }
            }
        break;
        case 3:
            {
             int array3[4];
              cout<<"请输入矩阵的行数列数"<<endl;
             for (int k=0;k<4;++k)
                cin>>array3[k];
             if(array3[1]!=array3[3]||array3[0]!=array3[2])
              {
                  cout<<"两个矩阵不同形"<<"\n";
                  cout<<"请重启动再次输入"<<endl;
                }
                else
             {
                Matrix D(array3[0],array3[1]);
                 cout<<"请给矩阵D赋值"<<endl;
                cin>>D;
                Matrix E(array3[2],array3[3]);
                cout<<"请给矩阵E赋值"<<endl;
                cin>>E;
                cout<<"矩阵D与矩阵E点乘\n";
                cout<<D+E;
             }
            }
    }
    return 0;
}

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们