矩阵的加法和乘法

# include<stdio.h>
# include<stdlib.h>
typedef struct
{
int r;
int c;
int num;
}Mp;
typedef struct
{
int a[100][100];
int row1,col1,count1;
int b[100][100];
int row2,col2,count2;
Mp M1[10000],M2[10000];
}MpSave;
typedef struct
{
int c[100][100];
int row3,col3,count3;
}MpSaveShow;
MpSave S1;
/*********功能选项函数***********/
int Number()
{
int N;
printf("/*******************************************************/\n");
printf("/****1:The input matrix elements\n");
printf("/****2:Display elements of the matrix and nonzero element\n");
printf("/****3:Shows the results affter matrices\n");
printf("/****4:Shows the results affter multiplication\n");
printf("/****0:exit\n");
printf("/*******************************************************/\n");
printf("Please enter your choice:");
scanf("%d",&N);
system("cls");
return N;
system("pause");
}
/******输入函数*********/
MpSave Print(MpSave S1)
{
int i,j;
S1.count1=0;
S1.count2=0;
printf("/****Please enter the relevant data of the first matrix!!!!*****/\n");
printf("Please enter the first row of the matrix:");
scanf("%d",&S1.row1);
printf("Please enter the first column of the matrix:");
scanf("%d",&S1.col1);
printf("Please enter the first matrix element:\n");
for(i=0;i<S1.row1;i++)
{
for(j=0;j<S1.col1;j++)
{

scanf("%d",&S1.a[i][j]);
if(S1.a[i][j]!=0)
{
S1.M1[S1.count1].r=i+1;
S1.M1[S1.count1].c=j+1;
S1.M1[S1.count1].num=S1.a[i][j];
S1.count1++;
}
}
}
printf("/****Please enter the relevant data of the second matrix!!!!****/\n");
printf("Please enter the second row of the matrix:");
scanf("%d",&S1.row2);
printf("Please enter the second column of the matrix:");
scanf("%d",&S1.col2);
printf("Please enter the second matrix element:\n");
for(i=0;i<S1.row2;i++)
{
for(j=0;j<S1.col2;j++)
{
scanf("%d",&S1.b[i][j]);
if(S1.b[i][j]!=0)
{
S1.M2[S1.count2].r=i+1;
S1.M2[S1.count2].c=j+1;
S1.M2[S1.count2].num=S1.b[i][j];
S1.count2++;
}
}
}
system("cls");
return S1;
system("pause");
}
/*****显示函数*********/
void Show(MpSave S1)
{
int i,j;
printf("Please output of the first matrix element:\n");
for(i=0;i<S1.row1;i++)
{
for(j=0;j<S1.col1;j++)
{

printf("%-5d",S1.a[i][j]);
}
printf("\n");
}
printf("List the output of the first nonzero element of the matrix rows and columns,and value:\n");
for(i=0;i<S1.count1;i++)
{
printf("row=%-5d col=%-5d num=%-5d\n",S1.M1[i].r,S1.M1[i].c,S1.M1[i].num);
}
printf("Please output of the second matrix element:\n");
for(i=0;i<S1.row2;i++)
{
for(j=0;j<S1.col2;j++)
{

printf("%-5d",S1.b[i][j]);
}
printf("\n");
}
printf("List the output of the second nonzero element of the matrix rows and columns,and value:\n");
for(i=0;i<S1.count2;i++)
{
printf("row=%-5d col=%-5d num=%-5d\n",S1.M2[i].r,S1.M2[i].c,S1.M2[i].num);
}
system("pause");
system("cls");
}
/*******矩阵相加函数*************/
void AddShow(MpSave S1)
{
int i,j;
Mp M3[10000];
MpSaveShow S2;
S2.count3=0;
if(S1.col1==S1.col2&&S1.row1==S1.row2)
{
S2.col3=S1.col1;
S2.row3=S1.row1;
for(i=0;i<S1.row1;i++)
{
for(j=0;j<S1.col1;j++)
{
S2.c[i][j]=S1.a[i][j]+S1.b[i][j];
if(S2.c[i][j]!=0)
{
M3[S2.count3].r=i+1;
M3[S2.count3].c=j+1;
M3[S2.count3].num=S2.c[i][j];
S2.count3++;
}
}
}
printf("Two matrices obtained form the output matrix:\n");
for(i=0;i<S1.row1;i++)
{
for(j=0;j<S1.col1;j++)
{
printf("%-5d",S2.c[i][j]);
}
printf("\n");
}
printf("Output after two  matrices nonzero element in the matrix:\n");
for(i=0;i<S2.count3;i++)
{
printf("row=%-5d col=%-5d num=%-5d\n",M3[i].r,M3[i].c,M3[i].num);
}
}
else
{
printf("Does not meet the requirements of adding two matrices!!!!!\n");
}
system("pause");
system("cls");
}
/*********矩阵相乘函数*********/
void MultiplyShow(MpSave S1)
{
int i,j,k;
Mp M3[10000];
MpSaveShow S2;
S2.count3=0;
S2.row3=S1.row1;
S2.col3=S1.col2;
if(S1.col1==S1.row2)
{
for(i=0;i<S2.row3;i++)
{
for(j=0;j<S2.col3;j++)
{
S2.c[i][j]=0;
for(k=0;k<S1.col1;k++)
{
S2.c[i][j]+=S1.a[i][k]*S1.b[k][j];
}
if(S2.c[i][j]!=0)
{
M3[S2.count3].r=i+1;
M3[S2.count3].c=j+1;
M3[S2.count3].num=S2.c[i][j];
S2.count3++;
}
}
}
printf("Multiplies two matrices obtained from the output matrix:\n");
for(i=0;i<S2.row3;i++)
{
for(j=0;j<S2.col3;j++)
{
printf("%-5d",S2.c[i][j]);
}
printf("\n");
}
printf("Multiplies two matrices obtained from the output of nonzero elements in the matrix:\n");
for(i=0;i<S2.count3;i++)
{
printf("row=%-5d col=%-5d num=%-5d\n",M3[i].r,M3[i].c,M3[i].num);
}
}
else
{
printf("Multiplies two matrices do not meet the requirements!!!!!\n");
}
system("pause");
system("cls");
}
/********主函数*********/
int main()
{
while(1)
{
switch(Number())
{
case 1:
S1=Print(S1);
break;
case 2:
Show(S1);
break;
case 3:
AddShow(S1);
break;
case 4:
MultiplyShow(S1);
break;
case 0:
exit(0);
}
}
system("pause");
return 0;


}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值