Advertisement
tranerius

22. Сортировка

Dec 24th, 2017
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.63 KB | None | 0 0
  1. #include <ctime>
  2. #include <iostream>
  3. #include <string>
  4. class workWithArr {
  5.     int **twoDimArr, rows, cols;
  6. public:
  7.     workWithArr(int rows, int cols) {
  8.         int temp;
  9.         this->rows = rows;
  10.         this->cols = cols;
  11.         this->twoDimArr = new int*[this->rows];
  12.         for (int i = 0; i < this->rows; i++) {
  13.             this->twoDimArr[i] = new int[this->cols];
  14.         }
  15.         for (int i = 0; i < this->rows; i++) {
  16.             for (int j = 0; j < this->cols; j++) {
  17.                 this->twoDimArr[i][j] = 1 + rand() % (this->cols*this->rows);
  18.             }
  19.         }
  20.         for (int i = 0; i < this->rows; i++) {
  21.             for (int j = 0; j < this->cols; j++) {
  22.                 for (int k = 0; k < this->rows; k++) {
  23.                     for (int l = 0; l < this->cols; l++) {
  24.                         if (i == k && j == l) {
  25.                             continue;
  26.                         }
  27.                         if (this->twoDimArr[i][j] == this->twoDimArr[k][l]) {
  28.                             this->twoDimArr[i][j] = 1 + rand() % (this->cols*this->rows);
  29.                             k--;
  30.                             i = 0; j = 0;
  31.                             break;
  32.                         }
  33.                     }
  34.                 }
  35.             }
  36.         }
  37.     }
  38.     ~workWithArr();
  39.     void shawArr() {
  40.         for (int i = 0; i < this->rows; i++) {
  41.             for (int j = 0; j < this->cols; j++) {
  42.                 std::cout << twoDimArr[i][j] << "\t";
  43.             }
  44.             std::cout << std::endl;
  45.         }
  46.     }
  47.     void sort(std::string sort_by, std::string sort_el) {
  48.         int temp;
  49.         if (sort_el == "rows") {
  50.             for (int i = 0; i < this->rows; i++) {
  51.                 for (int j = 0; j < this->cols; j++) {
  52.                     for (int k = j + 1; k < this->cols; k++) {
  53.                         if (sort_by == "increase") {
  54.                             if (this->twoDimArr[i][j] > this->twoDimArr[i][k]) {
  55.                                 temp = this->twoDimArr[i][j];
  56.                                 this->twoDimArr[i][j] = this->twoDimArr[i][k];
  57.                                 this->twoDimArr[i][k] = temp;
  58.                                 j = -1;
  59.                                 break;
  60.                             }
  61.                         }
  62.                         else {
  63.                             if (this->twoDimArr[i][j] < this->twoDimArr[i][k]) {
  64.                                 temp = this->twoDimArr[i][j];
  65.                                 this->twoDimArr[i][j] = this->twoDimArr[i][k];
  66.                                 this->twoDimArr[i][k] = temp;
  67.                                 j = -1;
  68.                                 break;
  69.                             }
  70.                         }
  71.                     }
  72.                 }
  73.             }
  74.         }
  75.         else if (sort_el == "cols") {
  76.             for (int j = 0; j < this->cols; j++) {
  77.                 for (int i = 0; i < this->rows; i++) {
  78.                     for (int k = i + 1; k < this->rows; k++) {
  79.                         if (sort_by == "increase") {
  80.                             if (this->twoDimArr[i][j] > this->twoDimArr[k][j]) {
  81.                                 temp = this->twoDimArr[i][j];
  82.                                 this->twoDimArr[i][j] = this->twoDimArr[k][j];
  83.                                 this->twoDimArr[k][j] = temp;
  84.                                 i = -1;
  85.                                 break;
  86.                             }
  87.                         }
  88.                         else {
  89.                             if (this->twoDimArr[i][j] < this->twoDimArr[k][j]) {
  90.                                 temp = this->twoDimArr[i][j];
  91.                                 this->twoDimArr[i][j] = this->twoDimArr[k][j];
  92.                                 this->twoDimArr[k][j] = temp;
  93.                                 i = -1;
  94.                                 break;
  95.                             }
  96.                         }
  97.                     }
  98.                 }
  99.             }
  100.         }
  101.         else {
  102.             for (int i = 0; i < rows; i++) {
  103.                 for (int j = 0; j < cols; j++) {
  104.                     for (int k = i; k < rows; k++) {
  105.                         for (int l = 0; l < cols; l++) {
  106.                             if (k == i) {
  107.                                 if (l < j) {
  108.                                     l = j + 1;
  109.                                     if (l >= cols) {
  110.                                         continue;
  111.                                     }
  112.                                 }
  113.                             }
  114.                             if (sort_by == "increase") {
  115.                                 if (this->twoDimArr[i][j] > this->twoDimArr[k][l]) {
  116.                                     temp = this->twoDimArr[i][j];
  117.                                     this->twoDimArr[i][j] = this->twoDimArr[k][l];
  118.                                     this->twoDimArr[k][l] = temp;
  119.                                     k--;
  120.                                     break;
  121.                                 }
  122.                             }
  123.                             else {
  124.                                 if (this->twoDimArr[i][j] < this->twoDimArr[k][l]) {
  125.                                     temp = this->twoDimArr[i][j];
  126.                                     this->twoDimArr[i][j] = this->twoDimArr[k][l];
  127.                                     this->twoDimArr[k][l] = temp;
  128.                                     k--;
  129.                                     break;
  130.                                 }
  131.                             }
  132.                         }
  133.                     }
  134.                 }
  135.             }
  136.         }
  137.     }
  138. };
  139. workWithArr::~workWithArr() {
  140.     for (int i = 0; i < this->rows; i++) {
  141.         delete[]this->twoDimArr[i];
  142.     }
  143.     delete[]this->twoDimArr;
  144.     this->twoDimArr = nullptr;
  145. }
  146. int main() {
  147.     srand(time(NULL));
  148.     setlocale(LC_ALL, "ru");
  149.     int rows, cols;
  150.     std::string sort_by, sort_el;
  151.     std::cout << "Количество строк: ";
  152.     std::cin >> rows;
  153.     std::cout << "Количество столбцов: ";
  154.     std::cin >> cols;
  155.     workWithArr o_1(rows, cols);
  156.     while (true) {
  157.         std::cout << "Какую часть массива отсортировать(rows, cols, all): ";
  158.         std::cin >> sort_el;
  159.         if (sort_el == "rows" || sort_el == "cols" || sort_el == "all") {
  160.             break;
  161.         }
  162.         else {
  163.             std::cout << "Неверный ответ" << std::endl;
  164.             continue;
  165.         }
  166.     }
  167.     while (true) {
  168.         std::cout << "Тип сортировки(increase, discrease): ";
  169.         std::cin >> sort_by;
  170.         if (sort_by == "increase" || sort_by == "discrease") {
  171.             break;
  172.         }
  173.         else {
  174.             std::cout << "Неверный ответ" << std::endl;
  175.             continue;
  176.         }
  177.     }
  178.     o_1.sort(sort_by, sort_el);
  179.     o_1.shawArr();
  180.     return 0;
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement