矩陣運(yùn)算——C語(yǔ)言實(shí)現(xiàn)_第1頁(yè)
矩陣運(yùn)算——C語(yǔ)言實(shí)現(xiàn)_第2頁(yè)
矩陣運(yùn)算——C語(yǔ)言實(shí)現(xiàn)_第3頁(yè)
矩陣運(yùn)算——C語(yǔ)言實(shí)現(xiàn)_第4頁(yè)
矩陣運(yùn)算——C語(yǔ)言實(shí)現(xiàn)_第5頁(yè)
已閱讀5頁(yè),還剩8頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、#include stdafx.h#include /結(jié)構(gòu)體typedef structdouble * mat;int row;int col;Matrix;void InitialMatrix(Matrix *T, int row,int col); /只分配空間不初始化;void InitialMatrixZero(Matrix *T,int row, int col); /初始化為0void InitialMatrixRand(Matrix *T,int row, int col); /初始化為50以?xún)?nèi)隨機(jī)正整數(shù)void InputMatrix(Matrix *T);/鍵盤(pán)輸入矩陣vo

2、id DestroyMatrix(Matrix *T);/ 釋放矩陣空間void PrintfMatrix(Matrix *T);/矩陣輸出int AddMatrix(Matrix *A,Matrix *B,Matrix *C); / 矩陣加int MinusMatrix(Matrix *A,Matrix *B,Matrix *C); / 矩陣減int MultiMatrix(Matrix *A,Matrix *B,Matrix *C); /矩陣乘法double MeanMatrix(Matrix *T); /矩陣元素均值int SubMatrix(Matrix *T1,Matrix *T2,

3、int BeginRow,int BeginCol,int EndRow,int EndCol); /求T1的子矩陣T2;測(cè)試程序參考界面如下所示: 矩陣函數(shù)測(cè)試,請(qǐng)選擇功能,輸入對(duì)應(yīng)的數(shù)字: * 1:輸入一個(gè)矩陣,求矩陣均值; 2:產(chǎn)生一個(gè)隨機(jī)數(shù)矩陣,求矩陣均值; 3:輸入兩個(gè)個(gè)矩陣,求矩陣和; 4:輸入兩個(gè)個(gè)矩陣,求矩陣差; 5:輸入兩個(gè)個(gè)矩陣,求矩陣積; 6:產(chǎn)生兩個(gè)個(gè)隨機(jī)數(shù)矩陣,求矩陣和; 7:產(chǎn)生兩個(gè)個(gè)隨機(jī)數(shù)矩陣,求矩陣差; 8:產(chǎn)生兩個(gè)個(gè)隨機(jī)數(shù)矩陣,求矩陣積; 9:求矩陣的子陣,如矩陣的2-4行,1-3列的子陣; 0:結(jié)束!#include #include /結(jié)構(gòu)體typedef

4、 struct double *mat;int row;int col; Matrix;/函數(shù)聲明void InitialMatrix(Matrix *T, int row, int col);void InitialMatrixZero(Matrix *T, int row, int col);void InitialMatrixRand(Matrix *T, int row, int col);void InputMatrix(Matrix *T);void DestroyMatrix(Matrix *T);/ 釋放矩陣空間void PrintfMatrix(Matrix *T);/矩陣輸

5、出int AddMatrix(Matrix *A, Matrix *B, Matrix *C); / 矩陣加int MinusMatrix(Matrix *A, Matrix *B, Matrix *C); / 矩陣減int MultiMatrix(Matrix *A, Matrix *B, Matrix *C); /矩陣乘法double MeanMatrix(Matrix *T); /矩陣元素均值/int SubMatrix(Matrix *T1, Matrix *T2, int BeginRow, int BeginCol, int EndRow, int EndCol); /求T1的子矩

6、陣T2;void NMatrix(void);/求逆矩陣 /主函數(shù)int main()int inputevent;/int i, j;int row, col;Matrix T;Matrix A;Matrix B;Matrix C;printf( 矩陣函數(shù)測(cè)試,請(qǐng)選擇功能,輸入對(duì)應(yīng)的數(shù)字:n);printf( *nn);printf(1:輸入一個(gè)矩陣,求矩陣均值;n);printf(2:產(chǎn)生一個(gè)隨機(jī)數(shù)矩陣,求矩陣均值;n);printf(3:輸入兩個(gè)個(gè)矩陣,求矩陣和;n);printf(4:輸入兩個(gè)個(gè)矩陣,求矩陣差;n);printf(5:輸入兩個(gè)矩陣,求矩陣積;);printf(n6:產(chǎn)生

7、兩個(gè)隨機(jī)數(shù)矩陣,求矩陣和;n);printf(7:產(chǎn)生兩個(gè)隨機(jī)數(shù)矩陣,求矩陣差;n);printf(8:產(chǎn)生兩個(gè)隨機(jī)數(shù)矩陣,求矩陣積;n);printf(9:求矩陣的子陣,如矩陣的2-4行D,1-3列的子陣;n);printf(10:輸入一個(gè)方陣,求其逆矩陣n);printf(0:結(jié)束!n);printf(nn選擇:);scanf(%d, &inputevent);while (inputevent != 0)if (inputevent = 1) printf(您要輸入的矩陣的行數(shù)和列數(shù)e.g:5,6:);scanf(%d,%d, &row, &col);InitialMatrix(&T,

8、row, col);InitialMatrixZero(&T, T.row, T.col);InputMatrix(&T);printf(n矩陣的平均值為:n%lfnn, MeanMatrix(&T);DestroyMatrix(&T);if (inputevent = 2) printf(輸入您要產(chǎn)生的矩陣的行數(shù)和列數(shù)e.g:5,6:);scanf(%d,%d, &row, &col);InitialMatrix(&T, row, col);InitialMatrixRand(&T, row, col);MeanMatrix(&T);PrintfMatrix(&T);printf(n矩陣的平

9、均值為:n%lfnn, MeanMatrix(&T);DestroyMatrix(&T);if (inputevent = 3) printf(您要輸入的矩陣A的行數(shù)和列數(shù)e.g:5,6:);scanf(%d,%d, &row, &col);InitialMatrix(&A, row, col);InitialMatrixZero(&A, A.row, A.col);InputMatrix(&A);printf(您要輸入的矩陣B的行數(shù)和列數(shù)e.g:5,6:);scanf(%d,%d, &row, &col);InitialMatrix(&B, row, col);InitialMatrixZe

10、ro(&B, B.row, B.col);InputMatrix(&B);InitialMatrix(&C, B.row, B.col);InitialMatrixZero(&C, C.row, C.col);if (AddMatrix(&A, &B, &C) = 1) printf(nn矩陣的和為:A*Bnn);PrintfMatrix(&C);else printf(nn無(wú)法計(jì)算!nn);DestroyMatrix(&A);DestroyMatrix(&B);DestroyMatrix(&C);if (inputevent = 4) printf(您要輸入的矩陣A的行數(shù)和列數(shù)e.g:5,6

11、:);scanf(%d,%d, &row, &col);InitialMatrix(&A, row, col);InitialMatrixZero(&A, A.row, A.col);InputMatrix(&A);printf(您要輸入的矩陣B的行數(shù)和列數(shù)e.g:5,6:);scanf(%d,%d, &row, &col);InitialMatrix(&B, row, col);InitialMatrixZero(&B, B.row, B.col);InputMatrix(&B);InitialMatrix(&C, B.row, B.col);InitialMatrixZero(&C, C.

12、row, C.col);if (MinusMatrix(&A, &B, &C) = 1) printf(n矩陣的差為:A-B=nn);PrintfMatrix(&C);else printf(nn無(wú)法計(jì)算!nn);DestroyMatrix(&A);DestroyMatrix(&B);DestroyMatrix(&C);if (inputevent = 5) int able;printf(您要輸入的矩陣A的行數(shù)和列數(shù)e.g: 5,6: );scanf(%d,%d, &row, &col);InitialMatrix(&A, row, col);InitialMatrixZero(&A, A.

13、row, A.col);InputMatrix(&A);printf(您要輸入的矩陣B的行數(shù)和列數(shù)e.g:5,6: );scanf(%d,%d, &row, &col);InitialMatrix(&B, row, col);InitialMatrixZero(&B, B.row, B.col);InputMatrix(&B);InitialMatrix(&C, A.row, B.col);InitialMatrixZero(&C, C.row, C.col);able = MultiMatrix(&A, &B, &C);if (able = 1) printf(n積為:A*Bnn);Prin

14、tfMatrix(&C);else printf(nn無(wú)法計(jì)算!nn);DestroyMatrix(&A);DestroyMatrix(&B);DestroyMatrix(&C);if (inputevent = 6) printf(您要產(chǎn)生的矩陣A的行數(shù)和列數(shù)e.g:5,6: );scanf(%d,%d, &row, &col);InitialMatrix(&A, row, col);InitialMatrixRand(&A, row, col);printf(A為:nn);PrintfMatrix(&A);printf(您要產(chǎn)生的矩陣B的行數(shù)和列數(shù)e.g:5,6: );scanf(%d,%

15、d, &row, &col);InitialMatrix(&B, row, col);InitialMatrixRand(&B, row, col);printf(B為:nn);PrintfMatrix(&B);InitialMatrix(&C, A.row, A.col);InitialMatrixZero(&C, C.row, C.col);if (AddMatrix(&A, &B, &C) = 1) printf(n矩陣的和為:A+B=nn);PrintfMatrix(&C);else printf(nn無(wú)法計(jì)算!nn);DestroyMatrix(&A);DestroyMatrix(&

16、B);DestroyMatrix(&C);if (inputevent = 7) printf(您要產(chǎn)生的矩陣A的行數(shù)和列數(shù)e.g:5,6: );scanf(%d,%d, &row, &col);InitialMatrix(&A, row, col);InitialMatrixRand(&A, row, col);printf(您要產(chǎn)生的矩陣B的行數(shù)和列數(shù)e.g:5,6: );scanf(%d,%d, &row, &col);InitialMatrix(&B, row, col);InitialMatrixRand(&B, row, col);InitialMatrix(&C, A.row,

17、A.col);InitialMatrixZero(&C, C.row, C.col);if (MinusMatrix(&A, &B, &C) = 1) printf(n矩陣的差為:A-B=nn);PrintfMatrix(&C);else printf(nn無(wú)法計(jì)算!nn);DestroyMatrix(&A);DestroyMatrix(&B);DestroyMatrix(&C);if (inputevent = 8) printf(您要產(chǎn)生的矩陣A的行數(shù)和列數(shù)e.g:5,6: n);scanf(%d,%d, &row, &col);InitialMatrix(&A, row, col);In

18、itialMatrixRand(&A, row, col);printf(A為:nn);PrintfMatrix(&A);printf(您要產(chǎn)生的矩陣B的行數(shù)和列數(shù)e.g:5,6: n);scanf(%d,%d, &row, &col);InitialMatrix(&B, row, col);InitialMatrixRand(&B, row, col);printf(B為:nn);PrintfMatrix(&B);InitialMatrix(&C, A.row, A.col);InitialMatrixZero(&C, C.row, C.col);if (MultiMatrix(&A, &B

19、, &C) = 1) printf(n積為:A*B=nn);PrintfMatrix(&C);else printf(nn無(wú)法計(jì)算;nn);DestroyMatrix(&A);DestroyMatrix(&B);DestroyMatrix(&C);if (inputevent = 9) printf(對(duì)不起,該函數(shù)尚在完善中nn);if (inputevent = 10) NMatrix();if (inputevent = 0)break;printf( 矩陣函數(shù)測(cè)試,請(qǐng)選擇功能,輸入對(duì)應(yīng)的數(shù)字:n);printf( *nn);printf(1:輸入一個(gè)矩陣,求矩陣均值;n);printf(

20、2:產(chǎn)生一個(gè)隨機(jī)數(shù)矩陣,求矩陣均值;n);printf(3:輸入兩個(gè)個(gè)矩陣,求矩陣和;n);printf(4:輸入兩個(gè)個(gè)矩陣,求矩陣差;n);printf(5:輸入兩個(gè)矩陣,求矩陣積;);printf(n6:產(chǎn)生兩個(gè)隨機(jī)數(shù)矩陣,求矩陣和;n);printf(7:產(chǎn)生兩個(gè)隨機(jī)數(shù)矩陣,求矩陣差;n);printf(8:產(chǎn)生兩個(gè)隨機(jī)數(shù)矩陣,求矩陣積;n);printf(9:求矩陣的子陣,如矩陣的2-4行D,1-3列的子陣;n);printf(10:輸入一個(gè)方陣,求其逆矩陣n);printf(0:結(jié)束!n);printf(nn選擇:);scanf(%d, &inputevent);return 0;/

21、其他函數(shù)void InitialMatrix(Matrix *T, int row, int col)/printf(分配內(nèi)存中.n);int i;int succ = 1;/T=(Matrix *)malloc(sizeof(Matrix);T-row = row;T-col = col;T-mat = (double *)malloc(T-row * sizeof(double *);if (T-mat = NULL) succ = 0;else for (i = 0; i row; i+) T-mati = (double *)malloc(T-col * sizeof(double);

22、if (T-mati = NULL) succ = 0;break;/if(succ=1)/ printf(內(nèi)存分配成功|;?n);/else printf(內(nèi)存分配失敗;n);void InitialMatrixZero(Matrix *T, int row, int col) /printf(矩陣初始化為零中.n);int i, j;for (i = 0; i row; i+)for (j = 0; j matij = 0;/printf(矩陣初始化為零矩陣成功;n);void InitialMatrixRand(Matrix *T, int row, int col) int i, j;

23、for (i = 0; i row; i+)for (j = 0; j col; j+)(*T).matij = rand() % 50;void InputMatrix(Matrix *T) printf(輸入矩陣:n);int i, j;for (i = 0; i (*T).row; i+)for (j = 0; j (*T).col; j+)scanf(%lf, &(*T).matij);void DestroyMatrix(Matrix *T)int i;for (i = 0; i (*T).row; i+)free(*T).mati);void PrintfMatrix(Matrix

24、 *T)int i, j;for (i = 0; i (*T).row; i+)for (j = 0; j (*T).col; j+)printf(%lf , (*T).matij);printf(n);int AddMatrix(Matrix *A, Matrix *B, Matrix *C)int i, j;if (*A).row = (*B).row & (*A).col = (*B).col)for (i = 0; i (*A).row; i+)for (j = 0; j (*A).col; j+)(*C).matij = (*A).matij + (*B).matij;for (i

25、= 0; i (*A).row; i+)for (j = 0; j (*A).col; j+)return 1;else printf(這兩個(gè)矩陣不能相加!n);return 0;int MinusMatrix(Matrix *A, Matrix *B, Matrix *C)int i, j;if (*A).row = (*B).row & (*A).col = (*B).col)for (i = 0; i (*A).row; i+)for (j = 0; j (*A).col; j+)(*C).matij = (*A).matij - (*B).matij;return 1;elseprin

26、tf(這兩個(gè)矩陣不能相減!n);return 0;int MultiMatrix(Matrix *A, Matrix *B, Matrix *C)int i=0, j=0;int k = 0;if (*A).col = (*B).row)for (i = 0; i (*A).row; i+) for (j = 0; j (*B).col; j+)for(k=0;k col);k+)(*C).matij += (*A).matik * (*B).matkj;return 1; elseprintf(這兩個(gè)矩陣不能相乘!n);return 0;double MeanMatrix(Matrix *T) /矩陣元素均值double mean;double su

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論