網站首頁 編程語言 正文
?程序的探索經過如下:
?①首先考慮到后續代碼的維護及其復用性,為此考慮用類實現
?②矩陣基本的類成員應該包括矩陣的行列數以及矩陣名以及初始化、輸入輸出
?③由于需要實現三種矩陣乘法運算,為此考慮利用運算符重載使用友元函數重載* +運算符分別實 ? 現矩陣的數乘、乘法以及點乘
源碼如下:
#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
相關推薦
- 2022-12-08 React競態條件Race?Condition實例詳解_React
- 2022-08-12 Android自定義彈出框的方法_Android
- 2024-01-16 Oracle的取整函數
- 2022-10-23 Redis?如何清空所有數據_Redis
- 2022-12-16 python實例方法的使用注意及代碼實例_python
- 2022-05-10 torch.cuda.is_available()返回false最終解決方案
- 2022-07-06 如何使用Python?OpenCV提取物體輪廓詳解_python
- 2022-05-22 C#裝箱和拆箱的原理介紹_C#教程
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支