Advertisement
believe_me

Untitled

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