矩阵的运算代码(加减乘除)(内有注释)
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#define ROW 10 //定义行
#define COL 10 //定义列
//设置全局变量A矩阵的m代表实际矩阵的行数,n代表实际矩阵的列数
//设置全局变量B矩阵的p代表实际矩阵的行数,q代表实际矩阵的列数
int m, n;
int p, q;
void menu()
{printf("**********************请选择你要进行的运算\n");printf("**********************0:退出矩阵运算 \n");printf("**********************1:加法运算 \n");printf("**********************2:减法运算 \n");printf("**********************3:转置运算 \n");printf("**********************4:乘法运算 \n");
}
void Input_MAT(int A[][COL], int B[][COL])
{int i = 0;int j = 0;printf("输入A数组的行数,列数:");scanf("%d%d", &m, &n);printf("输入A数组:\n");for (; i < m; i++){for (j = 0; j < n; j++){scanf("%d", &A[i][j]);}}printf("输入B数组的行数,列数:");scanf("%d%d", &p, &q);printf("输入B数组:\n");//输入B数组for (i = 0; i < p; i++){for (j = 0; j < q; j++){scanf("%d", &B[i][j]);}}
}
void Output_MAT(int A[][COL], int B[][COL])
{int i = 0;int j = 0;//输出A数组printf("输出A数组:\n");for (; i < m; i++){for (j = 0; j < n; j++){printf("%2d ", A[i][j]);}printf("\n");}printf("输出B数组:\n");//输出B数组for (i = 0; i < p; i++){for (j = 0; j < q; j++){printf("%2d ", B[i][j]);}printf("\n");}
}
//矩阵相加
void Add(int A[][COL],int B[][COL],int sum[][COL])
{if (m == p && n == q){int i = 0;int j = 0;for (; i < m; i++){for (j = 0; j < n; j++)sum[i][j] = A[i][j] + B[i][j];//得到和矩阵}//打印和矩阵printf("开始打印和矩阵\n");for (i=0; i < m; i++){for (j = 0; j < n; j++)printf("%2d ", sum[i][j]);printf("\n");}}elseprintf("你的矩阵相加形式不合法,请重新输入\n");
}
void Sub(int A[][COL], int B[][COL], int sum[][COL])
{if (m == p && n == q){int i = 0;int j = 0;for (; i < m; i++){for (j = 0; j < n; j++)sum[i][j] = A[i][j] - B[i][j];//得到差矩阵}//打印和矩阵printf("开始打印减矩阵\n");for (i = 0; i < m; i++){for (j = 0; j < n; j++)printf("%2d ", sum[i][j]);printf("\n");}}elseprintf("你的矩阵相减形式不合法,请重新输入\n");
}
void Tra(int A[][COL],int B[][COL])
{printf("下面开始A矩阵的转置\n");int i = 0;int j = 0;//转置运算for (; i < m; i++){for (j = i; j < n; j++){int tmp = 0;tmp = A[i][j];A[i][j] = A[j][i];A[j][i] = tmp;}}printf("下面开始B矩阵的转置\n");for (i = 0; i < p; i++){for (j = i; j < q; j++){int tmp = 0;tmp = B[i][j];B[i][j] = B[j][i];B[j][i] = tmp;}}//输入转置后的A,B矩阵Output_MAT(A, B);
}
void Mul1(int A[][COL], int B[][COL], int sum[][COL])
{int i = 0;int j = 0;int k = 0;if (n == p){for (; i < m; i++){for (j = 0; j < q; j++){for (k = 0; k < n; k++)sum[i][j] += A[i][k] * B[k][j];}}printf("开始打印积矩阵\n");for (i = 0; i < m; i++){for (j = 0; j < q; j++)printf("%2d ", sum[i][j]);printf("\n");}}elseprintf("矩阵相乘形式不合法\n");
}
void Mul2(int A[][COL], int B[][COL], int sum[][COL])
{int i = 0;int j = 0;int k = 0;if (q == m){for (; i < p; i++){for (j = 0; j < n; j++){for (k = 0; k < q; k++)sum[i][j] += B[i][k] * A[k][j];}}printf("开始打印积矩阵\n");for (i = 0; i < p; i++){for (j = 0; j < n; j++)printf("%2d ", sum[i][j]);printf("\n");}}elseprintf("矩阵相乘形式不合法\n");
}int main()
{int A[ROW][COL] = { 0 };//初始化A数组int B[ROW][COL] = { 0 };//初始化B数组//矩阵输入函数//Input_MAT(A,B);//矩阵输出函数//Output_MAT(A, B);int input = 0;//初始化要进行的运算密码do{menu();//菜单,你要进行的矩阵运算printf("请输入密码进行矩阵运算:");scanf("%d", &input);//矩阵输入函数Input_MAT(A, B);//矩阵输出函数Output_MAT(A, B);int flag = 0;//矩阵乘法,若flag = 0,进行A乘以B,若flag=1,进行B乘以Aint sum[ROW][COL] = { 0 };switch (input){case 0:printf("退出矩阵运算");break;case 1://定义矩阵之和的和矩阵Add(A, B, sum);//加法运算break;case 2://矩阵减法运算Sub(A, B, sum);break;case 3://A,B矩阵的转置Tra(A,B);break;case 4://A,B矩阵的乘法printf("请输入你要进行的矩阵乘法\n");printf("输入 0 进行A矩阵乘以B矩阵的运算\n");printf("输入 1 进行B矩阵乘以A矩阵的运算\n");printf("请输入数字:");scanf("%d", &flag);if(flag==0)Mul1(A, B, sum);if(flag==1)Mul2(A, B, sum);break;default:printf("输入数字不合法,请重新输入\n");break;}} while (input);return 0;
}