Advertisement
believe_me

Untitled

Oct 28th, 2021
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.27 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 < 2 || numberOfLines >5)
  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 < 2 || numberOfColumns > 5)
  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.     } while (isIncorrect);
  205. }
  206.  
  207. void InputNumberOfColumns()
  208. {
  209.     bool isIncorrect;
  210.     cout << "Введите количество столбцов.\n";
  211.     do {
  212.         isIncorrect = false;
  213.         numberOfColumns = inputNumber(2, 5);
  214.     } while (isIncorrect);
  215. }
  216.  
  217. int** reveiveMatrix(int userWay)
  218. {
  219.     string path;
  220.     switch (userWay)
  221.     {
  222.     case 1:
  223.     {
  224.         InputNumberOfLines();
  225.         InputNumberOfColumns();
  226.         break;
  227.     }
  228.     case 2:
  229.     {
  230.         path = inputPathToFile();
  231.         reveiveNumberOfLinesFromFile(path);
  232.         reveiveNumberOfColumnsFromFile(path);
  233.         break;
  234.     }
  235.     }
  236.     int** matrix = matrixCreating(numberOfLines, numberOfColumns);
  237.     switch (userWay)
  238.     {
  239.     case 1:
  240.         matrix = inputMatrixFromConsole(matrix, numberOfLines, numberOfColumns);
  241.         break;
  242.     case 2:
  243.         matrix = reveiveMatrixFromFile(path, numberOfLines, numberOfColumns, matrix);
  244.         break;
  245.     }
  246.     return matrix;
  247. }
  248.  
  249. void printResultInFile(string path, int numberOfSortedLines)
  250. {
  251.     bool isIncorrect;
  252.     do {
  253.         isIncorrect = false;
  254.         checkExtension(&path);
  255.         ofstream fout(path, ios::trunc);
  256.         if (fout.is_open()) {
  257.             fout << "Количество отсортированных по возрастанию строк: " << numberOfSortedLines << endl;
  258.             fout.close();
  259.         }
  260.         else {
  261.             cout << "Файл закрыт для записи. \n";
  262.             isIncorrect = true;
  263.             path = inputPathToFile();
  264.         }
  265.     } while (isIncorrect);
  266. }
  267.  
  268. void userWayOfOutput(int numberOfSortedLines)
  269. {
  270.     char userWay;
  271.     cout << "Если хотите записать результат в файл, введите '1'. Если не хотите - введите другой символ:\n";
  272.     cin >> userWay;
  273.     if (userWay == '1')
  274.     {
  275.         string path = inputPathToFile();
  276.         printResultInFile(path, numberOfSortedLines);
  277.         cout << "Результат записан в файл. \n";
  278.     }
  279. }
  280.  
  281. int main()
  282. {
  283.     setlocale(LC_ALL, "Russian");
  284.     cout << "Программа считает количество строк данной матрицы, которые упорядочены по возрастанию.\n";
  285.     int userWay;
  286.     userWay = chooseWayOfInput();
  287.     int** matrix = reveiveMatrix(userWay);
  288.     int numberOfSortedLines;
  289.     numberOfSortedLines = counterOfSortedLines(matrix, numberOfLines, numberOfColumns);
  290.     matrixDelete(matrix, numberOfLines, numberOfColumns);
  291.     resaultOutput(numberOfSortedLines);
  292.     userWayOfOutput(numberOfSortedLines);
  293.     cout << "Программа завершена";
  294.     return 0;
  295. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement