首页 > 编程 > C++ > 正文

学习二维动态数组指针做矩阵运算的方法

2020-05-23 14:16:44
字体:大 中 小
来源:转载
供稿:网友

这片文章介绍了如何利用二维动态数组指针做矩阵运算,需要的朋友可以参考下

本文分享了利用二维动态数组指针做矩阵运算的实现代码。

1. 头文件

 

 
  1. // juzhen 2.cpp : Defines the entry point for the console application. 
  2. // 
  3.  
  4. #include "stdafx.h" 
  5. #include "stdlib.h" 
  6. #include "windows.h" 
  7. #define OK 0 
  8. #define NG -1 
  9. typedef struct mat 
  10. { 
  11. int nRow; /* 行数 */ 
  12. int nCol; /* 列数 */ 
  13. int* pData; /* 指向矩??体的指? */ 
  14. }MAT; 

2. 程序代码

 

 
  1. #include "stdafx.h" 
  2. #include "Matrix_cal.h" 
  3. /* Entity and initial matrix of the application matrix function */ 
  4. int MATAlloc(MAT *pMat, int nRow, int nCol) 
  5. { 
  6. pMat->pData = (int *) malloc (nRow * nCol * sizeof(int) ); 
  7. if(NULL == pMat->pData) 
  8. { 
  9. printf("Memary is error!/n"); 
  10. return NG; 
  11. } 
  12. for(int i=0; i<nRow; ++i) 
  13. { 
  14. for(int j=0; j<nCol; ++j) 
  15. { 
  16. *(pMat->pData + i*nCol + j)=0; 
  17. } 
  18. } 
  19. pMat->nRow = nRow; 
  20. pMat->nCol = nCol; 
  21. return OK; 
  22. } 
  23.  
  24. /* Release the memory space and reset the matrix data function */ 
  25. void MATFree(MAT* pMat) 
  26. { 
  27. free(pMat->pData); 
  28. pMat->pData = NULL; 
  29. pMat->nRow = 0; 
  30. pMat->nCol = 0; 
  31. } 
  32.  
  33. /* Import of matrix function */ 
  34. int MATAssign (MAT* pMat1, const MAT* pMat2) 
  35. { 
  36. MATAlloc(pMat1, pMat2->nRow, pMat2->nCol); 
  37. for(int i=0; i < pMat1->nRow; ++i) 
  38. { 
  39. for(int j=0; j < pMat1->nCol; ++j) 
  40. { 
  41. *(pMat1->pData + i * pMat1->nCol + j) = *(pMat2->pData + i * pMat1->nCol + j); 
  42. } 
  43. } 
  44. return OK;  
  45. } 
  46.  
  47. /* Matrix sum function */ 
  48. int MATAdd(const MAT* pMat1, const MAT* pMat2, MAT* pMat3) 
  49. { 
  50. MATAlloc(pMat3, pMat1->nRow, pMat1->nCol); 
  51. if((pMat1->nRow == pMat2->nRow) && (pMat1->nCol == pMat2->nCol)) 
  52. { 
  53. for(int i=0; i<pMat1->nRow; ++i) 
  54. { 
  55. for(int j=0; j<pMat1->nCol; ++j) 
  56. { 
  57. *(pMat3->pData + i * pMat3->nCol + j) = *(pMat1->pData + i * pMat1->nCol + j) + *(pMat2->pData + i * pMat1->nCol + j); 
  58. } 
  59. } 
  60. return OK;  
  61. } 
  62. else 
  63. { 
  64. printf("Not add!/n"); 
  65. return NG; 
  66. } 
  67.  
  68. } 
  69.  
  70. /* Matrix subtraction function */ 
  71. int MATSub(const MAT* pMat1, const MAT* pMat2, MAT* pMat3) 
  72. { 
  73. MATAlloc(pMat3, pMat1->nRow, pMat1->nCol); 
  74. if((pMat1->nRow == pMat2->nRow) && (pMat1->nCol == pMat2->nCol)) 
  75. { 
  76. for(int i=0; i<pMat1->nRow; ++i) 
  77. { 
  78. for(int j=0; j<pMat1->nCol; ++j) 
  79. { 
  80. *(pMat3->pData + i * pMat3->nCol + j) = *(pMat1->pData + i * pMat1->nCol + j) - *(pMat2->pData + i * pMat1->nCol + j); 
  81. } 
  82. } 
  83. return OK;  
  84. } 
  85. else 
  86. { 
  87. printf("Not Sub!/n"); 
  88. return NG; 
  89. } 
  90.  
  91. } 
  92.  
  93. /* Matrix clear */ 
  94. void MATClear(MAT* pMat) 
  95. { 
  96. for(int i=0; i<pMat->nRow; ++i) 
  97. { 
  98. for(int j=0; j<pMat->nCol; ++j) 
  99. { 
  100. *(pMat->pData + i * pMat->nCol + j)=0; 
  101. } 
  102. } 
  103. } 
  104.  
  105. /* Matrix multiplication C function */ 
  106. void MATMulC (MAT* pMat, int C) 
  107. { 
  108. for(int i=0; i<pMat->nRow; ++i) 
  109. { 
  110. for(int j=0; j<pMat->nCol; ++j) 
  111. { 
  112. *(pMat->pData + i * pMat->nCol + j) = C * (*(pMat->pData + i * pMat->nCol + j) ); 
  113. } 
  114. } 
  115. } 
  116.  
  117. /* Matrix multiplication function */ 
  118. int MATMul (const MAT* pMat1, const MAT* pMat2, MAT* pMat3) 
  119. { 
  120. MATAlloc(pMat3, pMat1->nRow, pMat2->nCol); 
  121. if(pMat1->nCol == pMat2->nRow) 
  122. { 
  123. for(int i=0; i<pMat1->nRow; ++i) 
  124. { 
  125. for(int j=0; j<pMat2->nCol; ++j) 
  126. { 
  127. for(int k=0; k<pMat1->nCol; ++k) 
  128. { 
  129. *(pMat3->pData + i * pMat2->nCol+j) += *(pMat1->pData + i * pMat2->nRow + k) * (*(pMat2->pData + k * pMat2->nCol + j) ); 
  130. } 
  131. } 
  132. } 
  133. return OK;  
  134. } 
  135. else 
  136. { 
  137. printf("not Mul/n"); 
  138. return NG; 
  139. } 
  140. } 
  141. /* Matrix transpose function */ 
  142. int MATTransport(const MAT* pMat1, MAT* pMat2) 
  143. { 
  144. MATAlloc(pMat2, pMat1->nCol, pMat1->nRow); 
  145. for(int i=0; i<pMat1->nRow; ++i) 
  146. { 
  147. for(int j=0; j<pMat1->nCol; ++j) 
  148. { 
  149. *(pMat2->pData + j * pMat1->nRow + i) = *(pMat1->pData + i * pMat1->nCol + j); 
  150. } 
  151. } 
  152. return OK; 
  153. } 
  154. /* 
  155. bool Check_digit(char *kk) 
  156. { 
  157. int a = strlen(kk); 
  158. for(int i = 0; i<a; ++i) 
  159. { 
  160. if( ( (int) (*(kk + i) ) > 48) && ( (int) (*(kk + i) ) < 57 || (int) (*(kk + i) ) == 32) ) 
  161. { 
  162. return 1; 
  163. } 
  164. } 
  165. return 0; 
  166. } 
  167.  
  168. */ 
  169.  
  170. /* Matrix initialization */ 
  171. void MATinit(MAT *pMat) 
  172. { 
  173. bool kos=1; 
  174. int nRow = 0, nCol = 0; 
  175. printf("Please input the number of rows: "); 
  176. scanf_s("%d",&nRow); 
  177. putchar('/n'); 
  178. printf("Please input the number of columns: "); 
  179. scanf_s("%d",&nCol); 
  180. putchar('/n'); 
  181. printf("Please input %dX%d Matrix:/n",nRow,nCol); 
  182. kos=MATAlloc(pMat,nRow,nCol); 
  183. for(int i=0; i<nRow; ++i) 
  184. { 
  185. for(int j=0; j<nCol; ++j) 
  186. { 
  187. scanf("%d", pMat->pData + i*nCol + j); 
  188. } 
  189. } 
  190. } 
  191. /*char arr[100][100]={0}; 
  192. for(int i=0; i<nRow; ++i) 
  193. { 
  194. for(int j=0; j<nCol; ++j) 
  195. { 
  196. scanf("%c", &arr[i][j]); 
  197. kos = Check_digit(&arr[i][j]); 
  198. } 
  199. } 
  200. //ks= atoi(arr[0]); 
  201. while(kos) 
  202. { 
  203. printf(" input is error,Please input again!"); 
  204. for(int i=0; i<nRow; ++i) 
  205. { 
  206. for(int j=0; j<nCol; ++j) 
  207. { 
  208. scanf("%c", arr[i]); 
  209. } 
  210. } 
  211. kos = Check_digit(arr[0]); 
  212. //ks= atoi(arr[0]); 
  213. } 
  214. for(int i=0; i<nRow; ++i) 
  215. { 
  216. for(int j=0; j<nCol; ++j) 
  217. { 
  218. *(pMat->pData + i*nCol + j) = atoi(&arr[i][j]); 
  219. } 
  220. } 
  221.  
  222. } 
  223. */ 
  224.  
  225. /* Output matrix */ 
  226. void Print(MAT *pMat) 
  227. { 
  228. printf("The result is:/n"); 
  229. for(int i = 0; i < pMat->nRow; ++i) 
  230. { 
  231. for(int j=0; j<pMat->nCol; ++j) 
  232. { 
  233. printf("%d ",*( pMat->pData + i * pMat->nCol + j) ); 
  234. } 
  235. putchar('/n'); 
  236. } 
  237. } 
  238.  
  239. int _tmain(int argc, _TCHAR* argv[]) 
  240. { 
  241. int nRow = 1,nCol = 1,sign = 1,C = 1,work = 1,sigal=0; 
  242. MAT Mat, Mat1, Mat2; 
  243. MAT *pMat = &Mat; 
  244. MAT *pMat1 = &Mat1; 
  245. MAT *pMat2 = &Mat2; 
  246. while(work) 
  247. { 
  248. system("cls"); 
  249. printf(" Welcome To The Matrix Operation system! /n"); 
  250. printf("------------------------------------------------/n"); 
  251. printf("1: Open The Generating matrix function!/n"); 
  252. printf("2: Open The Release matrix function!/n"); 
  253. printf("3: Open The Import matrix function!/n"); 
  254. printf("4: Open The Add matrix function!/n"); 
  255. printf("5: Open The Matrix subtraction function!/n"); 
  256. printf("6: Open The Clear matrix function!/n"); 
  257. printf("7: Open The Matrix multiplication C function!/n"); 
  258. printf("8: Open The Matrix multiplication function!/n"); 
  259. printf("9: Open The Matrix transpose function!/n"); 
  260. printf("------------------------------------------------/n"); 
  261. printf("Please Select operation type:"); 
  262. scanf("%d",&sign); 
  263. switch(sign) 
  264. { 
  265. case 1: 
  266. { 
  267. MATinit(pMat); 
  268. Print(pMat); 
  269. } 
  270. break; 
  271. case 2: 
  272. { 
  273. MATinit(pMat); 
  274. Print(pMat); 
  275. MATFree(pMat); 
  276. } 
  277. break; 
  278. case 3: 
  279. { 
  280.  
  281. MATinit(pMat2); 
  282. MATAssign (pMat1, pMat2); 
  283. Print(pMat1); 
  284. } 
  285. break; 
  286. case 4: 
  287. { 
  288. MATinit(pMat1); 
  289. MATinit(pMat2); 
  290. sigal = MATAdd(pMat1, pMat2,pMat); 
  291. if(0 == sigal) 
  292. { 
  293. Print(pMat); 
  294. } 
  295. } 
  296. break; 
  297. case 5: 
  298. { 
  299. MATinit(pMat1); 
  300. MATinit(pMat2); 
  301. sigal = MATSub(pMat1, pMat2,pMat); 
  302. if(0 == sigal) 
  303. { 
  304. Print(pMat); 
  305. } 
  306. } 
  307. break; 
  308. case 6: 
  309. { 
  310. MATinit(pMat); 
  311. Print(pMat); 
  312. MATClear(pMat); 
  313. Print(pMat); 
  314. } 
  315. break; 
  316. case 7: 
  317. { 
  318. printf("Please input the number of C: "); 
  319. scanf("%d",&C); 
  320. putchar('/n'); 
  321. MATinit(pMat); 
  322. MATMulC (pMat, C); 
  323. Print(pMat); 
  324. } 
  325. break; 
  326. case 8: 
  327. { 
  328. MATinit(pMat1); 
  329. MATinit(pMat2); 
  330. sigal = MATMul (pMat1, pMat2, pMat); 
  331. if(0 == sigal) 
  332. { 
  333. Print(pMat); 
  334. } 
  335. } 
  336. break; 
  337. case 9: 
  338. { 
  339. MATinit(pMat1); 
  340. MATTransport(pMat1, pMat2); 
  341. Print(pMat2); 
  342. } 
  343. break; 
  344. default: printf("input is error!"); 
  345. } 
  346. printf("Whether exit the Matrix calculation system?(1 is not exit,0 is exit)/n"); //whether exit the system. 
  347. scanf("%d", &work); 
  348. fflush(stdin); 
  349. while (work != 0 && work != 1) //work must is 1 or 0. 
  350. { 
  351. printf(" Input is error,Please input again!/n"); 
  352. scanf("%d", &work); 
  353. fflush(stdin); 
  354. } 
  355. } 
  356. printf("/n-------------Thanks For You Using The Matrix Calculation System !--------------/n"); 
  357. Sleep(2000); //deley some times. 
  358. return 0; 
  359. } 

以上就是实现二维动态数组指针做矩阵运算的代码,希望对大家的学习有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表