Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ctime>
- #include <iostream>
- #include <string>
- class workWithArr {
- int **twoDimArr, rows, cols;
- public:
- workWithArr(int rows, int cols) {
- int temp;
- this->rows = rows;
- this->cols = cols;
- this->twoDimArr = new int*[this->rows];
- for (int i = 0; i < this->rows; i++) {
- this->twoDimArr[i] = new int[this->cols];
- }
- for (int i = 0; i < this->rows; i++) {
- for (int j = 0; j < this->cols; j++) {
- this->twoDimArr[i][j] = 1 + rand() % (this->cols*this->rows);
- }
- }
- for (int i = 0; i < this->rows; i++) {
- for (int j = 0; j < this->cols; j++) {
- for (int k = 0; k < this->rows; k++) {
- for (int l = 0; l < this->cols; l++) {
- if (i == k && j == l) {
- continue;
- }
- if (this->twoDimArr[i][j] == this->twoDimArr[k][l]) {
- this->twoDimArr[i][j] = 1 + rand() % (this->cols*this->rows);
- k--;
- i = 0; j = 0;
- break;
- }
- }
- }
- }
- }
- }
- ~workWithArr();
- void shawArr() {
- for (int i = 0; i < this->rows; i++) {
- for (int j = 0; j < this->cols; j++) {
- std::cout << twoDimArr[i][j] << "\t";
- }
- std::cout << std::endl;
- }
- }
- void sort(std::string sort_by, std::string sort_el) {
- int temp;
- if (sort_el == "rows") {
- for (int i = 0; i < this->rows; i++) {
- for (int j = 0; j < this->cols; j++) {
- for (int k = j + 1; k < this->cols; k++) {
- if (sort_by == "increase") {
- if (this->twoDimArr[i][j] > this->twoDimArr[i][k]) {
- temp = this->twoDimArr[i][j];
- this->twoDimArr[i][j] = this->twoDimArr[i][k];
- this->twoDimArr[i][k] = temp;
- j = -1;
- break;
- }
- }
- else {
- if (this->twoDimArr[i][j] < this->twoDimArr[i][k]) {
- temp = this->twoDimArr[i][j];
- this->twoDimArr[i][j] = this->twoDimArr[i][k];
- this->twoDimArr[i][k] = temp;
- j = -1;
- break;
- }
- }
- }
- }
- }
- }
- else if (sort_el == "cols") {
- for (int j = 0; j < this->cols; j++) {
- for (int i = 0; i < this->rows; i++) {
- for (int k = i + 1; k < this->rows; k++) {
- if (sort_by == "increase") {
- if (this->twoDimArr[i][j] > this->twoDimArr[k][j]) {
- temp = this->twoDimArr[i][j];
- this->twoDimArr[i][j] = this->twoDimArr[k][j];
- this->twoDimArr[k][j] = temp;
- i = -1;
- break;
- }
- }
- else {
- if (this->twoDimArr[i][j] < this->twoDimArr[k][j]) {
- temp = this->twoDimArr[i][j];
- this->twoDimArr[i][j] = this->twoDimArr[k][j];
- this->twoDimArr[k][j] = temp;
- i = -1;
- break;
- }
- }
- }
- }
- }
- }
- else {
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < cols; j++) {
- for (int k = i; k < rows; k++) {
- for (int l = 0; l < cols; l++) {
- if (k == i) {
- if (l < j) {
- l = j + 1;
- if (l >= cols) {
- continue;
- }
- }
- }
- if (sort_by == "increase") {
- if (this->twoDimArr[i][j] > this->twoDimArr[k][l]) {
- temp = this->twoDimArr[i][j];
- this->twoDimArr[i][j] = this->twoDimArr[k][l];
- this->twoDimArr[k][l] = temp;
- k--;
- break;
- }
- }
- else {
- if (this->twoDimArr[i][j] < this->twoDimArr[k][l]) {
- temp = this->twoDimArr[i][j];
- this->twoDimArr[i][j] = this->twoDimArr[k][l];
- this->twoDimArr[k][l] = temp;
- k--;
- break;
- }
- }
- }
- }
- }
- }
- }
- }
- };
- workWithArr::~workWithArr() {
- for (int i = 0; i < this->rows; i++) {
- delete[]this->twoDimArr[i];
- }
- delete[]this->twoDimArr;
- this->twoDimArr = nullptr;
- }
- int main() {
- srand(time(NULL));
- setlocale(LC_ALL, "ru");
- int rows, cols;
- std::string sort_by, sort_el;
- std::cout << "Количество строк: ";
- std::cin >> rows;
- std::cout << "Количество столбцов: ";
- std::cin >> cols;
- workWithArr o_1(rows, cols);
- while (true) {
- std::cout << "Какую часть массива отсортировать(rows, cols, all): ";
- std::cin >> sort_el;
- if (sort_el == "rows" || sort_el == "cols" || sort_el == "all") {
- break;
- }
- else {
- std::cout << "Неверный ответ" << std::endl;
- continue;
- }
- }
- while (true) {
- std::cout << "Тип сортировки(increase, discrease): ";
- std::cin >> sort_by;
- if (sort_by == "increase" || sort_by == "discrease") {
- break;
- }
- else {
- std::cout << "Неверный ответ" << std::endl;
- continue;
- }
- }
- o_1.sort(sort_by, sort_el);
- o_1.shawArr();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement