Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <ctime>
- class Matrix {
- int **matrix, rows, cols;
- public:
- Matrix() {
- this->matrix = nullptr;
- }
- Matrix(const Matrix &object) {
- this->rows = object.rows;
- this->cols = object.cols;
- this->matrix = new int*[this->rows];
- for (int i = 0; i < this->rows; i++) {
- this->matrix[i] = new int[this->cols];
- }
- for (int i = 0; i < this->rows; i++) {
- for (int j = 0; j < this->cols; j++) {
- this->matrix[i][j] = object.matrix[i][j];
- }
- }
- }
- ~Matrix() {
- for (int i = 0; i < this->rows; i++) {
- delete[] this->matrix[i];
- }
- delete[]this->matrix;
- this->matrix = nullptr;
- }
- void createMatrix(const int rows, const int cols) {
- this->rows = rows;
- this->cols = cols;
- this->matrix = new int*[rows];
- for (int i = 0; i < rows; i++) {
- this->matrix[i] = new int[cols];
- }
- }
- void fillMatrix(std::string fill) {
- if (fill == "manually") {
- for (int i = 0; i < this->rows; i++) {
- for (int j = 0; j < this->cols; j++) {
- std::cout << "Введите значение " << i + 1 << ", " << j + 1 << " элемента матрицы: ";
- std::cin >> this->matrix[i][j];
- }
- }
- }
- else {
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < cols; j++) {
- this->matrix[i][j] = 1 + rand() % (cols*rows);
- }
- }
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < cols; j++) {
- for (int l = 0; l < rows; l++) {
- for (int m = 0; m < cols; m++) {
- if (i == l && j == m) {
- continue;
- }
- if (this->matrix[i][j] == this->matrix[l][m]) {
- this->matrix[i][j] = 1 + rand() % (cols*rows);
- i = 0;
- j = 0;
- break;
- }
- }
- }
- }
- }
- }
- }
- void ShawMatrix() {
- std::cout << "\nМатрица " << rows << " x " << cols << " :" << std::endl;
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < cols; j++) {
- std::cout << this->matrix[i][j] << "\t";
- }
- std::cout << std::endl;
- }
- system("pause");
- }
- void changeMatrix() {
- int i, j;
- char answer;
- do {
- std::cout << "\nКакой элемент матрицы изменить? " << std::endl;
- while (true) {
- std::cout << "Введите номер строки: ";
- std::cin >> i;
- if (i<1 || i>this->rows) {
- std::cout << "Ошибка." << std::endl;
- continue;
- }
- else { break; }
- }
- while (true) {
- std::cout << "Введите номер столбца: ";
- std::cin >> j;
- if (j<1 || j>this->cols) {
- std::cout << "Ошибка." << std::endl;
- continue;
- }
- else { break; }
- }
- std::cout << "Введите значение элемента " << i << ", " << j << " матрицы: ";
- std::cin >> this->matrix[i - 1][j - 1];
- do {
- std::cout << "Хотите изменить ещё элемент матрицы?(y/n) ";
- std::cin >> answer;
- if (answer != 'y' && answer != 'n') {
- std::cout << "Неверный ответ." << std::endl;
- }
- } while (answer != 'y' && answer != 'n');
- } while (answer != 'n');
- }
- void transposeMatrix() {
- int temp;
- int **transpose_matrix = new int*[this->cols];
- for (int i = 0; i < this->cols; i++) {
- transpose_matrix[i] = new int[this->rows];
- }
- for (int i = 0; i < this->cols; i++) {
- for (int j = 0; j < this->rows; j++) {
- transpose_matrix[i][j] = this->matrix[j][i];
- }
- }
- for (int i = 0; i < this->rows; i++) {
- delete[]this->matrix[i];
- }
- delete[]this->matrix;
- temp = this->rows;
- this->rows = this->cols;
- this->cols = temp;
- this->matrix = transpose_matrix;
- }
- void shawElement(int rows, int cols) {
- std::cout << "Элемент " << rows+1 << ", " << cols+1 << " равен: " << this->matrix[rows][cols] << std::endl;
- system("pause");
- }
- Matrix & operator += (int value) {
- for (int i = 0; i < this->rows; i++) {
- for (int j = 0; j < this->cols; j++) {
- this->matrix[i][j] += value;
- }
- }
- return *this;
- }
- Matrix & operator -= (int value) {
- for (int i = 0; i < this->rows; i++) {
- for (int j = 0; j < this->cols; j++) {
- this->matrix[i][j] -= value;
- }
- }
- return *this;
- }
- int getRows() {
- return this->rows;
- }
- int getCols() {
- return this->cols;
- }
- };
- int main() {
- setlocale(LC_ALL, "ru");
- srand(time(NULL));
- int rows, cols, value;
- char answer = '0';
- bool second_operation = false;
- Matrix object_1;
- std::string fill;
- while (true) {
- if (answer != '0') {
- second_operation = true;
- }
- if (second_operation) {
- std::cout << "Список доступных операций:\n1 - Заполнить созданную матрицу"
- << "\ne - Завершить работу\nКакую операцию вы хотите выполнить? ";
- std::cin >> answer;
- if (answer != '1' && answer != 'e') {
- std::cout << "Неверный ответ.\n " << std::endl;
- continue;
- }
- }
- else {
- std::cout << "Список доступных операций:\n1 - Создать матрицу\ne - Завершить работу\nКакую операцию вы хотите выполнить? ";
- std::cin >> answer;
- if (answer != '1' && answer != 'e') {
- std::cout << "Неверный ответ.\n " << std::endl;
- continue;
- }
- }
- if (second_operation) {
- switch (answer) {
- case '1':
- do {
- std::cout << "Каким образом выполнить заполнение матрицы?(manually/rand) ";
- std::cin >> fill;
- if (fill != "manually" && fill != "rand") {
- std::cout << "Неверный ответ." << std::endl;
- }
- } while (fill != "rand" && fill != "manually");
- object_1.fillMatrix(fill);
- break;
- case 'e':
- return 0;
- }
- break;
- }
- else {
- switch (answer) {
- case '1':
- std::cout << "Введите количество строк: ";
- std::cin >> rows;
- std::cout << "Введите количество столбцов: ";
- std::cin >> cols;
- object_1.createMatrix(rows, cols);
- break;
- case 'e':
- return 0;
- }
- }
- continue;
- }
- while (true) {
- std::cout << "\nСписок доступных операций:\n1 - Отобразить матрицу"
- << "\n2 - Изменить элементы матрицы"
- << "\n3 - Транспонировать матрицу"
- << "\n4 - Увеличить элементы матрицы на введенное пользователем значение"
- << "\n5 - Уменьшить элементы матрицы на введенное пользователем значение"
- << "\n6 - Отобразить элемент матрицы с заданным индексом"
- << "\ne - Завершить работу\nКакую операцию вы хотите выполнить? ";
- std::cin >> answer;
- if (answer != '1' && answer != '2' && answer != '3' && answer != '4' && answer != '5' && answer != '6' && answer != '7' && answer != 'e') {
- std::cout << "Неверный ответ.\n " << std::endl;
- continue;
- }
- switch (answer) {
- case '1':
- object_1.ShawMatrix();
- break;
- case '2':
- object_1.changeMatrix();
- break;
- case '3':
- object_1.transposeMatrix();
- break;
- case '4':
- std::cout << "\nНа какое значение увеличить элементы матрицы? ";
- std::cin >> value;
- object_1 += value;
- break;
- case '5':
- std::cout << "\nНа какое значение уменьшить элементы матрицы? ";
- std::cin >> value;
- object_1 -= value;
- break;
- case '6':
- std::cout << "\nКакой элемент матрицы отобразить?" << std::endl;
- while (true) {
- std::cout << "Введите номер строки: ";
- std::cin >> rows;
- if (rows<1 || rows>object_1.getRows()) {
- std::cout << "Ошибка." << std::endl;
- continue;
- }
- else { break; }
- }
- while (true) {
- std::cout << "Введите номер столбца: ";
- std::cin >> cols;
- if (cols<1 || cols>object_1.getCols()) {
- std::cout << "Ошибка." << std::endl;
- continue;
- }
- else { break; }
- }
- rows -= 1;
- cols -= 1;
- object_1.shawElement(rows, cols);
- break;
- case 'e':
- return 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement