78 lines
1.6 KiB
C++
78 lines
1.6 KiB
C++
|
/*
|
|||
|
Program: matrix-product-file-v0.c (Report comments/bugs to chikh@yuntech.edu.tw)
|
|||
|
Function: Ū<EFBFBD><EFBFBD><EFBFBD>ɮ׳]<EFBFBD>w<EFBFBD>x<EFBFBD>}<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>A<EFBFBD>]<EFBFBD>m<EFBFBD>nA<EFBFBD>PB<EFBFBD>x<EFBFBD>}<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>A<EFBFBD><EFBFBD><EFBFBD>@<EFBFBD>x<EFBFBD>}<EFBFBD>ۭ<EFBFBD>
|
|||
|
Note: <EFBFBD>w<EFBFBD>]Ū<EFBFBD><EFBFBD>matrixData1.txt<EFBFBD>FmatrixData2.txt<EFBFBD>Ω<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>A<EFBFBD>PB<EFBFBD><EFBFBD><EFBFBD>פ<EFBFBD><EFBFBD>Ū<EFBFBD><EFBFBD><EFBFBD><EFBFBD>p
|
|||
|
*/
|
|||
|
|
|||
|
#include <stdio.h>
|
|||
|
|
|||
|
#define N 100
|
|||
|
|
|||
|
int rowA, colA, rowB, colB;
|
|||
|
char idA, idB;
|
|||
|
|
|||
|
int init(int A[][N], int B[][N]) //<2F><><EFBFBD>l<EFBFBD><6C>A<EFBFBD>PB<50>x<EFBFBD>}
|
|||
|
{
|
|||
|
FILE *f;
|
|||
|
int i, j;
|
|||
|
|
|||
|
f = fopen("matrixData1.txt","r");
|
|||
|
//f = fopen("matrixData2.txt","r");
|
|||
|
fscanf(f,"%c %d %d %c %d %d",&idA,&rowA,&colA,&idB,&rowB,&colB);
|
|||
|
if (colA != rowB) return 0;
|
|||
|
for (i = 0; i < rowA; i++)
|
|||
|
for (j = 0; j < colA; j++)
|
|||
|
fscanf(f,"%d",&A[i][j]);
|
|||
|
for (i = 0; i < rowB; i++)
|
|||
|
for (j = 0; j < colB; j++)
|
|||
|
fscanf(f,"%d",&B[i][j]);
|
|||
|
|
|||
|
return 1;
|
|||
|
}
|
|||
|
|
|||
|
void mul(int A[][N], int B[][N], int C[][N]) //<2F><><EFBFBD>@C=A*B<>x<EFBFBD>}<7D>ۭ<EFBFBD><DBAD>B<EFBFBD><42><EFBFBD>A<EFBFBD>B<EFBFBD><EFBFBD>G<EFBFBD>s<EFBFBD>JC
|
|||
|
{
|
|||
|
int i, j, k, sum;
|
|||
|
|
|||
|
for (i = 0; i < rowA; i++) {
|
|||
|
for (j = 0; j < colB; j++) {
|
|||
|
for (sum = k = 0; k < colA; k++)
|
|||
|
sum += A[i][k]*B[k][j];
|
|||
|
C[i][j] = sum;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
void show(char p, char q, int X[][N], int row, int col)
|
|||
|
{
|
|||
|
int i, j;
|
|||
|
|
|||
|
if (q != '-') //<2F>Hq<48>ӰϹj<CFB9><6A><EFBFBD>ܼҦ<DCBC><D2A6><EFBFBD><EFBFBD>t<EFBFBD><74>
|
|||
|
printf("\nMatrix %c*%c =\n",p,q);
|
|||
|
else
|
|||
|
printf("\nMatrix %c =\n",p);
|
|||
|
|
|||
|
for (i = 0; i < row; i++) {
|
|||
|
for (j = 0; j < col; j++) printf("%6d",X[i][j]);
|
|||
|
printf("\n");
|
|||
|
}
|
|||
|
printf("\n");
|
|||
|
}
|
|||
|
|
|||
|
int main()
|
|||
|
{
|
|||
|
int A[N][N] = {0}, B[N][N] = {0}, C[N][N] = {0}, D[N][N] = {0};
|
|||
|
|
|||
|
printf("\n*** Ū<><C5AA><EFBFBD>y<EFBFBD>z<EFBFBD>x<EFBFBD>}<7D><><EFBFBD>ɮסA<D7A1>i<EFBFBD><69><EFBFBD>x<EFBFBD>}<7D><><EFBFBD>n<EFBFBD>B<EFBFBD><42> ***\n\n");
|
|||
|
if (init(A,B)==0) {
|
|||
|
printf("<EFBFBD>x<EFBFBD>}<7D><><EFBFBD>צ<EFBFBD><D7A6>~<7E>A<EFBFBD>L<EFBFBD>k<EFBFBD>B<EFBFBD><42><EFBFBD>Asorry\n");
|
|||
|
return -1;
|
|||
|
}
|
|||
|
mul(A,B,C);
|
|||
|
show('A','-',A,rowA,colA);
|
|||
|
show('B','-',B,rowB,colB);
|
|||
|
show('A','B',C,rowA,colB);
|
|||
|
|
|||
|
return 0;
|
|||
|
}
|