Skip to content

Commit 888affa

Browse files
committed
code splitted into separate files
1 parent 17f017c commit 888affa

File tree

3 files changed

+132
-104
lines changed

3 files changed

+132
-104
lines changed

Variants/Lab_12/var_12/main.cpp

Lines changed: 3 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -11,110 +11,7 @@
1111
//* Напишите программу для тестирования вашего класса
1212
// Операторы + - * / *(int)
1313

14-
#include <vector>
15-
#include <iostream>
16-
#include <cstdlib> // для exit()
17-
18-
using namespace std;
19-
20-
unsigned int get_size(vector <vector <double>> arr) {
21-
unsigned int size = arr.size();
22-
for (int i = 0; i < size; i++) {
23-
if (arr[i].size() != size) {
24-
return 0;
25-
}
26-
}
27-
return size;
28-
}
29-
30-
class Matrix {
31-
private:
32-
vector <vector <double>> arr;
33-
34-
public:
35-
Matrix(unsigned int size = 1, double def = 0) {
36-
try {
37-
if (size > 0) {
38-
arr.resize(size);
39-
for (int i = 0; i < size; i++) {
40-
arr[i].resize(size);
41-
if (def) {
42-
fill(arr[i].begin(), arr[i].end(), def);
43-
}
44-
}
45-
} else {
46-
throw "Matrix is not square";
47-
}
48-
} catch (const char *s) {
49-
cerr << "Error in class constructor: " << s << endl;
50-
exit(1);
51-
}
52-
}
53-
54-
Matrix(vector <vector <double>> temp) : Matrix (get_size(temp)) {
55-
arr = temp;
56-
}
57-
58-
const unsigned int size() {
59-
return arr.size();
60-
}
61-
62-
Matrix& set(unsigned int x, unsigned int y, double value) {
63-
try {
64-
if (x < arr.size() && y < arr.size()) {
65-
arr[y][x] = value;
66-
} else {
67-
throw "Invalid index";
68-
}
69-
} catch (const char *s) {
70-
cerr << "Error in 'set' operator: " << s << endl;
71-
exit(1);
72-
}
73-
return *this;
74-
}
75-
76-
Matrix& set(vector <vector <double>> temp) {
77-
// TODO: сделать присваивание матрицы другого разера
78-
try {
79-
if (get_size(temp) == arr.size()) {
80-
arr = temp;
81-
} else {
82-
throw "Target matrix has different size";
83-
}
84-
} catch (const char *s) {
85-
cerr << "Error in 'set' operator: " << s << endl;
86-
exit(1);
87-
}
88-
return *this;
89-
}
90-
91-
Matrix& set(Matrix& m) {
92-
this->arr = m.arr;
93-
return *this;
94-
}
95-
96-
double get(unsigned int x, unsigned int y) {
97-
try {
98-
if (x < arr.size() && y < arr.size()) {
99-
return arr[y][x];
100-
} else {
101-
throw "Invalid index";
102-
}
103-
} catch (const char *s) {
104-
cerr << "Error in 'get' operator: " << s << endl;
105-
exit(1);
106-
}
107-
}
108-
109-
void print() {
110-
for (int y = 0; y < arr.size(); y++) {
111-
for (int x = 0; x < arr[y].size(); x++)
112-
cout << arr[y][x] << " ";
113-
cout << endl;
114-
}
115-
cout << endl;
116-
}
117-
};
14+
#include "matrix.h"
11815

11916
int main() {
12017
Matrix m1;
@@ -228,4 +125,6 @@ int main() {
228125
// Error in 'get' operator: Invalid index
229126
cout << m10.get(10, 10) << " at 10, 10" << endl;
230127
*/
128+
129+
231130
}

Variants/Lab_12/var_12/matrix.cpp

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#include <cstdlib> // для exit()
2+
#include "matrix.h"
3+
4+
// class Matrix
5+
6+
Matrix::Matrix(unsigned int size, double def) {
7+
try {
8+
if (size > 0) {
9+
arr.resize(size);
10+
for (int i = 0; i < size; i++) {
11+
arr[i].resize(size);
12+
if (def) {
13+
fill(arr[i].begin(), arr[i].end(), def);
14+
}
15+
}
16+
} else {
17+
throw "Matrix is not square";
18+
}
19+
} catch (const char *s) {
20+
cerr << "Error in class constructor: " << s << endl;
21+
exit(1);
22+
}
23+
}
24+
25+
Matrix::Matrix(vector <vector <double>> temp):
26+
Matrix (get_size(temp)) {
27+
arr = temp;
28+
}
29+
30+
const unsigned int Matrix::size() {
31+
return arr.size();
32+
}
33+
34+
// сеттеры
35+
36+
Matrix& Matrix::set(unsigned int x, unsigned int y, double value) {
37+
try {
38+
if (x < arr.size() && y < arr.size()) {
39+
arr[y][x] = value;
40+
} else {
41+
throw "Invalid index";
42+
}
43+
} catch (const char *s) {
44+
cerr << "Error in 'set' operator: " << s << endl;
45+
exit(1);
46+
}
47+
return *this;
48+
}
49+
50+
Matrix& Matrix::set(vector <vector <double>> temp) {
51+
// TODO: сделать присваивание матрицы другого разера
52+
try {
53+
if (get_size(temp) == arr.size()) {
54+
arr = temp;
55+
} else {
56+
throw "Target matrix has different size";
57+
}
58+
} catch (const char *s) {
59+
cerr << "Error in 'set' operator: " << s << endl;
60+
exit(1);
61+
}
62+
return *this;
63+
}
64+
65+
Matrix& Matrix::set(Matrix& m) {
66+
this->arr = m.arr;
67+
return *this;
68+
}
69+
70+
// геттеры
71+
72+
double Matrix::get(unsigned int x, unsigned int y) {
73+
try {
74+
if (x < arr.size() && y < arr.size()) {
75+
return arr[y][x];
76+
} else {
77+
throw "Invalid index";
78+
}
79+
} catch (const char *s) {
80+
cerr << "Error in 'get' operator: " << s << endl;
81+
exit(1);
82+
}
83+
}
84+
85+
void Matrix::print() {
86+
for (int y = 0; y < arr.size(); y++) {
87+
for (int x = 0; x < arr[y].size(); x++)
88+
cout << arr[y][x] << " ";
89+
cout << endl;
90+
}
91+
cout << endl;
92+
}
93+
94+
// other functions
95+
96+
unsigned int get_size(vector <vector <double>> arr) {
97+
unsigned int size = arr.size();
98+
for (int i = 0; i < size; i++) {
99+
if (arr[i].size() != size) {
100+
return 0;
101+
}
102+
}
103+
return size;
104+
}

Variants/Lab_12/var_12/matrix.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#pragma once
2+
3+
#include <vector>
4+
#include <iostream>
5+
using namespace std;
6+
7+
unsigned int get_size(vector <vector <double>> arr);
8+
9+
class Matrix {
10+
private:
11+
vector <vector <double>> arr;
12+
13+
public:
14+
Matrix(unsigned int size = 1, double def = 0);
15+
Matrix(vector <vector <double>> temp);
16+
17+
Matrix& set(unsigned int x, unsigned int y, double value);
18+
Matrix& set(vector <vector <double>> temp);
19+
Matrix& set(Matrix& m);
20+
21+
double get(unsigned int x, unsigned int y);
22+
const unsigned int size();
23+
24+
void print();
25+
};

0 commit comments

Comments
 (0)