tranerius

20.16 через класс

Dec 19th, 2017
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.46 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. class twoDimArr {
  4.     int **arr, rows, cols;
  5. public:
  6.     twoDimArr(const int rows, const int cols) {
  7.         this->rows = rows;
  8.         this->cols = cols;
  9.         this->arr = new int*[rows];
  10.         for (int i = 0; i < rows; i++) {
  11.             arr[i] = new int[cols];
  12.         }
  13.     }
  14.     void FillArray() {
  15.         char answer;
  16.         do {
  17.             std::cout << "Указать элементы матрицы вручную?(y/n) ";
  18.             std::cin >> answer;
  19.             if (answer != 'y' && answer != 'n') {
  20.                 std::cout << "Неверный ответ." << std::endl;
  21.             }
  22.         } while (answer != 'y' && answer != 'n');
  23.         if (answer == 'y') {
  24.             while (true) {
  25.                 for (int i = 0; i < rows; i++) {
  26.                     for (int j = 0; j < cols; j++) {
  27.                         std::cout << "Введите элемент " << i + 1 << ", " << j + 1 << " матрицы ";
  28.                         std::cin >> arr[i][j];
  29.                     }
  30.                 }
  31.                 for (int i = 0; i < rows; i++) {
  32.                     for (int j = 0; j < cols; j++) {
  33.                         std::cout << arr[i][j] << "\t";
  34.                     }
  35.                     std::cout << std::endl;
  36.                 }
  37.                 do {
  38.                     std::cout << "Матрица составлена верно?(y/n) ";
  39.                     std::cin >> answer;
  40.                     if (answer != 'y' && answer != 'n') {
  41.                         std::cout << "Неверный ответ." << std::endl;
  42.                     }
  43.                 } while (answer != 'y' && answer != 'n');
  44.                 if (answer == 'y') {
  45.                     break;
  46.                 }
  47.                 else {
  48.                     continue;
  49.                 }
  50.             }
  51.         }
  52.         else if (answer == 'n') {
  53.             for (int i = 0; i < rows; i++) {
  54.                 for (int j = 0; j < cols; j++) {
  55.                     arr[i][j] = 1 + rand() % (cols*rows * 2);
  56.                 }
  57.             }
  58.             for (int i = 0; i < rows; i++) {
  59.                 for (int j = 0; j < cols; j++) {
  60.                     for (int k = 0; k < rows; k++) {
  61.                         for (int l = 0; l < cols; l++) {
  62.                             if (i == k && j == l) {
  63.                                 continue;
  64.                             }
  65.                             if (arr[i][j] == arr[k][l]) {
  66.                                 arr[i][j] = 1 + rand() % (cols*rows * 2);
  67.                                 k = -1;
  68.                                 i = 0;
  69.                                 j = 0;
  70.                                 break;
  71.                             }
  72.                         }
  73.                     }
  74.                 }
  75.             }
  76.         }
  77.     }
  78.     void ShawArray() {
  79.         for (int i = 0; i < rows; i++) {
  80.             for (int j = 0; j < cols; j++) {
  81.                 std::cout << arr[i][j] << "\t";
  82.             }
  83.             std::cout << std::endl;
  84.         }
  85.     }
  86.     void Transpose() {
  87.         int **transposeArr = new int*[cols];
  88.         for (int i = 0; i < cols; i++) {
  89.             transposeArr[i] = new int[rows];
  90.         }
  91.         for (int i = 0; i < cols; i++) {
  92.             for (int j = 0; j < rows; j++) {
  93.                 transposeArr[i][j] = arr[j][i];
  94.             }
  95.         }
  96.         for (int i = 0; i < rows; i++) {
  97.             delete[]arr[i];
  98.         }
  99.         delete[]arr;
  100.         this->arr = transposeArr;
  101.         int temp = rows;
  102.         this->rows = cols;
  103.         this->cols = temp;
  104.     }
  105.     twoDimArr(const twoDimArr &object) {
  106.         this->cols = object.cols;
  107.         this->rows = object.rows;
  108.         this->arr = new int*[object.rows];
  109.         for (int i = 0; i < object.rows; i++) {
  110.             arr[i] = new int[object.cols];
  111.         }
  112.     }
  113.     ~twoDimArr() {
  114.         for (int i = 0; i < rows; i++) {
  115.             delete[]arr[i];
  116.         }
  117.         delete[]arr;
  118.         arr = nullptr;
  119.     }
  120. };
  121. int main() {
  122.     setlocale(LC_ALL, "ru");
  123.     srand(time(NULL));
  124.     int rows, cols;
  125.     std::cout << "Введите количество строк: ";
  126.     std::cin >> rows;
  127.     std::cout << "Введите количество столбцов: ";
  128.     std::cin >> cols;
  129.     twoDimArr a(rows, cols);
  130.     twoDimArr b(a);
  131.     a.FillArray();
  132.     a.ShawArray();
  133.     a.Transpose();
  134.     std::cout << "Транспонированная матрица: " << std::endl;
  135.     a.ShawArray();
  136.     b.FillArray();
  137.     std::cout << "Матрица 1:" << std::endl;
  138.     a.ShawArray();
  139.     std::cout << "Матрица 2:" << std::endl;
  140.     b.ShawArray();
  141.     return 0;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment