InitialMatrixRand(&A, row, col); printf(\为:\\n\\n\PrintfMatrix(&A);
printf(\您要产生的矩阵B的行数和列数e.g:5,6: \ scanf(\ InitialMatrix(&B, row, col);
InitialMatrixRand(&B, row, col); printf(\为:\\n\\n\ PrintfMatrix(&B);
InitialMatrix(&C, A.row, A.col); InitialMatrixZero(&C, C.row, C.col); if (AddMatrix(&A, &B, &C) == 1) { printf(\矩阵的和为:A+B=\\n\\n\ PrintfMatrix(&C); }
else printf(\无法计算!\\n\\n\
DestroyMatrix(&A); DestroyMatrix(&B); DestroyMatrix(&C); }
if (inputevent == 7) { printf(\您要产生的矩阵A的行数和列数e.g:5,6: scanf(\ InitialMatrix(&A, row, col);
InitialMatrixRand(&A, row, col);
printf(\您要产生的矩阵B的行数和列数e.g:5,6: scanf(\ InitialMatrix(&B, row, col);
InitialMatrixRand(&B, row, col); InitialMatrix(&C, A.row, A.col); InitialMatrixZero(&C, C.row, C.col); if (MinusMatrix(&A, &B, &C) == 1) { printf(\矩阵的差为:A-B=\\n\\n\ PrintfMatrix(&C); }
else printf(\无法计算!\\n\\n\
6 / 13
\\
}
DestroyMatrix(&A); DestroyMatrix(&B); DestroyMatrix(&C);
if (inputevent == 8) { printf(\您要产生的矩阵A的行数和列数e.g:5,6: \\n\ scanf(\ InitialMatrix(&A, row, col); InitialMatrixRand(&A, row, col); }
printf(\为:\\n\\n\PrintfMatrix(&A);
printf(\您要产生的矩阵B的行数和列数e.g:5,6: \\n\scanf(\InitialMatrix(&B, row, col);
InitialMatrixRand(&B, row, col); printf(\为:\\n\\n\PrintfMatrix(&B);
InitialMatrix(&C, A.row, A.col); InitialMatrixZero(&C, C.row, C.col); if (MultiMatrix(&A, &B, &C) == 1) { }
printf(\积为:A*B=\\n\\n\PrintfMatrix(&C);;
else printf(\无法计算;\\n\\n\DestroyMatrix(&A); DestroyMatrix(&B); DestroyMatrix(&C);
if (inputevent == 9) printf(\对不起,该函数尚在完善中\\n\\n\if (inputevent == 10) NMatrix(); if (inputevent == 0) break;
printf(\ 矩阵函数测试,请选择功能,输入对应的数字:\\n\
printf(\printf(\:输入一个矩阵,求矩阵均值;\\n\
7 / 13
printf(\:产生一个随机数矩阵,求矩阵均值;\\n\ printf(\:输入两个个矩阵,求矩阵和;\\n\ printf(\:输入两个个矩阵,求矩阵差;\\n\ printf(\:输入两个矩阵,求矩阵积;\ printf(\:产生两个随机数矩阵,求矩阵和;\\n\ printf(\:产生两个随机数矩阵,求矩阵差;\\n\ printf(\:产生两个随机数矩阵,求矩阵积;\\n\ printf(\求矩阵的子阵,如矩阵的2-4行D,1-3列的子阵;\\n\ printf(\:输入一个方阵,求其逆矩阵\\n\ printf(\结束!\\n\ printf(\选择:\ scanf(\ } return 0; }
//其他函数
void InitialMatrix(Matrix *T, int row, int col) {
//printf(\分配内存中......\\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 < T->row; i++) { T->mat[i] = (double *)malloc(T->col * sizeof(double)); if (T->mat[i] == NULL) { succ = 0; break;
8 / 13
} } //if(succ==1) // printf(\内存分配成功|;?\\n\ //else printf(\内存分配失败;\\n\ } }
void InitialMatrixZero(Matrix *T, int row, int col) { //printf(\矩阵初始化为零中......\\n\ int i, j; for (i = 0; i < row; i++) for (j = 0; j < col; j++) T->mat[i][j] = 0; //printf(\矩阵初始化为零矩阵成功;\\n\}
void InitialMatrixRand(Matrix *T, int row, int col) { int i, j; for (i = 0; i < row; i++) for (j = 0; j < col; j++) (*T).mat[i][j] = 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(\}
void DestroyMatrix(Matrix *T) { int i; for (i = 0; i < (*T).row; i++) free((*T).mat[i]); }
9 / 13
void PrintfMatrix(Matrix *T) {
int i, j; for (i = 0; i < (*T).row; i++) { for (j = 0; j < (*T).col; j++) printf(\ \ printf(\ } }
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).mat[i][j] = (*A).mat[i][j] + (*B).mat[i][j]; for (i = 0; i < (*A).row; i++) for (j = 0; j < (*A).col; j++) return 1; } else { printf(\这两个矩阵不能相加!\\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++)
10 / 13