Advertisement
believe_me

Untitled

Oct 24th, 2021
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.33 KB | None | 0 0
  1. #include <fstream>
  2. #include <stdio.h>
  3. #include <string>
  4. #include <iostream>
  5.  
  6. int numberOfLines, numberOfColumns;
  7.  
  8. using namespace std;
  9.  
  10. int inputNumber() {
  11.     const int MAX_NUMBER = 1000;
  12.     bool isIncorrect;
  13.     int number;
  14.     string input = "";
  15.     do {
  16.         getline(cin, input);
  17.         isIncorrect = false;
  18.         try {
  19.             number = stoi(input);
  20.         }
  21.         catch (invalid_argument ex) {
  22.             cout << "Нужно ввести целое число.\n";
  23.             isIncorrect = true;
  24.         }
  25.         catch (out_of_range ex) {
  26.             cout << "Нужно ввести целое число, которое по модулю не больше " << MAX_NUMBER << "\n";
  27.             isIncorrect = true;
  28.         }
  29.         if (!isIncorrect && abs(number) > MAX_NUMBER) {
  30.             cout << "Нужно ввести число, которое по модулю не больше " << MAX_NUMBER << "\n";
  31.             isIncorrect = true;
  32.         }
  33.     } while (isIncorrect);
  34.     return number;
  35. }
  36.  
  37. int chooseWayOfInput() {
  38.     int userWay;
  39.     do {
  40.         cout << "Выберите способ ввода: \nНажмите '1', если хотите ввести матрицу через консоль.\nНажмите '2', если хотите считать матрицу из файла.\n";
  41.         userWay = inputNumber();
  42.     } while (userWay != 1 && userWay != 2);
  43.     return userWay;
  44. }
  45.  
  46. string inputPathToFile() {
  47.     string path;
  48.     bool isIncorrect;
  49.     cout << "Введите путь к файлу:\n";
  50.     do {
  51.         isIncorrect = false;
  52.         cin >> path;
  53.         ifstream fin(path);
  54.         if (!fin.is_open())
  55.         {
  56.             cout << "Файл не найден. ";
  57.             isIncorrect = false;
  58.         }
  59.         else {
  60.             fin.close();
  61.         }
  62.     } while (isIncorrect);
  63.     return path;
  64. }
  65.  
  66. void getNumberOfLinesFromFile(string path)
  67. {
  68.     bool isIncorrect;
  69.     do {
  70.         isIncorrect = false;
  71.         ifstream fin(path);
  72.         fin >> numberOfLines;
  73.         if (numberOfLines < 1)
  74.         {
  75.             isIncorrect = true;
  76.             cout << "Некорректные данные в файле.";
  77.             path = inputPathToFile();
  78.         }
  79.         fin.close();
  80.     } while (isIncorrect);
  81. }
  82.  
  83. void getNumberOfColumnsFromFile(string path)
  84. {
  85.     bool isIncorrect;
  86.     do {
  87.         isIncorrect = false;
  88.         ifstream fin(path);
  89.         fin.ignore(1);
  90.         fin >> numberOfColumns;
  91.         if (numberOfColumns < 1)
  92.         {
  93.             isIncorrect = true;
  94.             cout << "Некорректные данные в файле.";
  95.             path = inputPathToFile();
  96.         }
  97.         fin.close();
  98.     } while (isIncorrect);
  99. }
  100.  
  101. int** getMatrixFromFile(string path, int numberOfLines, int numberOfColumns, int** matrix)
  102. {
  103.     ifstream fin(path);
  104.     fin.ignore(4);
  105.     for (int i = 0; i < numberOfLines; i++)
  106.     {
  107.         for (int j = 0; j < numberOfColumns; j++)
  108.         {
  109.             fin >> matrix[i][j];
  110.         }
  111.     }
  112.     fin.close();
  113.     return matrix;
  114. }
  115.  
  116. int** matrixCreating(int numberOfLines, int numberOfColumns)
  117. {
  118.     int** matrix = new int* [numberOfLines];
  119.     for (int i = 0; i < numberOfLines; i++)
  120.     {
  121.         matrix[i] = new int[numberOfColumns];
  122.     }
  123.     return matrix;
  124. }
  125.  
  126. int** inputMatrixFromConsole(int** matrix, int numberOfLines, int numberOfColumns)
  127. {
  128.     for (int i = 0; i < numberOfLines; i++)
  129.     {
  130.         for (int j = 0; j < numberOfColumns; j++)
  131.         {
  132.             cout << "Введите значение элемента[" << i + 1 << "]" << "[" << j + 1 << "]";
  133.             matrix[i][j] = inputNumber();
  134.         }
  135.     }
  136.     return matrix;
  137. }
  138.  
  139. void matrixDelete(int** matrix, int numberOfLines, int numberOfColumns)
  140. {
  141.     for (int i = 0; i < numberOfLines; i++)
  142.     {
  143.         delete[] matrix[i];
  144.     }
  145.     delete[] matrix;
  146. }
  147.  
  148. int counterOfSortedLines(int** matrix, int numberOfLines, int numberOfColumns)
  149. {
  150.     int counter, numberOfSortedLines;
  151.     numberOfSortedLines = 0;
  152.     if (numberOfColumns > 1)
  153.     {
  154.         for (int i = 0; i < numberOfLines; i++)
  155.         {
  156.             counter = 0;
  157.             for (int j = 1; j < numberOfColumns; j++)
  158.             {
  159.                 if (matrix[i][j - 1] < matrix[i][j])
  160.                 {
  161.                     counter++;
  162.                 }
  163.             }
  164.             if (counter == numberOfColumns - 1)
  165.             {
  166.                 numberOfSortedLines++;
  167.             }
  168.         }
  169.     }
  170.     return numberOfSortedLines;
  171. }
  172.  
  173. void resaultOutput(int numberOfSortedLines)
  174. {
  175.     cout << "Количество строк, отсортированных по возрастанию: " << numberOfSortedLines << endl;
  176. }
  177.  
  178. void InputNumberOfLines()
  179. {
  180.     bool isIncorrect;
  181.     cout << "Введите количество строк:\n";
  182.     do {
  183.         isIncorrect = false;
  184.         numberOfLines = inputNumber();
  185.         if (numberOfLines < 1)
  186.         {
  187.             isIncorrect = true;
  188.             cout << "Количество строк должно быть положительным:\n";
  189.         }
  190.     } while (isIncorrect);
  191. }
  192.  
  193. void InputNumberOfColumns()
  194. {
  195.     bool isIncorrect;
  196.     cout << "Введите количество столбцов.\n";
  197.     do {
  198.         isIncorrect = false;
  199.         numberOfColumns = inputNumber();
  200.         if (numberOfColumns < 1)
  201.         {
  202.             isIncorrect = true;
  203.             cout << "Количество столбцов должно быть положительным.\n";
  204.         }
  205.     } while (isIncorrect);
  206. }
  207.  
  208. int** getMatrix(int userWay)
  209. {
  210.     string path;
  211.     switch (userWay)
  212.     {
  213.     case 1:
  214.     {
  215.         InputNumberOfLines();
  216.         InputNumberOfColumns();
  217.         break;
  218.     }
  219.     case 2:
  220.     {
  221.         path = inputPathToFile();
  222.         getNumberOfLinesFromFile(path);
  223.         getNumberOfColumnsFromFile(path);
  224.         break;
  225.     }
  226.     }
  227.     int** matrix = matrixCreating(numberOfLines, numberOfColumns);
  228.     switch (userWay)
  229.     {
  230.     case 1:
  231.         matrix = inputMatrixFromConsole(matrix, numberOfLines, numberOfColumns);
  232.         break;
  233.     case 2:
  234.         matrix = getMatrixFromFile(path, numberOfLines, numberOfColumns, matrix);
  235.         break;
  236.     }
  237.     return matrix;
  238. }
  239.  
  240. void checkExtension(string* path)
  241. {
  242.     bool isIncorrect;
  243.     int lastIndex;
  244.     do {
  245.         isIncorrect = false;
  246.         lastIndex = (*path).length() - 1;
  247.         if ((*path)[lastIndex] != 't' || (*path)[lastIndex - 1] != 'x' || (*path)[lastIndex - 2] != 't' || (*path)[lastIndex - 3] != '.')
  248.         {
  249.             isIncorrect = true;
  250.             cout << "Файл имеет неверное расширение. ";
  251.             *path = inputPathToFile();
  252.         }
  253.     } while (isIncorrect);
  254. }
  255.  
  256. void printResultInFile(string path, int numberOfSortedLines)
  257. {
  258.     bool isIncorrect;
  259.     do {
  260.         isIncorrect = false;
  261.         checkExtension(&path);
  262.         ofstream fout(path, ios::trunc);
  263.         if (fout.is_open()) {
  264.             fout << "Количество отсортированных по возрастанию строк: " << numberOfSortedLines << endl;
  265.             fout.close();
  266.         }
  267.         else {
  268.             cout << "Файл закрыт для записи. \n";
  269.             isIncorrect = true;
  270.             path = inputPathToFile();
  271.         }
  272.     } while (isIncorrect);
  273. }
  274.  
  275. void userWayOfOutput(int numberOfSortedLines)
  276. {
  277.     char userWay;
  278.     cout << "Если хотите записать результат в файл, введите '1'. Если не хотите - введите другой символ:\n";
  279.     cin >> userWay;
  280.     if (userWay == '1')
  281.     {
  282.         string path = inputPathToFile();
  283.         printResultInFile(path, numberOfSortedLines);
  284.         cout << "Результат записан в файл. \n";
  285.     }
  286. }
  287.  
  288. int main()
  289. {
  290.     setlocale(LC_ALL, "Russian");
  291.     cout << "Программа считает количество строк данной матрицы, которые упорядочены по возрастанию.\n";
  292.     int userWay;
  293.     userWay = chooseWayOfInput();
  294.     int** matrix = getMatrix(userWay);
  295.     int numberOfSortedLines;
  296.     numberOfSortedLines = counterOfSortedLines(matrix, numberOfLines, numberOfColumns);
  297.     matrixDelete(matrix, numberOfLines, numberOfColumns);
  298.     userWayOfOutput(numberOfSortedLines);
  299.     resaultOutput(numberOfSortedLines);
  300.     cout << "Программа завершена";
  301.     return 0;
  302. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement