日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

C++利用類實現矩陣的數乘,乘法以及點乘_C 語言

作者:cc? ? 更新時間: 2022-12-14 編程語言

?程序的探索經過如下:

?①首先考慮到后續代碼的維護及其復用性,為此考慮用類實現

?②矩陣基本的類成員應該包括矩陣的行列數以及矩陣名以及初始化、輸入輸出

?③由于需要實現三種矩陣乘法運算,為此考慮利用運算符重載使用友元函數重載* +運算符分別實 ? 現矩陣的數乘、乘法以及點乘

源碼如下:

#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;
}

原文鏈接:https://blog.csdn.net/m0_68791333/article/details/127865927

欄目分類
最近更新