-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatr.cpp
More file actions
133 lines (107 loc) · 2.66 KB
/
Matr.cpp
File metadata and controls
133 lines (107 loc) · 2.66 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
#include <iostream>
#include <fstream>
#include <stdexcept>
#include <vector>
using namespace std;
class Matrix {
private:
int num_rows_;
int num_columns_;
vector<vector<int>> elements_;
public:
Matrix() {
num_rows_ = 0;
num_columns_ = 0;
}
Matrix(int num_rows, int num_columns) {
Reset(num_rows, num_columns);
}
void Reset(int num_rows, int num_columns) {
if (num_rows < 0) {
throw out_of_range("num_rows must be >= 0");
}
if (num_columns < 0) {
throw out_of_range("num_columns must be >= 0");
}
if (num_rows == 0 || num_columns == 0) {
num_rows = num_columns = 0;
}
num_rows_ = num_rows;
num_columns_ = num_columns;
elements_.assign(num_rows, vector<int>(num_columns));
}
int& At(int row, int column) {
return elements_.at(row).at(column);
}
int At(int row, int column) const {
return elements_.at(row).at(column);
}
int GetNumRows() const {
return num_rows_;
}
int GetNumColumns() const {
return num_columns_;
}
};
bool operator==(const Matrix& one, const Matrix& two) {
if (one.GetNumRows() != two.GetNumRows()) {
return false;
}
if (one.GetNumColumns() != two.GetNumColumns()) {
return false;
}
for (int row = 0; row < one.GetNumRows(); ++row) {
for (int column = 0; column < one.GetNumColumns(); ++column) {
if (one.At(row, column) != two.At(row, column)) {
return false;
}
}
}
return true;
}
Matrix operator+(const Matrix& one, const Matrix& two) {
if (one.GetNumRows() != two.GetNumRows()) {
throw invalid_argument("Mismatched number of rows");
}
if (one.GetNumColumns() != two.GetNumColumns()) {
throw invalid_argument("Mismatched number of columns");
}
Matrix result(one.GetNumRows(), one.GetNumColumns());
for (int row = 0; row < result.GetNumRows(); ++row) {
for (int column = 0; column < result.GetNumColumns(); ++column) {
result.At(row, column) = one.At(row, column) + two.At(row, column);
}
}
return result;
}
istream& operator>>(istream& in, Matrix& matrix) {
int num_rows, num_columns;
in >> num_rows >> num_columns;
matrix.Reset(num_rows, num_columns);
for (int row = 0; row < num_rows; ++row) {
for (int column = 0; column < num_columns; ++column) {
in >> matrix.At(row, column);
}
}
return in;
}
ostream& operator<<(ostream& out, const Matrix& matrix) {
out << matrix.GetNumRows() << " " << matrix.GetNumColumns() << endl;
for (int row = 0; row < matrix.GetNumRows(); ++row) {
for (int column = 0; column < matrix.GetNumColumns(); ++column) {
if (column > 0) {
out << " ";
}
out << matrix.At(row, column);
}
out << endl;
}
return out;
}
int main() {
Matrix one;
Matrix two;
cin >> one >> two;
cout << one + two << endl;
return 0;
}