Advertisement
tranerius

21. Перегрузка += и -=

Dec 22nd, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.33 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <ctime>
  4. class Matrix {
  5.     int **matrix, rows, cols;
  6. public:
  7.     Matrix() {
  8.         this->matrix = nullptr;
  9.     }
  10.     Matrix(const Matrix &object) {
  11.         this->rows = object.rows;
  12.         this->cols = object.cols;
  13.         this->matrix = new int*[this->rows];
  14.         for (int i = 0; i < this->rows; i++) {
  15.             this->matrix[i] = new int[this->cols];
  16.         }
  17.         for (int i = 0; i < this->rows; i++) {
  18.             for (int j = 0; j < this->cols; j++) {
  19.                 this->matrix[i][j] = object.matrix[i][j];
  20.             }
  21.         }
  22.     }
  23.     ~Matrix() {
  24.         for (int i = 0; i < this->rows; i++) {
  25.             delete[] this->matrix[i];
  26.         }
  27.         delete[]this->matrix;
  28.         this->matrix = nullptr;
  29.     }
  30.     void createMatrix(const int rows, const int cols) {
  31.         this->rows = rows;
  32.         this->cols = cols;
  33.         this->matrix = new int*[rows];
  34.         for (int i = 0; i < rows; i++) {
  35.             this->matrix[i] = new int[cols];
  36.         }
  37.     }
  38.     void fillMatrix(std::string fill) {
  39.         if (fill == "manually") {
  40.             for (int i = 0; i < this->rows; i++) {
  41.                 for (int j = 0; j < this->cols; j++) {
  42.                     std::cout << "Введите значение " << i + 1 << ", " << j + 1 << " элемента матрицы: ";
  43.                     std::cin >> this->matrix[i][j];
  44.                 }
  45.             }
  46.         }
  47.         else {
  48.             for (int i = 0; i < rows; i++) {
  49.                 for (int j = 0; j < cols; j++) {
  50.                     this->matrix[i][j] = 1 + rand() % (cols*rows);
  51.                 }
  52.             }
  53.             for (int i = 0; i < rows; i++) {
  54.                 for (int j = 0; j < cols; j++) {
  55.                     for (int l = 0; l < rows; l++) {
  56.                         for (int m = 0; m < cols; m++) {
  57.                             if (i == l && j == m) {
  58.                                 continue;
  59.                             }
  60.                             if (this->matrix[i][j] == this->matrix[l][m]) {
  61.                                 this->matrix[i][j] = 1 + rand() % (cols*rows);
  62.                                 i = 0;
  63.                                 j = 0;
  64.                                 break;
  65.                             }
  66.                         }
  67.                     }
  68.                 }
  69.             }
  70.         }
  71.     }
  72.     void ShawMatrix() {
  73.         std::cout << "\nМатрица " << rows << " x " << cols << " :" << std::endl;
  74.         for (int i = 0; i < rows; i++) {
  75.             for (int j = 0; j < cols; j++) {
  76.                 std::cout << this->matrix[i][j] << "\t";
  77.             }
  78.             std::cout << std::endl;
  79.         }
  80.         system("pause");
  81.     }
  82.     void changeMatrix() {
  83.         int i, j;
  84.         char answer;
  85.         do {
  86.             std::cout << "\nКакой элемент матрицы изменить? " << std::endl;
  87.             while (true) {
  88.                 std::cout << "Введите номер строки: ";
  89.                 std::cin >> i;
  90.                 if (i<1 || i>this->rows) {
  91.                     std::cout << "Ошибка." << std::endl;
  92.                     continue;
  93.                 }
  94.                 else { break; }
  95.             }
  96.             while (true) {
  97.                 std::cout << "Введите номер столбца: ";
  98.                 std::cin >> j;
  99.                 if (j<1 || j>this->cols) {
  100.                     std::cout << "Ошибка." << std::endl;
  101.                     continue;
  102.                 }
  103.                 else { break; }
  104.             }
  105.             std::cout << "Введите значение элемента " << i << ", " << j << " матрицы: ";
  106.             std::cin >> this->matrix[i - 1][j - 1];
  107.             do {
  108.                 std::cout << "Хотите изменить ещё элемент матрицы?(y/n) ";
  109.                 std::cin >> answer;
  110.                 if (answer != 'y' && answer != 'n') {
  111.                     std::cout << "Неверный ответ." << std::endl;
  112.                 }
  113.             } while (answer != 'y' && answer != 'n');
  114.         } while (answer != 'n');
  115.     }
  116.     void transposeMatrix() {
  117.         int temp;
  118.         int **transpose_matrix = new int*[this->cols];
  119.         for (int i = 0; i < this->cols; i++) {
  120.             transpose_matrix[i] = new int[this->rows];
  121.         }
  122.         for (int i = 0; i < this->cols; i++) {
  123.             for (int j = 0; j < this->rows; j++) {
  124.                 transpose_matrix[i][j] = this->matrix[j][i];
  125.             }
  126.         }
  127.         for (int i = 0; i < this->rows; i++) {
  128.             delete[]this->matrix[i];
  129.         }
  130.         delete[]this->matrix;
  131.         temp = this->rows;
  132.         this->rows = this->cols;
  133.         this->cols = temp;
  134.         this->matrix = transpose_matrix;
  135.     }
  136.     void shawElement(int rows, int cols) {
  137.         std::cout << "Элемент " << rows+1 << ", " << cols+1 << " равен: " << this->matrix[rows][cols] << std::endl;
  138.         system("pause");
  139.     }
  140.     Matrix & operator += (int value) {
  141.         for (int i = 0; i < this->rows; i++) {
  142.             for (int j = 0; j < this->cols; j++) {
  143.                 this->matrix[i][j] += value;
  144.             }
  145.         }
  146.         return *this;
  147.     }
  148.     Matrix & operator -= (int value) {
  149.         for (int i = 0; i < this->rows; i++) {
  150.             for (int j = 0; j < this->cols; j++) {
  151.                 this->matrix[i][j] -= value;
  152.             }
  153.         }
  154.         return *this;
  155.     }
  156.     int getRows() {
  157.         return this->rows;
  158.     }
  159.     int getCols() {
  160.         return this->cols;
  161.     }
  162. };
  163. int main() {
  164.     setlocale(LC_ALL, "ru");
  165.     srand(time(NULL));
  166.     int rows, cols, value;
  167.     char answer = '0';
  168.     bool second_operation = false;
  169.     Matrix object_1;
  170.     std::string fill;
  171.     while (true) {
  172.         if (answer != '0') {
  173.             second_operation = true;
  174.         }
  175.         if (second_operation) {
  176.             std::cout << "Список доступных операций:\n1 - Заполнить созданную матрицу"
  177.                 << "\ne - Завершить работу\nКакую операцию вы хотите выполнить? ";
  178.             std::cin >> answer;
  179.             if (answer != '1' && answer != 'e') {
  180.                 std::cout << "Неверный ответ.\n " << std::endl;
  181.                 continue;
  182.             }
  183.         }
  184.         else {
  185.             std::cout << "Список доступных операций:\n1 - Создать матрицу\ne - Завершить работу\nКакую операцию вы хотите выполнить? ";
  186.             std::cin >> answer;
  187.             if (answer != '1' && answer != 'e') {
  188.                 std::cout << "Неверный ответ.\n " << std::endl;
  189.                 continue;
  190.             }
  191.         }
  192.         if (second_operation) {
  193.             switch (answer) {
  194.             case '1':
  195.                 do {
  196.                     std::cout << "Каким образом выполнить заполнение матрицы?(manually/rand) ";
  197.                     std::cin >> fill;
  198.                     if (fill != "manually" && fill != "rand") {
  199.                         std::cout << "Неверный ответ." << std::endl;
  200.                     }
  201.                 } while (fill != "rand" && fill != "manually");
  202.                 object_1.fillMatrix(fill);
  203.                 break;
  204.             case 'e':
  205.                 return 0;
  206.             }
  207.             break;
  208.         }
  209.         else {
  210.             switch (answer) {
  211.             case '1':
  212.                 std::cout << "Введите количество строк: ";
  213.                 std::cin >> rows;
  214.                 std::cout << "Введите количество столбцов: ";
  215.                 std::cin >> cols;
  216.                 object_1.createMatrix(rows, cols);
  217.                 break;
  218.             case 'e':
  219.                 return 0;
  220.             }
  221.         }
  222.         continue;
  223.     }
  224.     while (true) {
  225.         std::cout << "\nСписок доступных операций:\n1 - Отобразить матрицу"
  226.             << "\n2 - Изменить элементы матрицы"
  227.             << "\n3 - Транспонировать матрицу"
  228.             << "\n4 - Увеличить элементы матрицы на введенное пользователем значение"
  229.             << "\n5 - Уменьшить элементы матрицы на введенное пользователем значение"
  230.             << "\n6 - Отобразить элемент матрицы с заданным индексом"
  231.             << "\ne - Завершить работу\nКакую операцию вы хотите выполнить? ";
  232.         std::cin >> answer;
  233.         if (answer != '1' && answer != '2' && answer != '3' && answer != '4' && answer != '5' && answer != '6' && answer != '7' && answer != 'e') {
  234.             std::cout << "Неверный ответ.\n " << std::endl;
  235.             continue;
  236.         }
  237.         switch (answer) {
  238.         case '1':
  239.             object_1.ShawMatrix();
  240.             break;
  241.         case '2':
  242.             object_1.changeMatrix();
  243.             break;
  244.         case '3':
  245.             object_1.transposeMatrix();
  246.             break;
  247.         case '4':
  248.             std::cout << "\nНа какое значение увеличить элементы матрицы? ";
  249.             std::cin >> value;
  250.             object_1 += value;
  251.             break;
  252.         case '5':
  253.             std::cout << "\nНа какое значение уменьшить элементы матрицы? ";
  254.             std::cin >> value;
  255.             object_1 -= value;
  256.             break;
  257.         case '6':
  258.             std::cout << "\nКакой элемент матрицы отобразить?" << std::endl;
  259.             while (true) {
  260.                 std::cout << "Введите номер строки: ";
  261.                 std::cin >> rows;
  262.                 if (rows<1 || rows>object_1.getRows()) {
  263.                     std::cout << "Ошибка." << std::endl;
  264.                     continue;
  265.                 }
  266.                 else { break; }
  267.             }
  268.             while (true) {
  269.                 std::cout << "Введите номер столбца: ";
  270.                 std::cin >> cols;
  271.                 if (cols<1 || cols>object_1.getCols()) {
  272.                     std::cout << "Ошибка." << std::endl;
  273.                     continue;
  274.                 }
  275.                 else { break; }
  276.             }
  277.             rows -= 1;
  278.             cols -= 1;
  279.             object_1.shawElement(rows, cols);
  280.             break;
  281.         case 'e':
  282.             return 0;
  283.         }
  284.     }
  285. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement