-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathMatrix.h
110 lines (93 loc) · 1.84 KB
/
Matrix.h
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
#include <iostream>
#include <stdio.h>
class Matrix {
private:
int **m;
int num_rows, num_cols;
class Column {
int *column;
int len;
public:
Column(int *column, int len) {
this->len = len;
this->column = column;
};
int &operator[](int i) const {
if (i < len && i >= 0) {
return column[i];
}
else {
throw std::out_of_range("");
}
}
};
public:
Matrix(int num_cols, int num_rows) {
this->num_cols = num_cols;
this->num_rows = num_rows;
m = new int*[num_cols];
for (int i = 0; i < num_cols; i++) {
m[i] = new int[num_rows];
}
};
~Matrix() {
for (int i = 0; i < num_cols; i++) {
delete[] m[i];
}
delete[] m;
}
int getRows() const {
return num_rows;
}
int getColumns() const {
return num_cols;
}
const Column operator[](int i) const {
if (i < num_cols && i >=0) {
Column arr(m[i], num_rows);
return arr;
}
else {
throw std::out_of_range("");
}
}
int& getNumber(int i, int j) const {
return m[i][j];
}
bool operator == (const Matrix &m2) const {
if (num_rows != m2.getRows() || num_cols != m2.getColumns()) {
return false;
} else {
for (int i = 0; i < num_rows; i++) {
for (int j = 0; j < num_cols; j++) {
if (m[i][j] != m2.m[i][j]) {
return false;
}
}
}
}
return true;
}
bool operator != (const Matrix &m2) const {
if (num_rows != m2.getRows() || num_cols != m2.getColumns()) {
return true;
}
else {
for (int i = 0; i < num_rows; i++) {
for (int j = 0; j < num_cols; j++) {
if (m[i][j] != m2.m[i][j]) {
return true;
}
}
}
}
return false;
}
void operator *= (int a) {
for (int i = 0; i < num_cols; i++) {
for (int j = 0; j < num_rows; j++) {
m[i][j] *= a;
}
}
}
};