include stdio include windows template typename TypeName class Matrix

 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
#include <stdio.h>
#include <windows.h>
template<typename TypeName> class Matrix{
public:
TypeName **matrix;
int _x, _y;
void Input(){
printf("Input matrix %dx%d:\n", _y, _x);
for(int y=0;y<_y;y++){
for(int x=0;x<_x;x++)
scanf("%d", &matrix[y][x]);
}
return;
}
void Print(){
for(int y=0;y<_y;y++){
for(int x=0;x<_x;x++)
printf("%d\t", matrix[y][x]);
printf("\n");
}
return;
}
Matrix(int y, int x){
_x=x;
_y=y;
matrix = new TypeName*[_y];
for(int i=0;i<_y;i++){
matrix[i]=new TypeName[_x];
}
return;
};
Matrix operator*(const Matrix &matr)const{
Matrix nmatrix(_y, matr._x);
for(int y=0;y<_y;y++){
for(int j=0;j<matr._x;j++){
nmatrix.matrix[y][j]=0;
for(int x=0;x<_x;x++){
nmatrix.matrix[y][j]+=matrix[y][x]*matr.matrix[x][j];
}
}
}
return nmatrix;
}
};
int main(void){
int y1, x1, y2, x2;//2 2 2 2 1 2 3 4 5 6 7 8
printf("Type size of first matrix:");
scanf("%d %d", &y1, &x1);
printf("Type size of second matrix:");
scanf("%d %d", &y2, &x2);
Matrix<int> mf(y1, x1), ms(y2, x2), mres(y1, x2);
mf.Input();
ms.Input();
if(mf._y==ms._x){
mres=mf*ms;
printf("Result is\n");
mres.Print();
}
else{
printf("Unable to multiply matrices.\n");
}
system("pause");
return 0;
}