本文共 935 字,大约阅读时间需要 3 分钟。
三阶矩阵的乘法(C语言从文件中读入)
#include"stdio.h"
#include"stdlib.h"
using namespace std;
void display(int A[3][3]){
for (int i = 0; i <3 ; ++i) {
for (int j = 0; j < 3; ++j) {
printf("%d ",A[i][j]);
}
printf("\n");
}
printf("\n");
}
int main()
{
int A[3][3] ,B[3][3];
int result[3][3];
FILE *fp1=fopen("D:\\A.txt","rt");
if(fp1==NULL)
{
printf("oh shit!cannot open file A");
result -1;
}
for(int i = 0; i<3; ++i)
{
for(int j = 0;j<3;++j)
{
fscanf(fp1,"%d",&A[i][j]);
}
fscanf(fp1,"\n");
}
fclose(fp1);
FILE *fp2=fopen("D:\\B.txt","rt");
if(fp2==NULL)
{
printf("oh shit! cannot open file B.\n");
return -1;
}
for(int i = 0; i <3; ++i)
{
for(int j = 0;j<3;++j)
{
fscanf(fp2,"%d",&B[i][j]);
}
fscanf(fp2,"\n");
}
fclose(fp2);
for(int i = 0;i < 3; ++i)
{
for(int j = 0; j<3 ; ++j)
{
result[i][j]=A[i][j]*B[i][j];
}
}
printf("Matrix_A:\n");
display(A);
printf("Matrix_B:\n");
display(B);
printf("Matrix_A * Matrix_B = \n");
display(result);
return 1;
}
转载地址:http://rhlyo.baihongyu.com/