? ? ----------------------------------矩陣的數(shù)乘、乘法、點(diǎn)乘---------------------------------------*
? ?*-description:用戶(hù)選擇運(yùn)算方式后輸入矩陣的規(guī)模并賦值,隨即進(jìn)行矩陣相關(guān)運(yùn)算??
? ?*- author:Luo? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
? ?*-e-mail:2459871931@qq.com? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ?*- Version ? ?:Dev-C++ 5.11? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
? ?*--------------------------------------------------------------------------------------------------------*
?小的第一次在CSDN論壇發(fā)文- -
?當(dāng)是簡(jiǎn)單記錄生活吧!有什么問(wèn)題一起交流探討!多指點(diǎn)指點(diǎn)俺!
?程序的探索經(jīng)過(guò)如下:
?①首先考慮到后續(xù)代碼的維護(hù)及其復(fù)用性,為此考慮用類(lèi)實(shí)現(xiàn)
?②矩陣基本的類(lèi)成員應(yīng)該包括矩陣的行列數(shù)以及矩陣名以及初始化、輸入輸出
?③由于需要實(shí)現(xiàn)三種矩陣乘法運(yùn)算,為此考慮利用運(yùn)算符重載使用友元函數(shù)重載* +運(yùn)算符分別實(shí)? ?現(xiàn)矩陣的數(shù)乘、乘法以及點(diǎn)乘
?源碼如下:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-461629.html
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long ll;
class Matrix ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//封裝一個(gè)矩陣類(lèi)便于后續(xù)使用
{
? ? int row;
? ? int col; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
? ? int** base; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //二維數(shù)組存放矩陣
public:
? ? Matrix(int r,int c) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //構(gòu)造函數(shù)初始化
? ? {
? ? ? ? row = r;
? ? ? ? col = c;
? ? ? ? create();
? ? }
? ? //在外部則寫(xiě)作:
?? ?//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) ? ? ? ? ? ? ? ?//重載等于號(hào)實(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 ? ? ? ? ? ? ? ? ? ? ? //重載[]以方便用"變量名[][]" 的形式讀寫(xiě)矩陣內(nèi)容
? ? {
? ? ? ? return base[i];
? ? }
? ? int getrow()const ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //對(duì)外提供矩陣信息
? ? {
? ? ? ? return row;
? ? }
? ? int getcol()const ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//對(duì)外提供矩陣信息
? ? {
? ? ? ? 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)建一個(gè)二維數(shù)組
? ? {
? ? ? ? base = new int*[row];
? ? ? ? for(int i = 0;i<row;i++)
? ? ? ? {
? ? ? ? ? ? base[i] = new int[col];
? ? ? ? }
? ? }
? ? void destroy() ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //釋放一個(gè)二維數(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為同形矩陣 取誰(shuí)的行列數(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<<"請(qǐng)輸入要進(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<<"請(qǐng)輸入矩陣的行數(shù)列數(shù)"<<endl;
?? ? ? ? ? ? for (int i=0;i<2;++i)
?? ? ? ? ? ? ? ?cin>>array1[i];
? ? ?? ??? ? int num;
? ? ?? ??? ? Matrix A(array1[0],array1[1]);
?? ? ? ? ? ? cout<<"請(qǐng)輸入乘數(shù)"<<endl;
? ? ? ? ? ? ?cin>>num;
? ? ? ? ? ? ?cout<<"請(qǐng)給矩陣A賦值"<<endl;
? ? ? ? ? ? ?cin>>A;
? ? ? ? ? ? ?cout<<"num與A數(shù)乘\n";?
? ? ? ? ? ? ?cout<<num*A;
? ? ? ? ? ? }
? ? ? ? break;
? ? ? ? case 2:?
? ? ? ? ? ? {
?? ??? ??? ? int array2[4];
? ? ? ? ? ? ?cout<<"請(qǐng)輸入矩陣的行數(shù)列數(shù)"<<endl;
?? ? ? ? ? ? for (int j=0;j<4;++j)
?? ? ? ? ? ? ? ?cin>>array2[j];
?? ??? ? ? ? if(array2[1]!=array2[2])
?? ? ?? ??? ? {
?? ? ??? ??? ??? ?cout<<"第一個(gè)矩陣列數(shù)不等于第二個(gè)矩陣行數(shù)"<<"\n";
? ? ? ?? ??? ??? ?cout<<"請(qǐng)重啟動(dòng)再次輸入"<<endl;
?? ? ? ?? ??? ? }
?? ? ? ?? ??? ? else
?? ??? ??? ? {
?? ??? ??? ??? ?Matrix B(array2[0],array2[1]);
?? ??? ??? ? ?? ?cout<<"請(qǐng)給矩陣B賦值"<<endl;
?? ??? ??? ? ? ?cin>>B;
?? ??? ??? ??? ?Matrix C(array2[2],array2[3]);
?? ??? ??? ??? ?cout<<"請(qǐng)給矩陣C賦值"<<endl;
?? ??? ??? ??? ?cin>>C;
?? ??? ??? ??? ?cout<<"矩陣B與矩陣C乘法\n";
?? ??? ??? ??? ?cout<<B*C;
?? ??? ??? ? }
?? ??? ? ? ?}
?? ??? ?break;
?? ??? ?case 3:
?? ??? ??? ?{
?? ??? ??? ? int array3[4];
? ? ? ? ? ? ? cout<<"請(qǐng)輸入矩陣的行數(shù)列數(shù)"<<endl;
?? ? ? ? ? ? for (int k=0;k<4;++k)
?? ? ? ? ? ? ? ?cin>>array3[k];
?? ??? ? ? ? if(array3[1]!=array3[3]||array3[0]!=array3[2])
?? ? ?? ??? ? {
?? ? ??? ??? ??? ?cout<<"兩個(gè)矩陣不同形"<<"\n";
? ? ? ?? ??? ??? ?cout<<"請(qǐng)重啟動(dòng)再次輸入"<<endl;
?? ? ? ?? ??? ? }
?? ? ? ?? ??? ? else
?? ??? ??? ? {
?? ??? ??? ??? ?Matrix D(array3[0],array3[1]);
?? ??? ??? ? ?? ?cout<<"請(qǐng)給矩陣D賦值"<<endl;
?? ??? ??? ? ? ?cin>>D;
?? ??? ??? ??? ?Matrix E(array3[2],array3[3]);
?? ??? ??? ??? ?cout<<"請(qǐng)給矩陣E賦值"<<endl;
?? ??? ??? ??? ?cin>>E;
?? ??? ??? ??? ?cout<<"矩陣D與矩陣E點(diǎn)乘\n";
?? ??? ??? ??? ?cout<<D+E;
?? ??? ??? ? }
?? ??? ? ? ?}
?? ?}
?? ?return 0;
}
?以上是源碼啦!喜歡的話(huà)可以點(diǎn)贊收藏,感謝大家!文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-461629.html
到了這里,關(guān)于C++利用類(lèi)實(shí)現(xiàn)矩陣的數(shù)乘、乘法以及點(diǎn)乘的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!