1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155/* Goal : Matrix multiplication program in C
* date : 2021. 01 . 29
* author : Young Chan Kim (CogaPlex)
*/
#define MAX_MATRICES 10
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
typedef struct
{
int row;
int col;
int elements[100];
} Matrix;
int getMat(int argc, char* argv[], Matrix* inMat);
int mulMat(Matrix* mat, Matrix* resMat, int matNum[]);
void printMat(Matrix* mat, int index);
void checkError(int errorCode);
int main(int argc, char* argv[])
{
if (argc == 1 || argc > MAX_MATRICES)
{
return 1;
}
int errorCode = 0;
int index = 0;
int indexes[3] = {0, 0, 0};
Matrix inMat[MAX_MATRICES];
Matrix resMat[MAX_MATRICES-1] = {0};
errorCode = getMat(argc, argv, inMat);
checkError(errorCode);
printMat(inMat, 0);
printMat(inMat, 1);
indexes[0] = 0;
indexes[1] = 1;
indexes[2] = 0;
errorCode = mulMat(inMat, resMat, &indexes);
checkError(errorCode);
printMat(resMat, 0);
}
int getMat(int argc, char* argv[], Matrix* inMat)
{
int res = 0;
for (int i = 0; i < argc - 1; i++)
{
FILE* file = fopen(argv[i+1], "r");
if (file == NULL)
{
return 2;
}
res = fscanf(file, "%d", &inMat[i].row);
res = fscanf(file, "%d", &inMat[i].col);
int j = 0;
while (1)
{
res = fscanf(file, "%d", &inMat[i].elements[j]);
if (res == EOF)
{
break;
}
j++;
}
fclose(file);
}
return 0;
}
int mulMat(Matrix* mat, Matrix* resMat, int indexes[])
{
int A = indexes[0];
int B = indexes[1];
int AB = indexes[2];
if ((mat + A)->col != (mat + B)->row)
{
return 3;
}
(resMat + AB)->row = (mat + A)->row;
(resMat + AB)->col = (mat + B)->col;
int n = (resMat + AB)->row * (resMat + AB)->col;
int i = 0, m = 0;
while (i < n)
{
for (int j = 0; j < (resMat + AB)->col; i++, j++)
{
for (int k = 0; k < (mat + A)->col; k++)
{
(resMat + AB)->elements[i] += (mat + A)->elements[k + ((mat + A)->col * m)] * (mat + B)->elements[(k * (mat + B)->col) + j];
}
}
m++;
}
return 0;
}
void printMat(Matrix* mat, int index)
{
int row = (mat + index)->row;
int col = (mat + index)->col;
printf("#row %d #col %d\n", row, col);
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
printf("%d ", (mat + index)->elements[(col * i) + j]);
}
printf("\n");
}
printf("\n");
}
void checkError(int errorCode)
{
switch (errorCode)
{
case 0:
break;
case 1:
printf("MatMul.exe μλ¬ : μ
λ ₯λ κ°μ΄ μκ±°λ, λ무 λ§μ΅λλ€.");
exit(1);
case 2:
printf("getMat μλ¬ : File openμ μ€ν¨νμ΅λλ€.");
exit(1);
case 3:
printf("mulMat μλ¬ : μ
λ ₯λ νλ ¬λΌλ¦¬ κ³±μ
μ°μ°μ΄ λΆκ°λ₯ν©λλ€.");
exit(1);
default:
printf("Undefined error code. Please check again.");
break;
}
}