Advertisement
MadCortez

Untitled

Mar 17th, 2021
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.78 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. int inputValue(int min, int max);
  9. pair<int, int> userInputFromConsole();
  10. short** userInputFromFile(string path);
  11. bool checkPath(string path);
  12. string userInputPath();
  13. short inputMethod();
  14. void printInConsole(short** a, short** b);
  15. string userOutputPath();
  16. void printInFile(short** a, short** b, string path);
  17. short outputMethod();
  18. void start();
  19. void printTask();
  20.  
  21. int inputValue(int min, int max) {
  22.     int currentValue;
  23.     bool isNotValid = true;
  24.     do {
  25.         cin >> currentValue;
  26.         if (currentValue >= min && currentValue <= max)
  27.             isNotValid = false;
  28.         else
  29.             cout << "Введите число в заданном диапазоне\n";
  30.     } while (isNotValid);
  31.     return currentValue;
  32. }
  33.  
  34. pair<int, int> userInputFromConsole() {
  35.     const int MIN_SIZE = 2;
  36.     const int MAX_SIZE = 100;
  37.     int n, m;
  38.     cout << "Введите размерность матрицы в диапазоне " << MIN_SIZE << ".." << MAX_SIZE << ": ";
  39.     n = inputValue(MIN_SIZE, MAX_SIZE);
  40.     m = inputValue(MIN_SIZE, MAX_SIZE);
  41.     return make_pair(n, m);
  42. }
  43.  
  44. short** userInputMatrixFromConsole(pair<int, int> size) {
  45.     const int MIN_VALUE = 0;
  46.     const int MAX_VALUE = 1;
  47.     int n = size.first;
  48.     int m = size.second;
  49.     cout << "Введите элементы матрицы в диапазоне " << MIN_VALUE << ".." << MAX_VALUE << ": " << endl;
  50.     short** matrix = new short* [n];
  51.     for (int i = 0; i < n; i++) {
  52.         matrix[i] = new short[m];
  53.         for (int j = 0; j < m; j++)
  54.             matrix[i][j] = inputValue(MIN_VALUE, MAX_VALUE);
  55.     }
  56.     return matrix;
  57. }
  58.  
  59. short** userInputFromFile(string path) {
  60.     int n, m;
  61.     ifstream file(path);
  62.     file.open(path);
  63.     file.clear();
  64.     file >> n;
  65.     file >> m;
  66.     short** matrix = new short*[n];
  67.     for (int i = 0; i < n; i++) {
  68.         matrix[i] = new short[m];
  69.         for (int j = 0; j < m; j++)
  70.             file >> matrix[i][j];
  71.     }
  72.     file.close();
  73.     return matrix;
  74. }
  75.  
  76. bool checkPath(string path) {
  77.     ifstream file(path);
  78.     if (file.is_open()) {
  79.         cout << path << " найден" << endl;
  80.         return true;
  81.     }
  82.     else {
  83.         cout << path << " не найден" << endl;
  84.         return false;
  85.     }
  86. }
  87.  
  88. string userInputPath() {
  89.     string path;
  90.     bool isNotValid = false;
  91.     do {
  92.         cout << "Введите абсолютный путь к файлу с входными данными" << endl;
  93.         cin >> path;
  94.     } while (!checkPath(path));
  95.     return path;
  96. }
  97.  
  98. short inputMethod() {
  99.     short method;
  100.     cout << "Каким способом хотите ввести данные?" << endl;
  101.     cout << "1 - с помощью консоли" << endl;
  102.     cout << "2 - с помощью файла" << endl;
  103.     do {
  104.         cin >> method;
  105.         if (method != 1 && method != 2)
  106.             cout << "Введите 1 или 2" << endl;
  107.     } while (method != 2 && method != 1);
  108.     return method;
  109. }
  110.  
  111. void printInConsole(short** a, short** b) {
  112.     cout << "Исходная матрица" << endl;
  113.     for (int i = 0; i < _msize(a) / sizeof(a); i++) {
  114.         for (int j = 0; j < _msize(a[0]) / sizeof(a[0][0]); j++)
  115.             cout << a[i][j] << " ";
  116.         cout << endl;
  117.     }
  118.     cout << endl;
  119.     cout << "Найденная подматрица" << endl;
  120.     for (int i = 0; i < _msize(a) / sizeof(a); i++) {
  121.         for (int j = 0; j < _msize(a[0]) / sizeof(a[0][0]); j++)
  122.             cout << b[i][j] << " ";
  123.         cout << endl;
  124.     }
  125. }
  126.  
  127. string userOutputPath() {
  128.     string path;
  129.     cout << "Введите абсолютный путь к файлу для вывода результата" << endl;
  130.     cin >> path;
  131.     return path;
  132. }
  133.  
  134. void printInFile(short** a, short** b, string path) {
  135.     ofstream file(path);
  136.     for (int i = 0; i < _msize(a) / sizeof(a); i++) {
  137.         for (int j = 0; j < _msize(a[0]) / sizeof(a[0][0]); j++)
  138.             file << a[i][j] << " ";
  139.         file << endl;
  140.     }
  141.     file << endl;
  142.     for (int i = 0; i < _msize(a) / sizeof(a); i++) {
  143.         for (int j = 0; j < _msize(a[0]) / sizeof(a[0][0]); j++)
  144.             file << b[i][j] << " ";
  145.         file << endl;
  146.     }
  147.     cout << "Результат работа помещён в файл";
  148. }
  149.  
  150. short outputMethod() {
  151.     short method;
  152.     cout << "Куда хотите вывести результат?" << endl;
  153.     cout << "1 - в консоль" << endl;
  154.     cout << "2 - в файл" << endl;
  155.     do {
  156.         cin >> method;
  157.         if (method != 1 && method != 2)
  158.             cout << "Введите 1 или 2" << endl;
  159.     } while (method != 2 && method != 1);
  160.     return method;
  161. }
  162.  
  163. short** userInput() {
  164.     pair<int, int> size;
  165.     short** matrix;
  166.     short method = inputMethod();
  167.     if (method == 1) {
  168.         size = userInputFromConsole();
  169.         matrix = userInputMatrixFromConsole(size);
  170.     }
  171.     else {
  172.         string path = userInputPath();
  173.         matrix = userInputFromFile(path);
  174.     }
  175.     return matrix;
  176. }
  177.  
  178. void printResult(short** a, short** b) {
  179.     short method = outputMethod();
  180.     if (method == 1)
  181.         printInConsole(a, b);
  182.     else {
  183.         string path = userOutputPath();
  184.         printInFile(a, b, path);
  185.     }
  186. }
  187.  
  188. short** findMatrix(short** matrix) {
  189.     int n = _msize(matrix) / sizeof(matrix);
  190.     int m = _msize(matrix[0]) / sizeof(matrix[0][0]);
  191.     short** b = new short* [_msize(matrix) / sizeof(matrix)];
  192.     for (int i = 0; i < _msize(b) / sizeof(b); i++) {
  193.         b[i] = new short[_msize(matrix[0]) / sizeof(matrix[0][0])];
  194.         for (int j = 0; j < _msize(matrix[0]) / sizeof(matrix[0][0]); j++)
  195.             b[i][j] = 0;
  196.     }
  197.     vector<int> a, x1, x2, y1, y2;
  198.     for (int i = 0; i < n; i++)
  199.         for (int j = 0; j < m; j++)
  200.             if (matrix[i][j] == 1) {
  201.                 int dlin = 0;
  202.                 int minShir = 0;
  203.                 int c = i;
  204.                 int k = j;
  205.                 while (matrix[c][k] == 1) {
  206.                     int shir = 0;
  207.                     while (matrix[c][k] == 1) {
  208.                         k++;
  209.                         shir++;
  210.                         if (minShir != 0)
  211.                             if (shir > minShir) {
  212.                                 shir--;
  213.                                 break;
  214.                             }
  215.                         if (k == m) {
  216.                             k--;
  217.                             break;
  218.                         }
  219.                     }
  220.                     if (minShir > shir || minShir == 0)
  221.                         minShir = shir;
  222.                     dlin++;
  223.                     a.push_back(dlin * minShir);
  224.                     x1.push_back(i);
  225.                     x2.push_back(c);
  226.                     y1.push_back(j);
  227.                     y2.push_back(y1[y1.size() - 1] + minShir - 1);
  228.                     k = j;
  229.                     c++;
  230.                     if (c == n) {
  231.                         c--;
  232.                         break;
  233.                     }
  234.                 }
  235.             }
  236.  
  237.     int maxId = 0;
  238.     for (int i = 0; i < a.size(); i++)
  239.         if (a[i] > a[maxId])
  240.             maxId = i;
  241.     for (int i = x1[maxId]; i < x2[maxId] + 1; i++)
  242.         for (int j = y1[maxId]; j < y2[maxId] + 1; j++)
  243.             b[i][j] = 1;
  244.     return b;
  245. }  
  246.  
  247. void printTask() {
  248.     cout << "Данная программа находит максимальную подматрицу из единиц" << endl;
  249. }
  250.  
  251. void start() {
  252.     printTask();
  253.     short** matrix = userInput();
  254.     short** newMatrix = findMatrix(matrix);
  255.     printResult(matrix, newMatrix);
  256. }
  257.  
  258. int main()
  259. {
  260.     setlocale(LC_ALL, "Russian");
  261.     start();
  262. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement