Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iomanip>
- #include <iostream>
- #include <random>
- template<typename Type, size_t Size, typename = std::enable_if_t<std::is_arithmetic_v<Type>>>
- class Array {
- public:
- Array() : sequence{} {}
- auto& operator[](size_t i) {
- return sequence[i];
- }
- auto& operator[](size_t i) const {
- return sequence[i];
- }
- auto begin() {
- return &sequence[0];
- }
- auto end() {
- return &sequence[Size];
- }
- auto begin() const {
- return &sequence[0];
- }
- auto end() const {
- return &sequence[Size];
- }
- constexpr size_t size() const {
- return Size;
- }
- private:
- Type sequence[Size];
- };
- template<typename Type, size_t Rows, size_t Columns, typename = std::enable_if_t<std::is_arithmetic_v<Type>>>
- class Matrix {
- public:
- auto& operator[](size_t i) const {
- return matrix[i];
- }
- auto& operator[](size_t i) {
- return matrix[i];
- }
- Matrix operator+(const Matrix& mx) {
- auto tmp = *this;
- for (size_t i = 0; i < Rows; ++i) {
- for (size_t j = 0; j < Columns; ++j) {
- tmp[i][j] += mx[i][j];
- }
- }
- return tmp;
- }
- Matrix operator-(const Matrix& mx) {
- auto tmp = *this;
- for (size_t i = 0; i < Rows; ++i) {
- for (size_t j = 0; j < Columns; ++j) {
- tmp[i][j] -= mx[i][j];
- }
- }
- return tmp;
- }
- bool is_compatibility(const Matrix& mx) const {
- return rows() == mx.rows() && columns() == mx.columns();
- }
- size_t rows() const {
- return Rows;
- }
- size_t columns() const {
- return Columns;
- }
- auto begin() {
- return &matrix[0];
- }
- auto end() {
- return &matrix[Rows];
- }
- auto begin() const {
- return &matrix[0];
- }
- auto end() const {
- return &matrix[Rows];
- }
- private:
- Array<Type, Columns> matrix[Rows];
- friend Matrix operator*(const Matrix& a, const Matrix& b) {
- static const auto rows = a.rows();
- static const auto columns = b.columns();
- const auto count = a.columns();
- auto tmp = Matrix{};
- for (size_t i = 0; i < rows; ++i) {
- for (size_t j = 0; j < columns; ++j) {
- for (size_t k = 0; k < count; ++k) {
- tmp[i][j] += a[i][k] * b[k][j];
- }
- }
- }
- return tmp;
- }
- };
- using Matrix_int_3_3 = Matrix<int, 3, 3>;
- void random_fill(Matrix_int_3_3& matrix, int a, int b) {
- if (a > b) std::swap(a, b);
- std::uniform_int_distribution<> uid(a, b);
- std::mt19937 gen{ std::random_device()() };
- for (auto& row : matrix) {
- for (auto& value : row) {
- value = uid(gen);
- }
- }
- }
- void show(const char* prompt, const Matrix_int_3_3& matrix, std::streamsize width) {
- std::cout << prompt << "\n\n";
- for (const auto& row : matrix) {
- for (const auto value : row) {
- std::cout << std::setw(width) << value << ' ';
- }
- std::cout.put('\n');
- }
- std::cout.put('\n');
- }
- int main() {
- constexpr auto l = 1;
- constexpr auto r = 9;
- constexpr std::streamsize w = 5;
- Matrix_int_3_3 matrix_a{};
- random_fill(matrix_a, l, r);
- show("Matrix A:", matrix_a, w);
- Matrix_int_3_3 matrix_b{};
- random_fill(matrix_b, l, r);
- show("Matrix B:", matrix_b, w);
- Matrix_int_3_3 matrix_c{};
- random_fill(matrix_c, l, r);
- show("Matrix C:", matrix_c, w);
- const auto matrix_sum_ab = matrix_a + matrix_b;
- show("Matrix A + B:", matrix_sum_ab, w);
- const auto matrix_sub_ac = matrix_a - matrix_c;
- show("Matrix A - C:", matrix_sub_ac, w);
- if (matrix_b.is_compatibility(matrix_c)) {
- const auto matrix_product_bc = matrix_b * matrix_c;
- show("Matrix B x C:", matrix_product_bc, w);
- } else {
- std::cout << "Matrices A and B are incompatible\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement