Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <random>
- using namespace std;
- class Matrix
- {
- public:
- Matrix() = delete;
- Matrix(const Matrix&) = delete;
- Matrix(Matrix&&) = delete;
- Matrix& operator=(const Matrix&) = delete;
- Matrix& operator=(Matrix&&) = delete;
- Matrix(const size_t rows, const size_t cols)
- : matrix_(nullptr), rows_(rows), cols_(cols)
- {
- matrix_ = create(rows_, cols_);
- }
- ~Matrix()
- {
- if (matrix_ != nullptr) {
- for (size_t row = 0; row < rows_; ++row) {
- if (matrix_[row]) {
- delete[] matrix_[row];
- }
- }
- delete[] matrix_;
- matrix_ = nullptr;
- }
- }
- void fill(int a, int b)const
- {
- if (a > b) {
- swap(a, b);
- }
- const uniform_int_distribution<> uid(a, b);
- random_device rd;
- mt19937 gen(rd());
- for (size_t row = 0; row < rows_; ++row) {
- for (size_t col = 0; col < cols_; ++col) {
- matrix_[row][col] = uid(gen);
- }
- }
- }
- void print(const streamsize width)const
- {
- for (size_t row = 0; row < rows_; ++row) {
- for (size_t col = 0; col < cols_; ++col) {
- cout << setw(width) << matrix_[row][col];
- }
- cout.put('\n');
- }
- }
- void front_row(const int* row, const size_t size)
- {
- if (size != cols_) {
- return;
- }
- auto tmp = copy();
- const auto rows = rows_;
- destroy(matrix_, rows_);
- ++rows_;
- matrix_ = create(rows_, cols_);
- for (size_t i = 0; i < cols_; ++i) {
- matrix_[0][i] = row[i];
- }
- for (size_t r = 1; r < rows_; ++r) {
- for (size_t c = 0; c < cols_; ++c) {
- matrix_[r][c] = tmp[r - 1][c];
- }
- }
- destroy(tmp, rows);
- tmp = nullptr;
- }
- void push_row(const int* row, const size_t size)
- {
- if (size != cols_) {
- return;
- }
- auto tmp = copy();
- const auto rows = rows_;
- destroy(matrix_, rows_);
- ++rows_;
- matrix_ = create(rows_, cols_);
- for (size_t r = 0; r < rows; ++r) {
- for (size_t c = 0; c < cols_; ++c) {
- matrix_[r][c] = tmp[r][c];
- }
- }
- for (size_t i = 0; i < cols_; ++i) {
- matrix_[rows][i] = row[i];
- }
- destroy(tmp, rows);
- tmp = nullptr;
- }
- void insert_row(int* row, const size_t size, const size_t index)
- {
- if (size != cols_) {
- return;
- }
- if (index == 0) {
- front_row(row, size);
- } else if (index >= rows_) {
- push_row(row, size);
- }
- auto tmp = copy();
- const auto rows = rows_;
- destroy(matrix_, rows_);
- ++rows_;
- matrix_ = create(rows_, cols_);
- for (size_t r = 0; r < rows_; ++r) {
- for (size_t c = 0; c < cols_; ++c) {
- if (r < index) {
- matrix_[r][c] = tmp[r][c];
- } else if (r == index) {
- matrix_[r][c] = row[c];
- } else {
- matrix_[r][c] = tmp[r - 1][c];
- }
- }
- }
- destroy(tmp, rows);
- tmp = nullptr;
- }
- private:
- int** matrix_;
- size_t rows_;
- size_t cols_;
- int** copy()const
- {
- const auto tmp = create(rows_, cols_);
- for (size_t row = 0; row < rows_; ++row) {
- for (size_t col = 0; col < cols_; ++col) {
- tmp[row][col] = matrix_[row][col];
- }
- }
- return tmp;
- }
- int** create(const size_t rows, const size_t cols)const
- {
- const auto tmp = new int*[rows_];
- for (size_t row = 0; row < rows_; ++row) {
- tmp[row] = new int[cols_];
- }
- return tmp;
- }
- static void destroy(int** tmp, const size_t rows)
- {
- for (size_t row = 0; row < rows; ++row) {
- delete[] tmp[row];
- }
- delete[] tmp;
- }
- };
- int main()
- {
- cout << "Rows: ";
- size_t rows;
- cin >> rows;
- cout << "Columns: ";
- size_t cols;
- cin >> cols;
- Matrix matrix(rows, cols);
- matrix.fill(1, 100);
- matrix.print(4);
- cout.put('\n');
- auto row = new int[cols];
- auto x = 900;
- for (size_t i = 0; i < cols; ++i) {
- row[i] = ++x;
- }
- matrix.front_row(row, cols);
- matrix.print(4);
- cout.put('\n');
- matrix.push_row(row, cols);
- matrix.print(4);
- cout.put('\n');
- cout << "Index: ";
- size_t index;
- cin >> index;
- matrix.insert_row(row, cols, index);
- matrix.print(4);
- cout.put('\n');
- delete[] row;
- row = nullptr;
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement