Data_Structure/作業/unit1/matrix multiplication/by_me_2.c

129 lines
2.6 KiB
C
Raw Permalink Normal View History

2025-01-20 21:30:53 +08:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LINE_LENGTH 1000
// <20><><EFBFBD>t<EFBFBD>x<EFBFBD>}<7D>O<EFBFBD><4F><EFBFBD><EFBFBD>
int** allocateMatrix(int rows, int cols) {
int i;
int** matrix = (int**)malloc(rows * sizeof(int*));
for (i = 0; i < rows; i++) {
matrix[i] = (int*)malloc(cols * sizeof(int));
}
return matrix;
}
// <20><><EFBFBD><EFBFBD><EFBFBD>x<EFBFBD>}<7D>O<EFBFBD><4F><EFBFBD><EFBFBD>
void freeMatrix(int** matrix, int rows) {
int i ;
for (i = 0; i < rows; i++) {
free(matrix[i]);
}
free(matrix);
}
// <20>x<EFBFBD>}<7D><><EFBFBD>k
void mul(int **a, int **b, int **c, int m, int k, int n) {
int i , j , l ;
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
c[i][j] = 0;
for (l = 0; l < k; l++) {
c[i][j] += a[i][l] * b[l][j];
}
}
}
}
// Ū<><C5AA><EFBFBD>x<EFBFBD>}<7D>ê<EFBFBD><C3AA>^<5E><><EFBFBD><EFBFBD>
int** readMatrix(FILE* file, int* rows, int* cols) {
char line[MAX_LINE_LENGTH];
int** matrix = NULL;
*rows = 0;
*cols = 0;
while (fgets(line, sizeof(line), file)) {
if (strlen(line) <= 1) break; // <20>Ŧ<EFBFBD><C5A6><EFBFBD><EFBFBD>ܯx<DCAF>}<7D><><EFBFBD><EFBFBD>
(*rows)++;
int tempCols = 0;
char* token = strtok(line, " \t\n");
while (token != NULL) {
tempCols++;
token = strtok(NULL, " \t\n");
}
if (*cols == 0) {
*cols = tempCols;
matrix = allocateMatrix(*rows, *cols);
}
if (tempCols != *cols) {
printf("<EFBFBD><EFBFBD><EFBFBD>~<7E>G<EFBFBD>x<EFBFBD>}<7D>C<EFBFBD>Ƥ<EFBFBD><C6A4>@<40>P\n");
exit(1);
}
// <20><><EFBFBD><73><C5AA><EFBFBD>Ӧ<EFBFBD><D3A6>æs<C3A6>x<EFBFBD>ƾ<EFBFBD>
int col = 0;
token = strtok(line, " \t\n");
while (token != NULL) {
matrix[*rows - 1][col++] = atoi(token);
token = strtok(NULL, " \t\n");
}
}
return matrix;
}
int main() {
int i , j ;
FILE *file = fopen("matrix_input.txt", "r");
if (file == NULL) {
printf("<EFBFBD>L<EFBFBD>k<EFBFBD>}<7D><><EFBFBD>ɮ<EFBFBD>\n");
return 1;
}
int m, k1, k2, n;
int **arr_a, **arr_b, **arr_c;
// Ū<><C5AA><EFBFBD>Ĥ@<40>ӯx<D3AF>}
arr_a = readMatrix(file, &m, &k1);
// Ū<><C5AA><EFBFBD>ĤG<C4A4>ӯx<D3AF>}
arr_b = readMatrix(file, &k2, &n);
fclose(file);
// <20>ˬd<CBAC>x<EFBFBD>}<7D>O<EFBFBD>_<EFBFBD>i<EFBFBD>H<EFBFBD>ۭ<EFBFBD>
if (k1 != k2) {
printf("<EFBFBD><EFBFBD><EFBFBD>~<7E>G<EFBFBD>o<EFBFBD><6F><EFBFBD>ӯx<D3AF>}<7D>L<EFBFBD>k<EFBFBD>ۭ<EFBFBD>\n");
freeMatrix(arr_a, m);
freeMatrix(arr_b, k2);
return 1;
}
// <20><><EFBFBD>t<EFBFBD><74><EFBFBD>G<EFBFBD>x<EFBFBD>}<7D><><EFBFBD>Ŷ<EFBFBD>
arr_c = allocateMatrix(m, n);
// <20><><EFBFBD><EFBFBD><EFBFBD>x<EFBFBD>}<7D><><EFBFBD>k
mul(arr_a, arr_b, arr_c, m, k1, n);
// <20><><EFBFBD>X<EFBFBD><58><EFBFBD>G
printf("<EFBFBD>x<EFBFBD>}<7D><><EFBFBD>k<EFBFBD><6B><EFBFBD>G<EFBFBD>G\n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("%d ", arr_c[i][j]);
}
printf("\n");
}
// <20><><EFBFBD><EFBFBD><EFBFBD>O<EFBFBD><4F><EFBFBD><EFBFBD>
freeMatrix(arr_a, m);
freeMatrix(arr_b, k1);
freeMatrix(arr_c, m);
return 0;
}