-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrix.CPP
130 lines (111 loc) · 2.77 KB
/
Matrix.CPP
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
#include <iostream.h>
#include <conio.h>
class matrix
{
int data[2][2];
public:
matrix (int, int,int,int);
int get(int,int);
void set(int,int,int,int);
void print();
static matrix tera(matrix);
static int determinan(matrix);
friend matrix operator+(matrix,matrix);
friend matrix operator*(matrix,matrix);
friend matrix operator-(matrix,matrix);
};
matrix operator-(matrix m1,matrix m2)
{
matrix m3(0,0,0,0);
m3.set(m1.data[0][0] - m2.data[0][0],
m1.data[0][1] - m2.data[0][1],
m1.data[1][0] - m2.data[1][0],
m1.data[1][1] - m2.data[1][1] );
return m3;
}
matrix::matrix (int a1, int a2,int a3,int a4)
{
data[0][0] = a1;
data[0][1] = a2;
data[1][0] = a3;
data[1][1] = a4;
}
int matrix::get(int row,int col)
{
return data[row][col];
}
void matrix::set (int a1, int a2,int a3,int a4)
{
data[0][0] = a1;
data[0][1] = a2;
data[1][0] = a3;
data[1][1] = a4;
}
void matrix::print()
{
cout << " " << data[0][0] << " " ;
cout << data[0][1] << endl;
cout << " " << data[1][0] << " ";
cout << data[1][1] << endl << endl;
}
matrix operator+(matrix m1,matrix m2)
{
matrix m3(0,0,0,0);
m3.set(m1.data[0][0] + m2.data[0][0],
m1.data[0][1] + m2.data[0][1],
m1.data[1][0] + m2.data[1][0],
m1.data[1][1] + m2.data[1][1] );
return m3;
}
matrix operator*(matrix m1,matrix m2)
{
matrix m3(0,0,0,0);
m3.set(m1.data[0][0] * m2.data[0][0] + m1.data[0][1] * m2.data[1][0],
m1.data[0][0] * m2.data[0][1] + m1.data[0][1] * m2.data[1][1],
m1.data[1][0] * m2.data[0][0] + m1.data[1][1] * m2.data[1][0],
m1.data[1][0] * m2.data[0][1] + m1.data[1][1] * m2.data[1][1] );
return m3;
}
matrix matrix::tera(matrix m1)
{
matrix m2(0,0,0,0);
m2.set(m1.get(0,0), //for (int i = 0; i<=1; i++)
m1.get(1,0), //for (int j = 0; j<=1; j++)
m1.get(0,1), //m2.data[j][i] = m1.data[i][j];
m1.get(1,1) );
return m2;
}
int matrix::determinan(matrix m1)
{
return m1.data[0][0]*m1.data[1][1]-m1.data[0][1]*m1.data[1][0];
}
int main()
{
matrix m1(5,2,4,3);
matrix m2(2,3,1,2);
matrix m3(0,0,0,0);
cout << "Matrix1:" << endl;
m1.print();
cout<<"determinan (Matrix1): "<<matrix::determinan(m1);
cout << "\n\nMatrix2:" << endl;
m2.print();
cout<<"determinan (Matrix2): "<<matrix::determinan(m2);
//m3 = matrix::sum(m1,m2);
m3=m1+m2;
cout << "\n\nSum(+):" << endl;
m3.print();
m3 = m1*m2;
cout << "Mul(*):" << endl;
m3.print();
m3=m1-m2;
cout << "Minus(-):" << endl;
m3.print();
m3 = matrix::tera(m1);
cout << "Tera(Matrix1):" << endl<< endl;
m3.print();
m3 = matrix::tera(m3);
cout << "Tera(Tera(Matrix1))=Matrix1:" << endl << endl;
m3.print();
getch();
return 0;
}