110 lines
2.5 KiB
C
110 lines
2.5 KiB
C
|
#include <stdio.h>
|
|||
|
#include <stdlib.h>
|
|||
|
#include <ctype.h>
|
|||
|
|
|||
|
#define MAX_SIZE 10 /* <20>w<EFBFBD>q<EFBFBD>x<EFBFBD>}<7D><><EFBFBD>̤j<CCA4>j<EFBFBD>p */
|
|||
|
|
|||
|
/* <20><><EFBFBD>ơGŪ<47><C5AA><EFBFBD>x<EFBFBD>} */
|
|||
|
int readMatrix(int matrix[MAX_SIZE][MAX_SIZE], int *cols, FILE *file) {
|
|||
|
int i = 0, j = 0, temp;
|
|||
|
char ch;
|
|||
|
|
|||
|
while (fscanf(file, "%d", &temp) == 1) {
|
|||
|
matrix[i][j] = temp;
|
|||
|
j++;
|
|||
|
|
|||
|
/* <20>ˬd<CBAC><64><EFBFBD>e<EFBFBD><65><EFBFBD>O<EFBFBD>_<EFBFBD><5F><EFBFBD><EFBFBD> */
|
|||
|
ch = fgetc(file);
|
|||
|
if (ch == '\n') {
|
|||
|
if (i == 0) {
|
|||
|
*cols = j; // <20><><EFBFBD><EFBFBD><EFBFBD>Ĥ@<40>檺<EFBFBD>C<EFBFBD><43>
|
|||
|
}
|
|||
|
i++; /* <20><><EFBFBD><EFBFBD><EFBFBD>U<EFBFBD>@<40><> */
|
|||
|
j = 0; /* <20><><EFBFBD>m<EFBFBD>C<EFBFBD>p<EFBFBD><70> */
|
|||
|
}
|
|||
|
if (ch == EOF) break;
|
|||
|
}
|
|||
|
return i; /* <20><><EFBFBD>^<5E><><EFBFBD><EFBFBD> */
|
|||
|
}
|
|||
|
|
|||
|
/* <20><><EFBFBD>ơG<C6A1><47><EFBFBD>L<EFBFBD>Ŧ<EFBFBD> */
|
|||
|
void skipEmptyLines(FILE *file) {
|
|||
|
char ch;
|
|||
|
while ((ch = fgetc(file)) != EOF) {
|
|||
|
if (!isspace(ch) || ch == '\n') {
|
|||
|
ungetc(ch, file); // <20>N<EFBFBD>D<EFBFBD>ťզr<D5A6>Űh<C5B0>^<5E>y<EFBFBD>A<EFBFBD>dzƤU<C6A4>@<40><>Ū<EFBFBD><C5AA>
|
|||
|
break;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/* <20><><EFBFBD>ơG<C6A1>L<EFBFBD>X<EFBFBD>x<EFBFBD>} */
|
|||
|
void printMatrix(int matrix[MAX_SIZE][MAX_SIZE], int rows, int cols) {
|
|||
|
int i, j;
|
|||
|
for (i = 0; i < rows; i++) {
|
|||
|
for (j = 0; j < cols; j++) {
|
|||
|
printf("%d ", matrix[i][j]);
|
|||
|
}
|
|||
|
printf("\n");
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/* <20><><EFBFBD>ơG<C6A1>x<EFBFBD>}<7D><><EFBFBD>k */
|
|||
|
void multiplyMatrix(int a[MAX_SIZE][MAX_SIZE], int b[MAX_SIZE][MAX_SIZE], int c[MAX_SIZE][MAX_SIZE],
|
|||
|
int rowsA, int colsA, int colsB) {
|
|||
|
int i, j, k;
|
|||
|
for (i = 0; i < rowsA; i++) {
|
|||
|
for (j = 0; j < colsB; j++) {
|
|||
|
c[i][j] = 0;
|
|||
|
for (k = 0; k < colsA; k++) {
|
|||
|
c[i][j] += a[i][k] * b[k][j];
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
int main() {
|
|||
|
FILE *file;
|
|||
|
int matrixA[MAX_SIZE][MAX_SIZE] = {0}, matrixB[MAX_SIZE][MAX_SIZE] = {0}, result[MAX_SIZE][MAX_SIZE] = {0};
|
|||
|
int rowsA = 0, colsA = 0, rowsB = 0, colsB = 0;
|
|||
|
|
|||
|
/* <20>}<7D><><EFBFBD>ɮ<EFBFBD> */
|
|||
|
file = fopen("matrix_input.txt", "r");
|
|||
|
if (file == NULL) {
|
|||
|
printf("<EFBFBD>L<EFBFBD>k<EFBFBD>}<7D><><EFBFBD>ɮ<EFBFBD>\n");
|
|||
|
return 1;
|
|||
|
}
|
|||
|
|
|||
|
/* Ū<><C5AA><EFBFBD>x<EFBFBD>} A */
|
|||
|
rowsA = readMatrix(matrixA, &colsA, file);
|
|||
|
|
|||
|
/* <20><><EFBFBD>L<EFBFBD>Ŧ<EFBFBD> */
|
|||
|
skipEmptyLines(file);
|
|||
|
|
|||
|
/* Ū<><C5AA><EFBFBD>x<EFBFBD>} B */
|
|||
|
rowsB = readMatrix(matrixB, &colsB, file);
|
|||
|
|
|||
|
/* <20><><EFBFBD><EFBFBD><EFBFBD>ɮ<EFBFBD> */
|
|||
|
fclose(file);
|
|||
|
|
|||
|
/* <20>L<EFBFBD>X<EFBFBD>x<EFBFBD>}<7D>j<EFBFBD>p */
|
|||
|
printf("<EFBFBD>x<EFBFBD>} A: %d x %d\n", rowsA, colsA);
|
|||
|
printf("<EFBFBD>x<EFBFBD>} B: %d x %d\n", rowsB, colsB);
|
|||
|
|
|||
|
/* <20>ˬd<CBAC>x<EFBFBD>}<7D>O<EFBFBD>_<EFBFBD>i<EFBFBD>H<EFBFBD>ۭ<EFBFBD> */
|
|||
|
if (colsA != rowsB) {
|
|||
|
printf("<EFBFBD><EFBFBD><EFBFBD>~<7E>G<EFBFBD>o<EFBFBD><6F><EFBFBD>ӯx<D3AF>}<7D>L<EFBFBD>k<EFBFBD>ۭ<EFBFBD>\n");
|
|||
|
return 1;
|
|||
|
}
|
|||
|
|
|||
|
/* <20><><EFBFBD><EFBFBD><EFBFBD>x<EFBFBD>}<7D><><EFBFBD>k */
|
|||
|
multiplyMatrix(matrixA, matrixB, result, rowsA, colsA, colsB);
|
|||
|
|
|||
|
/* <20>L<EFBFBD>X<EFBFBD><58><EFBFBD>G */
|
|||
|
printf("<EFBFBD>x<EFBFBD>}<7D><><EFBFBD>k<EFBFBD><6B><EFBFBD>G<EFBFBD>G\n");
|
|||
|
printMatrix(result, rowsA, colsB);
|
|||
|
|
|||
|
return 0;
|
|||
|
}
|
|||
|
|