redmanexe

Lab2ExtraCPP

Nov 17th, 2024 (edited)
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.49 KB | None | 0 0
  1. #include <fstream>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. constexpr int PRINT_TYPE_MIN = 0;
  7. constexpr int PRINT_TYPE_MAX = 2;
  8.  
  9. constexpr int SCAN_TYPE_MIN = 0;
  10. constexpr int SCAN_TYPE_MAX = 1;
  11.  
  12. constexpr int MATRIX_LENGTH_MIN = 1;
  13. constexpr int MATRIX_LENGTH_MAX = 100;
  14.  
  15. constexpr int MATRIX_VALUES_MIN = 0;
  16. constexpr int MATRIX_VALUES_MAX = 10;
  17.  
  18. constexpr int ERR_READ_INT_VALUE = -100000000;
  19.  
  20. int calculate(int** matrix, int* goods, int m, int n)
  21. {
  22.     int nowGoods = 0;
  23.     for (int i = 0; i < m; i++) {
  24.         int j = 0;
  25.         bool isGood = true;
  26.         while (j < n && isGood) {
  27.             if (matrix[i][j] < 6 || matrix[i][j] > 8)
  28.                 isGood = false;
  29.             j++;
  30.         }
  31.  
  32.         if (isGood) {
  33.             goods[nowGoods] = i;
  34.             nowGoods++;
  35.         }
  36.     }
  37.  
  38.     return nowGoods;
  39. }
  40. bool isTextFile(const string &filePath)
  41. {
  42.     bool isTxt;
  43.     isTxt = (filePath.length() > 4 &&
  44.              filePath[filePath.length() - 4] == '.' &&
  45.              filePath[filePath.length() - 3] == 't' &&
  46.              filePath[filePath.length() - 2] == 'x' &&
  47.              filePath[filePath.length() - 1] == 't');
  48.  
  49.     return isTxt;
  50. }
  51. bool checkFileAvailability(const string &filePath, const bool read)
  52. {
  53.     ios::openmode mode;
  54.     if (read)
  55.         mode = ifstream::in;
  56.     else
  57.         mode = ifstream::app;
  58.     fstream file(filePath, mode);
  59.     bool available;
  60.     available = file.good();
  61.     file.close();
  62.     if (available && !isTextFile(filePath))
  63.         available = false;
  64.  
  65.     return available;
  66. }
  67. int takeIntValueFromConsole(const string& text)
  68. {
  69.     bool isInCorrect;
  70.     int value;
  71.     isInCorrect = true;
  72.     while (isInCorrect)
  73.     {
  74.         cout << text;
  75.         cin >> value;
  76.         isInCorrect = false;
  77.         if (cin.fail())
  78.         {
  79.             cout << "Введите число, а не строку или что-то иное!\n";
  80.             cin.clear();
  81.             cin.ignore(numeric_limits<streamsize>::max(), '\n');
  82.             isInCorrect = true;
  83.         }
  84.     }
  85.  
  86.     return value;
  87. }
  88. int takeIntValueInRangeFromConsole(const string& text, const int min, const int max)
  89. {
  90.     bool isInCorrect;
  91.     int value;
  92.     isInCorrect = true;
  93.     value = 0;
  94.     while (isInCorrect)
  95.     {
  96.         value = takeIntValueFromConsole(text);
  97.         isInCorrect = false;
  98.         if (value < min || value > max)
  99.         {
  100.             cout << "Число должно находится в границах от " << min << " до " << max << "\n";
  101.             cin.clear();
  102.             cin.ignore(numeric_limits<streamsize>::max(), '\n');
  103.             isInCorrect = true;
  104.         }
  105.     }
  106.  
  107.     return value;
  108. }
  109. string takeCorrectFile(const bool input)
  110. {
  111.     bool isInCorrect;
  112.     string value;
  113.     isInCorrect = true;
  114.     while(isInCorrect) {
  115.         if (input)
  116.             cout << "Введите путь до входного файла: ";
  117.         else
  118.             cout << "Введите путь до выходного файла: ";
  119.         cin >> value;
  120.         isInCorrect = false;
  121.         if (!checkFileAvailability(value, input)) {
  122.             isInCorrect = true;
  123.             cout << "Путь ведёт до файла, который недоступен или который не является текстовым файлом!\n";
  124.         }
  125.     }
  126.  
  127.     return value;
  128. }
  129. int takeIntValueFromFile(ifstream &reader)
  130. {
  131.     int value;
  132.     reader >> value;
  133.     if (reader.fail())
  134.         value = ERR_READ_INT_VALUE;
  135.  
  136.     return value;
  137. }
  138. int** readMatrixFromFile(int* m, int* n)
  139. {
  140.     bool isInCorrect = true;
  141.     int** matrix;
  142.     while (isInCorrect)
  143.     {
  144.         string filePath = takeCorrectFile(true);
  145.         isInCorrect = false;
  146.         ifstream file(filePath);
  147.         *m = takeIntValueFromFile(file);
  148.         *n = takeIntValueFromFile(file);
  149.         if (*m > (MATRIX_LENGTH_MIN - 1))
  150.         {
  151.             matrix = new int*[*m];
  152.             for (int i = 0; i < *m; i++)
  153.             {
  154.                 matrix[i] = new int[*n];
  155.                 for (int j = 0; j < *n; j++)
  156.                 {
  157.                     matrix[i][j] = takeIntValueFromFile(file);
  158.                     if (matrix[i][j] == ERR_READ_INT_VALUE)
  159.                         isInCorrect = true;
  160.                 }
  161.             }
  162.         }
  163.         else
  164.         {
  165.             isInCorrect = true;
  166.             cout << "Матрица не может быть размером меньше, чем " + to_string(MATRIX_LENGTH_MIN) + "x" + to_string(MATRIX_LENGTH_MIN) + "!";
  167.         }
  168.  
  169.         if (*n > MATRIX_LENGTH_MIN && isInCorrect)
  170.         {
  171.             cout << "В файле содержится неверные значения! Введите путь до файла с верным содержимым!\n";
  172.         }
  173.     }
  174.  
  175.     return matrix;
  176. }
  177. int** readMatrixFromConsole(int* m, int* n)
  178. {
  179.     *m = takeIntValueInRangeFromConsole("Введите количество учеников (значение должно быть в границах от " + to_string(MATRIX_LENGTH_MIN) + " и до " + to_string(MATRIX_LENGTH_MAX) + "): ", MATRIX_LENGTH_MIN, MATRIX_LENGTH_MAX);
  180.     *n = takeIntValueInRangeFromConsole("Введите количество отметок (значение должно быть в границах от " + to_string(MATRIX_LENGTH_MIN) + " и до " + to_string(MATRIX_LENGTH_MAX) + "): ", MATRIX_LENGTH_MIN, MATRIX_LENGTH_MAX);
  181.     int** matrix = new int*[*m];
  182.  
  183.     for (int i = 0; i < *m; i++)
  184.     {
  185.         matrix[i] = new int[*n];
  186.         for (int j = 0; j < *n; j++) {
  187.             matrix[i][j] = takeIntValueInRangeFromConsole("Отметка " + to_string(j + 1) + " человека " + to_string(i + 1) + " (значение должно быть в границах от " + to_string(MATRIX_VALUES_MIN) + " и до " + to_string(MATRIX_VALUES_MAX) + "): ", MATRIX_VALUES_MIN, MATRIX_VALUES_MAX);
  188.         }
  189.     }
  190.  
  191.     return matrix;
  192. }
  193. int** readMatrix(int* m, int* n)
  194. {
  195.     int type;
  196.     int** matrix;
  197.     cout << "\nКак считать значения для поиска решения?\n";
  198.     cout << "0 - Из ввода с клавиатуры (консоль)\n";
  199.     cout << "1 - Из файла\n";
  200.     type = takeIntValueInRangeFromConsole("Выбранный вариант ввода: ", SCAN_TYPE_MIN, SCAN_TYPE_MAX);
  201.     if (type == 1)
  202.         matrix = readMatrixFromFile(m, n);
  203.     else
  204.         matrix = readMatrixFromConsole(m, n);
  205.  
  206.     return matrix;
  207. }
  208. bool saveResultIntoFile(const string &filePath, const int* goods, const int goodsCount)
  209. {
  210.     int i;
  211.     bool saved;
  212.     saved = true;
  213.     ofstream file(filePath);
  214.     if (file.good()) {
  215.         for (i = 0; i < goodsCount; i++)
  216.             file << (goods[i] + 1) << " ";
  217.     } else
  218.         saved = false;
  219.     file.close();
  220.  
  221.     return saved;
  222. }
  223. void printResultIntoConsole(const int* goods, const int goodsCount) {
  224.     cout << "\nНомера хорошистов: \n";
  225.     for (int i = 0; i < goodsCount; i++) {
  226.         cout << (goods[i] + 1) << " ";
  227.     }
  228.     cout << "\n";
  229. }
  230. void printResult(const int* goods, const int goodsCount)
  231. {
  232.     bool saved;
  233.     int type;
  234.     cout << "\nКуда вывести результат решения?\n";
  235.     cout << "0 - Только консоль\n";
  236.     cout << "1 - Только в файл\n";
  237.     cout << "2 - И в файл, и в консоль\n";
  238.     type = takeIntValueInRangeFromConsole("Выбранный вариант вывода: ", PRINT_TYPE_MIN, PRINT_TYPE_MAX);
  239.     saved = false;
  240.     string output;
  241.     switch (type)
  242.     {
  243.         case 0:
  244.             printResultIntoConsole(goods, goodsCount);
  245.             break;
  246.         case 1:
  247.             output = takeCorrectFile(false);
  248.             saved = saveResultIntoFile(output, goods, goodsCount);
  249.             break;
  250.         case 2:
  251.             output = takeCorrectFile(false);
  252.             saved = saveResultIntoFile(output, goods, goodsCount);
  253.             printResultIntoConsole(goods, goodsCount);
  254.             break;
  255.     }
  256.  
  257.     if (saved)
  258.         cout << "Результат записан в выходной файл!" << "\n";
  259. }
  260. int main()
  261. {
  262.     int** matrix;
  263.     int* goods;
  264.     int goodsCount, m, n;
  265.     matrix = readMatrix(&m, &n);
  266.     goods = new int[m];
  267.     goodsCount = calculate(matrix, goods, m, n);
  268.     printResult(goods, goodsCount);
  269.  
  270.     for (int i = 0; i < m; i++) {
  271.         delete[] matrix[i];
  272.     }
  273.     delete[] matrix;
  274.     delete[] goods;
  275.  
  276.     return 0;
  277. }
Advertisement
Add Comment
Please, Sign In to add comment