Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- using namespace std;
- int takeNum(int Min, int Max) {
- const string error = "Проверьте корректность введенных данных!\n";
- bool isIncorrect;
- int num;
- do {
- isIncorrect = false;
- cin >> num;
- if (cin.fail()) {
- isIncorrect = true;
- cout << error;
- cin.clear();
- while (cin.get() != '\n');
- }
- if (!isIncorrect && cin.get() != '\n') {
- cin.clear();
- while (cin.get() != '\n');
- cout << error;
- isIncorrect = true;
- }
- if (!isIncorrect && (num < Min || num > Max)) {
- isIncorrect = true;
- cout << error;
- }
- } while (isIncorrect);
- return num;
- }
- template <typename T>
- T** add(T** first_matrix, T** second_matrix, int height, int length) {
- T** temp_matrix = new T * [height];
- for (int i = 0; i < height; i++)
- temp_matrix[i] = new T[length];
- for (int i = 0; i < height; i++)
- for (int j = 0; j < length; j++)
- temp_matrix[i][j] = first_matrix[i][j] + second_matrix[i][j];
- return temp_matrix;
- }
- template <typename T>
- T search(T** matrix, int height, int length) {
- T max_negative = INT_MIN;
- for (int i = 0; i < height; i++)
- for (int j = 0; j < length; j++)
- if (matrix[i][j] < 0 && matrix[i][j] > max_negative)
- max_negative = matrix[i][j];
- return max_negative;
- }
- template <typename T>
- void replace(T** matrix , int height, int length) {
- for (int i = 0; i < height; i++)
- for (int j = 0; j < length; j++)
- if (matrix[i][j] < 0)
- matrix[i][j] = abs(matrix[i][j]);
- }
- template <typename T>
- void sort(T** matrix, int height, int length) {
- T max = matrix[0][0];
- int index = 0;
- for (int i = 0; i < height; i++)
- for (int j = 0; j < length; j++)
- if (matrix[i][j] > max) {
- max = matrix[i][j];
- index = j;
- }
- for (int i = 0; i < height - 1; i++)
- for (int j = 0; j < height - i - 1; j++)
- if (matrix[j][index] < matrix[j + 1][index]) {
- T temp = matrix[j][index];
- matrix[j][index] = matrix[j + 1][index];
- matrix[j + 1][index] = temp;
- }
- }
- void createMatrix(int** matrix, int length, int height) {
- cout << "Введите значения элементов в матрице: \n";
- for (int i = 0; i < height; i++)
- for (int j = 0; j < length; j++) {
- cout << "a[" << i << "][" << j << "] = ";
- cin >> matrix[i][j];
- }
- }
- void getSize(int& height, int& length) {
- cout << "Введите количество строк в матрице: ";
- cin >> height;
- cout << "Введите количество столбцов в матрице: ";
- cin >> length;
- }
- void outputMatrix(int** matrix, int& length, int& height) {
- cout << "Ваша матрица: \n";
- for (int i = 0; i < height; i++)
- for (int j = 0; j < length; j++)
- cout << matrix[i][j] << " ";
- cout << endl;
- }
- void menu_program() {
- setlocale(LC_ALL, "Rus");
- int length = 0, height = 0;
- int** matrix = nullptr;
- int** tempMatrix = nullptr;
- int min = 0;
- while (true) {
- int choice;
- cout << "Текстовое меню:\n";
- cout << "1. Создание матрицы и ввод в нее значений\n";
- cout << "2. Вывести матрицу\n";
- cout << "3. Сложение матриц\n";
- cout << "4. Поиск максимального отрицательного элемента в матрице\n";
- cout << "5. Замена всех отрицательных элементов в матрице им противоположными\n";
- cout << "6. Сортировка столбцов, содержащих максимальный элемент матрицы, по убыванию\n";
- cout << "7. Выход\n";
- cout << "Введите номер выбранного действия: ";
- choice = takeNum(1, 2147483647);
- switch (choice) {
- case 1:
- getSize(height, length);
- matrix = new int* [height];
- for (int i = 0; i < height; i++)
- matrix[i] = new int[length];
- createMatrix(matrix, length, height);
- break;
- case 2:
- system("cls");
- outputMatrix(matrix, length, height);
- break;
- case 3:
- tempMatrix = new int* [height];
- for (int i = 0; i < height; i++)
- tempMatrix[i] = new int[length];
- createMatrix(tempMatrix, length, height);
- matrix = add(matrix, tempMatrix, height, length);
- for (int i = 0; i < height; i++)
- delete[] tempMatrix[i];
- delete[] tempMatrix;
- break;
- case 4:
- min = search(matrix, height, length);
- cout << "Максимальный отрицательный элемент это - " << min << endl;
- break;
- case 5:
- replace(matrix, height, length);
- cout << "Замена прошла успешно!" << endl;
- break;
- case 6:
- sort(matrix, height, length);
- cout << "Сортировка прошла успешно!" << endl;
- break;
- case 7:
- cout << "Программа завершена!" << endl;
- for (int i = 0; i < height; i++)
- delete[] matrix[i];
- delete[] matrix;
- return;
- default:
- cout << "Ошибка ввода! Введите то число, которое есть в меню\n";
- break;
- }
- }
- }
- int main() {
- menu_program();
- }
Advertisement
Add Comment
Please, Sign In to add comment