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
|
#include <iostream>
#include <vector>
#include <exception>
class Matrix {
using Cols = std::vector<int>;
using Array = std::vector<Cols>;
Array data;
public:
const size_t rows {}, cols {};
Matrix(size_t rows, size_t cols);
Matrix(const Matrix& m);
Matrix(Matrix&& m) noexcept;
int at(size_t r, size_t c) const;
int& at(size_t r, size_t c);
Matrix add(Matrix m) const noexcept;
Matrix subtract(Matrix m) const noexcept;
Matrix multiply(const Matrix& m) const;
Matrix multiply(int num) const;
Matrix& addto(const Matrix& m);
Matrix& subtractto(const Matrix& m);
Matrix& multiplyto(int num) noexcept;
};
std::ostream& output(std::ostream& os, const Matrix& m) {
for (size_t i {}; i < m.rows; ++i) {
for (size_t j {}; j < m.cols; ++j)
os << m.at(i, j) << ' ';
os << '\n';
}
return os << '\n';
}
std::istream& input(std::istream& is, Matrix& m) {
for (size_t r {}; r < m.rows; ++r)
for (size_t c {}; c < m.cols; ++c) {
int value {};
is >> value;
m.at(r, c) = value;
}
return is;
}
Matrix::Matrix(size_t r, size_t c) : rows(r), cols(c), data { Array(r, Cols(c)) } {}
Matrix::Matrix(const Matrix& m) : data(m.data), rows(m.rows), cols(m.cols) {}
Matrix::Matrix(Matrix&& m) noexcept: data(std::move(m.data)), rows(m.rows), cols(m.cols) {}
int Matrix::at(size_t r, size_t c) const {
return data[r][c];
}
int& Matrix::at(size_t r, size_t c) {
return data[r][c];
}
Matrix Matrix::add(Matrix m) const noexcept {
return m.addto(*this);
}
Matrix Matrix::subtract(Matrix m) const noexcept {
return m.subtractto(*this);
}
Matrix Matrix::multiply(int num) const {
auto m1 { *this };
return m1.multiplyto(num);
}
Matrix& Matrix::addto(const Matrix& m) {
if (m.cols == cols && m.rows == rows) {
for (size_t i {}; i < rows; ++i)
for (size_t j {}; j < cols; ++j)
at(i, j) += m.at(i, j);
return *this;
} else
throw std::out_of_range("Invalid matrix size");
}
Matrix& Matrix::subtractto(const Matrix& m) {
if (m.cols == cols && m.rows == rows) {
for (size_t i {}; i < rows; ++i)
for (size_t j {}; j < cols; ++j)
at(i, j) -= m.at(i, j);
return *this;
} else
throw std::out_of_range("Invalid matrix size");
}
Matrix& Matrix::multiplyto(int num) noexcept {
for (size_t i {}; i < rows; ++i)
for (size_t j {}; j < cols; ++j)
at(i, j) *= num;
return *this;
}
Matrix Matrix::multiply(const Matrix& target) const {
if (cols == target.rows) {
Matrix output(rows, target.cols);
for (size_t r {}; r < output.rows; ++r)
for (size_t c {}; c < output.cols; ++c)
for (size_t k {}; k < target.rows; ++k)
output.at(r, c) += at(r, k) * target.at(k, c);
return output;
} else
throw std::out_of_range("Invalid matrix size");
};
int main() {
size_t r {}, c {}, c3 {};
std::cout << "Number of Rows in Matrix: ";
std::cin >> r;
std::cout << "Number of Columns in Matrix: ";
std::cin >> c;
Matrix m1(r, c), m2(r, c);
std::cout << "Values of Matrix 1 (row/column order): ";
input(std::cin, m1);
std::cout << "Values of Matrix 2 (row/column order): ";
input(std::cin, m2);
std::cout << "m1 is:\n";
output(std::cout, m1);
std::cout << "m2 is:\n";
output(std::cout, m2);
std::cout << "m1 + m2 is:\n";
output(std::cout, m1.add(m2));
std::cout << "m2 * 6 is:\n";
output(std::cout, m2.multiply(6));
std::cout << "Number of Columns in Matrix 3: ";
std::cin >> c3;
Matrix m3(c, c3);
std::cout << "Values of Matrix 3 (row/column order " << c << 'x' << c3 << "): ";
input(std::cin, m3);
std::cout << "m1 * m3 is:\n";
output(std::cout, m1.multiply(m3));
std::cout << '\n';
}
|