-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclasses.h
More file actions
349 lines (316 loc) · 8.62 KB
/
classes.h
File metadata and controls
349 lines (316 loc) · 8.62 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#ifndef CPO_HW_CLASSES_H
#define CPO_HW_CLASSES_H
#include <iostream>
#include <string>
#include <cmath>
#include <chrono>
#include <random>
#include <vector>
template <class T>
class DynArray {
public:
explicit DynArray() {
arr.clear();
cur_length = 0;
}
DynArray(const DynArray& second) {
arr.clear();
this->cur_length = 0;
for (int i = 0; i < second.cur_length; ++i) {
this->append(second.arr[i]);
}
}
~DynArray() = default;
// T *arr = new T[1];
std::vector<T> arr;
int cur_length = 0;
void append(T val) {
arr.push_back(val);
cur_length++;
}
[[nodiscard]] T get(int index) const {
return arr[index];
}
[[nodiscard]] int cur_len() const {
return arr.size();
}
void print() const {
for (int i = 0; i < arr.size(); ++i) {
std::cout << arr[i] << ' ';
}
std::cout << std::endl;
}
DynArray& operator=(const DynArray& second) {
this->cur_length = 0;
for (int i = 0; i < second.cur_length; ++i) {
this->append(second.arr[i]);
}
return *this;
}
T& operator[](int i) {
return arr[i];
}
DynArray operator+(const DynArray& second) const {
DynArray temp;
for (int i = 0; i < this->cur_length; ++i) {
temp.append(this->arr[i]);
}
for (int i = 0; i < this->cur_length; ++i) {
temp.append(second.arr[i]);
}
return temp;
}
DynArray& operator>>(int k) {
k = k % cur_length;
for (int i = 0; i < k; ++i) {
double last = this->arr[cur_length - 1];
for (int j = cur_length - 2; j >= 0; j--) {
this->arr[j + 1] = this->arr[j];
}
this->arr[0] = last;
}
return *this;
}
DynArray& operator<<(int k) {
k = k % cur_length;
for (int i = 0; i < k; ++i) {
double last = this->arr[0];
for (int j = 1; j < cur_length; j++) {
this->arr[j - 1] = this->arr[j];
}
this->arr[cur_length - 1] = last;
}
return *this;
}
};
class MyStack {
public:
explicit MyStack(int size) {
this->stack = new int[size];
this->size = size;
cur_length = 0;
}
explicit MyStack() {
this->stack = new int[1];
this->size = 1;
cur_length = 0;
}
~MyStack() {
delete[] stack;
}
int *stack = new int[1];
int size, cur_length;
void push(int value) {
if (cur_length == size) {
auto *temp = new int[size * 2 + 1];
for (int i = 0; i < cur_length; ++i) {
temp[i] = stack[i];
}
delete[] stack;
stack = temp;
size = size * 2 + 1;
}
stack[cur_length] = value;
cur_length++;
}
int pop() {
cur_length--;
return stack[cur_length];
}
[[nodiscard]] int peak() const {
return stack[cur_length - 1];
}
[[nodiscard]] int capacity() const {
return size;
}
[[nodiscard]] int cur_size() const {
return cur_length;
}
void print() const {
for (int i = 0; i < cur_length; ++i) {
std::cout << stack[i] << ' ';
}
std::cout << std::endl;
}
};
template <class T>
class MyQueue {
public:
MyQueue() {
queue.clear();
cur_length = 0;
}
~MyQueue() = default;
std::vector<T> queue;
int cur_length = 0;
void push(T val) {
queue.push_back(val);
cur_length++;
}
T pop() {
T ans = queue[0];
operator<<(1);
cur_length--;
return ans;
}
T first() {
if (cur_length > 0) return queue[0];
std::cout << "Queue is empty!\n-1 returned" << std::endl;
return -1;
}
T last() {
if (cur_length > 0 ) return queue[cur_length - 1];
std::cout << "Queue is empty!\n-1 returned" << std::endl;
return -1;
}
void print() {
for (auto i = 0; i < cur_length; i++) {
std::cout << queue[i] << ' ';
}
std::cout << std::endl;
}
MyQueue<T> operator<<(int k) {
k = k % cur_length;
for (int i = 0; i < k; ++i) {
double last = this->queue[0];
for (int j = 1; j < cur_length; j++) {
this->queue[j - 1] = this->queue[j];
}
this->queue[cur_length - 1] = last;
}
return *this;
}
};
class Vec3D {
public:
double x, y, z;
Vec3D() : x(0.0), y(0.0), z(0.0) {}
Vec3D(double x_, double y_, double z_) : x(x_), y(y_), z(z_) {}
Vec3D operator*(Vec3D second) const {
Vec3D new_vec;
new_vec.x = this->x * second.x;
new_vec.y = this->y * second.y;
new_vec.z = this->z * second.z;
return new_vec;
}
Vec3D operator&(Vec3D second) const {
Vec3D new_vec;
new_vec.x = this->y * second.z - this->z * second.y;
new_vec.y = this->z * second.x - this->x * second.z;
new_vec.z = this->x * second.y - this->y * second.x;
return new_vec;
}
void print() const {
std::cout << '(' << this->x << ", " << this->y << ", " << this->z << ')' << std::endl;
}
};
template <class T>
class Mat2D {
public:
Mat2D(int n_, int m_) {
this->n = n_;
this->m = m_;
matrix.reserve(n);
for (int i = 0; i < m; ++i) {
matrix[i].reserve(m);
}
std::cout << "~matrix created " << n << 'x' << m << std::endl;
}
Mat2D() {
std::cout << "Input dim of matrix NxM:" << std::endl;
std::cin >> n >> m;
matrix.reserve(n);
for (int i = 0; i < m; ++i) {
matrix[i].reserve(m);
}
std::cout << "~matrix created " << n << 'x' << m << std::endl;
}
Mat2D(const Mat2D& matrix) {
this->n = matrix.n;
this->m = matrix.m;
this->matrix.reserve(n);
for (int i = 0; i < this->n; ++i) {
this->matrix[i].reserve(m);
for (int j = 0; j < this->m; ++j) {
this->matrix[i][j] = matrix.matrix[i][j];
}
}
std::cout << "~matrix created " << n << 'x' << m << std::endl;
}
~Mat2D() {
std::cout << "~matrix deleted " << n << 'x' << m << std::endl;
}
std::vector<std::vector<T>> matrix;
int n{}, m{};
void fill() {
std::cout << "Input matrix" << std::endl;
T a;
for (int i = 0; i < n; ++i) {
std::vector<T> temp;
for (int j = 0; j < m; ++j) {
std::cin >> a;
temp.push_back(a);
}
matrix.push_back(temp);
}
}
void print() const {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) { std::cout << this->matrix[i][j] << ' '; }
std::cout << std::endl;
}
}
void trans() {
std::vector<std::vector<T>> temp;
temp.resize(m);
for (int i = 0; i < this->m; ++i) {
temp[i].resize(n);
for (int j = 0; j < this->n; ++j) {
temp[i][j] = this->matrix[j][i];
}
}
matrix = temp;
std::swap(this->n, this->m);
}
[[nodiscard]] int determinant() const {
if (this->n != this->m) {
std::cout << "n != m, matrix is not squared" << std::endl;
return 0;
}
return 1;
}
Mat2D operator+(const Mat2D& second) {
if (this->n != second.n || this->m != second.m) {
std::cout << "Dimension error (+), first matrix returned" << std::endl;
return *this;
}
Mat2D<T> temp(this->n, this->m);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
temp.matrix[i][j] = this->matrix[i][j] + second.matrix[i][j];
}
}
return temp;
}
Mat2D operator*(const Mat2D& second) const {
if (this->m != second.n) {
std::cout << "Dimension error (*), first matrix returned" << std::endl;
return *this;
}
Mat2D<T> temp(this->n, second.m);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < second.m; ++j) {
temp.matrix[i][j] = 0;
for (int k = 0; k < this->m; ++k) {
temp.matrix[i][j] += this->matrix[i][k] * second.matrix[k][j];
}
}
}
return temp;
}
T& operator()(int i, int j)
{
return matrix[i][j];
}
};
#endif //CPO_HW_CLASSES_H