161 lines
3.3 KiB
C
161 lines
3.3 KiB
C
|
#include <stdio.h>
|
|||
|
#include <stdlib.h>
|
|||
|
#include <string.h>
|
|||
|
|
|||
|
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];
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
void read_matrix(FILE *file,int *m,int *k,int *n){
|
|||
|
char line[1000];
|
|||
|
// int *m = 0, *k1 = 0, *k2 = 0, *n = 0;
|
|||
|
int *k1 , *k2;
|
|||
|
int i, j, temp;
|
|||
|
char *token;
|
|||
|
|
|||
|
while (fgets(line, sizeof(line), file)) {
|
|||
|
if (strlen(line) <= 1) {
|
|||
|
break;
|
|||
|
}
|
|||
|
j = 0;
|
|||
|
token = strtok(line, " \n");
|
|||
|
while (token != NULL) {
|
|||
|
j++;
|
|||
|
token = strtok(NULL, " \n");
|
|||
|
}
|
|||
|
if (*k1 == 0) *k1 = j;
|
|||
|
m++;
|
|||
|
|
|||
|
}
|
|||
|
// Ū<><C5AA><EFBFBD>ĤG<C4A4>ӯx<D3AF>}
|
|||
|
while (fgets(line, sizeof(line), file)) {
|
|||
|
j = 0;
|
|||
|
token = strtok(line, " \n");
|
|||
|
while (token != NULL) {
|
|||
|
j++;
|
|||
|
token = strtok(NULL, " \n");
|
|||
|
}
|
|||
|
if (*n == 0) *n = j;
|
|||
|
k2++;
|
|||
|
*k2 = k2;
|
|||
|
}
|
|||
|
printf("OK");
|
|||
|
// if (*k1!=*k2) return 1;
|
|||
|
// if (*k1==*k2){
|
|||
|
}
|
|||
|
|
|||
|
int main() {
|
|||
|
FILE *file ;
|
|||
|
|
|||
|
int *m , *k , *n ;
|
|||
|
int i, j, temp;
|
|||
|
char *token;
|
|||
|
|
|||
|
if ((file = fopen("matrix_input.txt", "r")) == NULL){
|
|||
|
printf("<EFBFBD>L<EFBFBD>k<EFBFBD><EFBFBD><EFBFBD>}<7D><><EFBFBD><EFBFBD>\n");
|
|||
|
return 1;
|
|||
|
}
|
|||
|
|
|||
|
// Ū<><C5AA><EFBFBD>Ĥ@<40>ӯx<D3AF>}
|
|||
|
// while (fgets(line, sizeof(line), file)) {
|
|||
|
// if (strlen(line) <= 1) {
|
|||
|
// break;
|
|||
|
// }
|
|||
|
// j = 0;
|
|||
|
// token = strtok(line, " \n");
|
|||
|
// while (token != NULL) {
|
|||
|
// j++;
|
|||
|
// token = strtok(NULL, " \n");
|
|||
|
// }
|
|||
|
// if (k1 == 0) k1 = j;
|
|||
|
//
|
|||
|
// m++;
|
|||
|
// }
|
|||
|
//
|
|||
|
// // Ū<><C5AA><EFBFBD>ĤG<C4A4>ӯx<D3AF>}
|
|||
|
// while (fgets(line, sizeof(line), file)) {
|
|||
|
// j = 0;
|
|||
|
// token = strtok(line, " \n");
|
|||
|
// while (token != NULL) {
|
|||
|
// j++;
|
|||
|
// token = strtok(NULL, " \n");
|
|||
|
// }
|
|||
|
// if (n == 0) n = j;
|
|||
|
// k2++;
|
|||
|
// }
|
|||
|
|
|||
|
read_matrix(file,&m,&k,&n);
|
|||
|
|
|||
|
printf("<EFBFBD>Ĥ@<40>ӯx<D3AF>}: %d <20><> %d <20>C\n", *m, *k);
|
|||
|
printf("<EFBFBD>ĤG<EFBFBD>ӯx<EFBFBD>}: %d <20><> %d <20>C\n", *k, *n);
|
|||
|
printf("m = %d, k1 = %d, k2 = %d, n = %d\n\n", *m, *k , *k , *n);
|
|||
|
|
|||
|
|
|||
|
//<2F>ھ<EFBFBD>m,k,n<>t<EFBFBD>m<EFBFBD>O<EFBFBD><4F><EFBFBD><EFBFBD><EFBFBD>Ŷ<EFBFBD>
|
|||
|
int **arr_a = (int**)malloc(m * sizeof(int*));
|
|||
|
int **arr_b = (int**)malloc(k * sizeof(int*));
|
|||
|
int **arr_c = (int**)malloc(m * sizeof(int*));
|
|||
|
for(i=0;i<m;i++){
|
|||
|
arr_a[i] = (int*)malloc(k*sizeof(int));
|
|||
|
arr_c[i] = (int*)malloc(n*sizeof(int));
|
|||
|
}
|
|||
|
for(i=0;i<k;i++){
|
|||
|
arr_b[i] = (int*)malloc(n*sizeof(int));
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
// for (i = 0; i < m; i++) {
|
|||
|
// for (j = 0; j < k1; j++) {
|
|||
|
// arr_a[i][j] = matrix1[i][j];
|
|||
|
// }
|
|||
|
// }
|
|||
|
//
|
|||
|
// for (i = 0; i < k2; i++) {
|
|||
|
// for (j = 0; j < n; j++) {
|
|||
|
// arr_b[i][j] = matrix2[i][j];
|
|||
|
// }
|
|||
|
// }
|
|||
|
|
|||
|
mul(arr_a,arr_b,arr_c,m,k,n);
|
|||
|
|
|||
|
// printf("%d\n",arr_a[0][1]);
|
|||
|
// printf("%d\n",arr_b[0][1]);
|
|||
|
// printf("%d\n",arr_c[0][1]);
|
|||
|
|
|||
|
for(i=0;i<m;i++){
|
|||
|
for(j=0;j<n;j++){
|
|||
|
printf("c[%d][%d]=%d\n",i,j,arr_c[i][j]);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
fclose(file);
|
|||
|
|
|||
|
free (arr_a);
|
|||
|
free (arr_b);
|
|||
|
free (arr_c);
|
|||
|
for(i=0;i<m;i++){
|
|||
|
free (arr_a[i]);
|
|||
|
free (arr_c[i]);
|
|||
|
}
|
|||
|
for(i=0;i<k;i++){
|
|||
|
free (arr_b[i]);
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
// if(k1!=k2){
|
|||
|
// printf("<22><><EFBFBD>~! <20>x<EFBFBD>}<7D>L<EFBFBD>k<EFBFBD>ۭ<EFBFBD>\n");
|
|||
|
// fclose(file);
|
|||
|
// return 1;
|
|||
|
// }
|
|||
|
|
|||
|
return 0;
|
|||
|
}
|