-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix_sum.cpp
More file actions
158 lines (131 loc) · 3.02 KB
/
Copy pathmatrix_sum.cpp
File metadata and controls
158 lines (131 loc) · 3.02 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
#include <iostream>
#include <fstream>
#include <stdexcept>
#include <vector>
using namespace std;
// Реализуйте здесь
// * класс Matrix
class Matrix
{
public:
Matrix()
{
columns = 0;
}
Matrix(int num_rows, int num_cols)
{
Reset(num_rows, num_cols);
}
void Reset(int num_rows, int num_cols)
{
if (num_cols < 0 || num_rows < 0)
{
throw out_of_range("wrong params");
}
matrix.assign(num_rows, vector<int>(num_cols));
columns = num_cols;
}
int At(int num_rows, int num_cols) const
{
return matrix.at(num_rows).at(num_cols);
}
int& At(int num_rows, int num_cols)
{
return matrix.at(num_rows).at(num_cols);
}
int GetNumRows() const
{
return matrix.size();
}
int GetNumColumns() const
{
return columns;
}
const vector<vector<int>>& Value() const
{
return matrix;
}
private:
vector<vector<int>> matrix;
int columns = 0;
};
// * оператор ввода для класса Matrix из потока istream
istream& operator>>(istream &stream, Matrix &m)
{
int rows, cols;
stream >> rows >> cols;
m = Matrix(rows, cols);
for (int i = 0; i < rows; ++i)
{
for (int j = 0; j < cols; ++j)
{
stream >> m.At(i, j);
}
}
return stream;
}
// * оператор вывода класса Matrix в поток ostream
ostream& operator<<(ostream &stream, const Matrix &m)
{
int rows = m.GetNumRows();
int cols = m.GetNumColumns();
stream << rows << " " << cols << endl;
for (int i = 0; i < rows; ++i)
{
for (int j = 0; j < cols; ++j)
{
stream << m.At(i, j);
if (j == cols - 1)
{
stream << endl;
}
else
{
stream << " ";
}
}
}
return stream;
}
// * оператор проверки на равенство двух объектов класса Matrix
bool operator==(const Matrix &lhs, const Matrix &rhs)
{
if (lhs.Value() != rhs.Value() ||
lhs.GetNumRows() != rhs.GetNumRows() ||
lhs.GetNumColumns() != rhs.GetNumColumns())
{
return false;
}
else
{
return true;
}
}
// * оператор сложения двух объектов класса Matrix
Matrix operator+(const Matrix& lhs, const Matrix& rhs)
{
int rows = lhs.GetNumRows();
int cols = lhs.GetNumColumns();
if (rows != rhs.GetNumRows() || cols != rhs.GetNumColumns())
{
throw invalid_argument("matrix sizes are not equal");
}
Matrix sum(rows, cols);
for (int i = 0; i < rows; ++i)
{
for (int j = 0; j < cols; ++j)
{
sum.At(i, j) = lhs.At(i, j) + rhs.At(i, j);
}
}
return sum;
}
int main()
{
Matrix one;
Matrix two;
cin >> one >> two;
one.At(2, 2);
cout << one + two << endl;
return 0;
}