C++利用類實(shí)現(xiàn)矩陣的數(shù)乘,乘法以及點(diǎn)乘
程序的探索經(jīng)過如下:
①首先考慮到后續(xù)代碼的維護(hù)及其復(fù)用性,為此考慮用類實(shí)現(xiàn)
②矩陣基本的類成員應(yīng)該包括矩陣的行列數(shù)以及矩陣名以及初始化、輸入輸出
③由于需要實(shí)現(xiàn)三種矩陣乘法運(yùn)算,為此考慮利用運(yùn)算符重載使用友元函數(shù)重載* +運(yùn)算符分別實(shí) 現(xiàn)矩陣的數(shù)乘、乘法以及點(diǎn)乘
源碼如下:
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long ll;
class Matrix //封裝一個矩陣類便于后續(xù)使用
{
int row;
int col;
int** base; //二維數(shù)組存放矩陣
public:
Matrix(int r,int c) //構(gòu)造函數(shù)初始化
{
row = r;
col = c;
create();
}
//在外部則寫作:
//Matrix::Matrix(int r, int c)
//{
// row=r; col=c; create();
//}
Matrix(const Matrix& e) //拷貝構(gòu)造函數(shù)
{
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() //析構(gòu)函數(shù)防止內(nèi)存泄露
{
destroy();
}
Matrix& operator=(const Matrix& e) //重載等于號實(shí)現(xiàn)深拷貝
{
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 //重載[]以方便用"變量名[][]" 的形式讀寫矩陣內(nèi)容
{
return base[i];
}
int getrow()const //對外提供矩陣信息
{
return row;
}
int getcol()const //對外提供矩陣信息
{
return col;
}
friend Matrix operator*(int n,Matrix &e); //重載 * 實(shí)現(xiàn)數(shù)乘
friend Matrix operator*(Matrix& e,Matrix& f); //重載 * 實(shí)現(xiàn)矩陣乘法
friend Matrix operator+(Matrix& e,Matrix& f); //重載 + 實(shí)現(xiàn)矩陣點(diǎn)乘
private:
void create() //創(chuàng)建一個二維數(shù)組
{
base = new int*[row];
for(int i = 0;i<row;i++)
{
base[i] = new int[col];
}
}
void destroy() //釋放一個二維數(shù)組
{
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) //重載 * 實(shí)現(xiàn)數(shù)乘
{
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) //重載 * 實(shí)現(xiàn)矩陣乘法
{
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) //重載 + 實(shí)現(xiàn)矩陣點(diǎn)乘
{
Matrix ret(e.getrow(),e.getcol()); //矩陣 e 與矩陣 f為同形矩陣 取誰的行列數(shù)都一樣
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<<"請輸入要進(jìn)行的運(yùn)算(包括數(shù)乘、乘法以及點(diǎn)乘)"<<endl;
cout<<"數(shù)乘 1 乘法 2 點(diǎn)乘3 "<<endl;
int choice;
cin>>choice;
switch (choice)
{
case 1:
{
int array1[2];
cout<<"請輸入矩陣的行數(shù)列數(shù)"<<endl;
for (int i=0;i<2;++i)
cin>>array1[i];
int num;
Matrix A(array1[0],array1[1]);
cout<<"請輸入乘數(shù)"<<endl;
cin>>num;
cout<<"請給矩陣A賦值"<<endl;
cin>>A;
cout<<"num與A數(shù)乘\n";
cout<<num*A;
}
break;
case 2:
{
int array2[4];
cout<<"請輸入矩陣的行數(shù)列數(shù)"<<endl;
for (int j=0;j<4;++j)
cin>>array2[j];
if(array2[1]!=array2[2])
{
cout<<"第一個矩陣列數(shù)不等于第二個矩陣行數(shù)"<<"\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<<"請輸入矩陣的行數(shù)列數(shù)"<<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點(diǎn)乘\n";
cout<<D+E;
}
}
}
return 0;
}到此這篇關(guān)于C++利用類實(shí)現(xiàn)矩陣的數(shù)乘,乘法以及點(diǎn)乘的文章就介紹到這了,更多相關(guān)C++矩陣數(shù)乘 點(diǎn)乘內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語言中利用封裝好的函數(shù)實(shí)現(xiàn)英文字母的大小寫轉(zhuǎn)換
這篇文章主要介紹了C語言中利用封裝好的函數(shù)實(shí)現(xiàn)英文字母的大小寫轉(zhuǎn)換,需要的朋友可以參考下2017-10-10
C/C++?QT實(shí)現(xiàn)自定義對話框的示例代碼
對話框分為多種,常見的有通用對話框,自定義對話框,模態(tài)對話框,非模態(tài)對話框等,本文主要介紹了QT自定義對話框,感興趣的可以了解一下2021-11-11

