满意答案
fdsloeoel7el
推荐于 2016.01.21
采纳率:46% 等级:12
已帮助:8456人
#include
#include
const int M = 5;
const int N = 4;
const int L = 3;
void main(){
int i = 0,j = 0,k;
FILE *fp1=NULL,*fp2=NULL;
int matr1[M][N],matr2[N][L],matr3[M][L];
//从文本文件1中读入矩阵
printf("The array 1 is :\n");
fp1 = fopen("../array1.txt","r");
if(!fp1)
{
printf("Can not open file!\n");
exit (0);
}
for (i=0;i
for (j=0;j
fscanf(fp1,"%d",&matr1[i][j]);
}
}
for (i=0;i
for (j=0;j
printf("%d\t",matr1[i][j]);
}
printf("\n");
}
fclose(fp1);
//从文本文件2中读入矩阵
printf("The array 2 is:\n");
fp2 = fopen("../array2.txt","r");
if(!fp2)
{
printf("Can not open file!\n");
exit (0);
}
for (i=0;i
for (j=0;j
fscanf(fp2,"%d",&matr2[i][j]);
}
}
for (i=0;i
for (j=0;j
printf("%d\t",matr2[i][j]);
}
printf("\n");
}
fclose(fp2);
//实现矩阵相乘,并将结果矩阵写入新的文件
printf("The result is :\n");
FILE* fp=NULL;
if((fp = fopen("../result.txt","a+"))==NULL)
{
printf("Can not open file!\n");
exit(0);
}
for(i = 0;i
for(j = 0;j
matr3[i][j] = 0;
for(k = 0;k
matr3[i][j] += matr1[i][k]*matr2[k][j];
}
printf("%d\t",matr3[i][j]);
}
printf("\n");
}
for(i = 0;i
for(j = 0;j
fprintf(fp,"%d\t",matr3[i][j]);
}
fputs("\n",fp);
}
fclose(fp);
}
00分享举报