redmanexe

Lab2Challenge2CPP

Oct 22nd, 2024
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.38 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. constexpr int FRIEND_NUMBERS_FIND_MAX = 32000;
  7. constexpr int FRIEND_ARRAY_SIZE = 100;
  8. constexpr int PRINT_TYPE_MIN = 0;
  9. constexpr int PRINT_TYPE_MAX = 2;
  10.  
  11. int getSumOfDivers(const int num)
  12. {
  13.     int sum = 0;
  14.     for (int i = 1; i < num; i++) {
  15.         if (num % i == 0)
  16.             sum += i;
  17.     }
  18.     return sum;
  19. }
  20.  
  21. int calculate(int** friends)
  22. {
  23.     int j = 0;
  24.     for (int i = 2; i < (FRIEND_NUMBERS_FIND_MAX + 1); i++)
  25.     {
  26.         int potFriend = getSumOfDivers(i);
  27.         if (i != potFriend && i == getSumOfDivers(potFriend))
  28.         {
  29.             friends[0][j] = i;
  30.             friends[1][j] = potFriend;
  31.             j++;
  32.         }
  33.     }
  34.     return j;
  35. }
  36.  
  37. bool isTextFile(const string &filePath)
  38. {
  39.     bool isTxt;
  40.     isTxt = (filePath.length() > 4 &&
  41.                 filePath[filePath.length() - 4] == '.' &&
  42.                 filePath[filePath.length() - 3] == 't' &&
  43.                 filePath[filePath.length() - 2] == 'x' &&
  44.                 filePath[filePath.length() - 1] == 't');
  45.  
  46.     return isTxt;
  47. }
  48. bool checkFileAvailability(const string &filePath, const bool read)
  49. {
  50.     ios::openmode mode;
  51.     if (read)
  52.         mode = ifstream::in;
  53.     else
  54.         mode = ifstream::app;
  55.     fstream file(filePath, mode);
  56.     bool available;
  57.     available = file.good();
  58.     file.close();
  59.     if (available && !isTextFile(filePath))
  60.         available = false;
  61.  
  62.     return available;
  63. }
  64. int takeIntValueFromConsole(const string& text)
  65. {
  66.     bool isInCorrect;
  67.     int value;
  68.     isInCorrect = true;
  69.     while (isInCorrect)
  70.     {
  71.         cout << text;
  72.         cin >> value;
  73.         isInCorrect = false;
  74.         if (cin.fail())
  75.         {
  76.             cout << "Введите число, а не строку или что-то иное!\n";
  77.             cin.clear();
  78.             cin.ignore(numeric_limits<streamsize>::max(), '\n');
  79.             isInCorrect = true;
  80.         }
  81.     }
  82.  
  83.     return value;
  84. }
  85. int takeIntValueInRangeFromConsole(const string& text, const int min, const int max)
  86. {
  87.     bool isInCorrect;
  88.     int value;
  89.     isInCorrect = true;
  90.     value = 0;
  91.     while (isInCorrect)
  92.     {
  93.         value = takeIntValueFromConsole(text);
  94.         isInCorrect = false;
  95.         if (value < min || value > max)
  96.         {
  97.             cout << "Число должно находится в границах от " << min << " до " << max << "\n";
  98.             cin.clear();
  99.             cin.ignore(numeric_limits<streamsize>::max(), '\n');
  100.             isInCorrect = true;
  101.         }
  102.     }
  103.  
  104.     return value;
  105. }
  106. string takeCorrectFile(const bool input)
  107. {
  108.     bool isInCorrect;
  109.     string value;
  110.     isInCorrect = true;
  111.     while(isInCorrect) {
  112.         if (input)
  113.             cout << "Введите путь до входного файла: ";
  114.         else
  115.             cout << "Введите путь до выходного файла: ";
  116.         cin >> value;
  117.         isInCorrect = false;
  118.         if (!checkFileAvailability(value, input)) {
  119.             isInCorrect = true;
  120.             cout << "Путь ведёт до файла, который недоступен или который не является текстовым файлом!\n";
  121.         }
  122.     }
  123.  
  124.     return value;
  125. }
  126. bool saveResultIntoFile(const string &filePath, int** friends, const int length)
  127. {
  128.     bool saved;
  129.     saved = true;
  130.     ofstream file(filePath);
  131.     if (file.good())
  132.     {
  133.         for (int i = 0; i < length; i++)
  134.             file << friends[0][i] << " " << friends[1][i] << "\n";
  135.     }
  136.     else
  137.         saved = false;
  138.     file.close();
  139.  
  140.     return saved;
  141. }
  142. void printResultIntoConsole(int** friends, const int length)
  143. {
  144.     cout << "\nДружественные числа до " << FRIEND_NUMBERS_FIND_MAX << ":\n";
  145.     for (int i = 0; i < length; i++)
  146.         cout << friends[0][i] << " " << friends[1][i] << "\n";
  147.     cout << "\n";
  148. }
  149. void printResult(int** friends, const int length)
  150. {
  151.     bool saved;
  152.     int type;
  153.     cout << "\nКуда вывести результат решения?\n";
  154.     cout << "0 - Только консоль\n";
  155.     cout << "1 - Только в файл\n";
  156.     cout << "2 - И в файл, и в консоль\n";
  157.     type = takeIntValueInRangeFromConsole("Выбранный вариант вывода: ", PRINT_TYPE_MIN, PRINT_TYPE_MAX);
  158.     saved = false;
  159.     string output;
  160.     switch (type)
  161.     {
  162.         case 0:
  163.             printResultIntoConsole(friends, length);
  164.         break;
  165.         case 1:
  166.             output = takeCorrectFile(false);
  167.             saved = saveResultIntoFile(output, friends, length);
  168.         break;
  169.         case 2:
  170.             output = takeCorrectFile(false);
  171.             saved = saveResultIntoFile(output, friends, length);
  172.             printResultIntoConsole(friends, length);
  173.         break;
  174.     }
  175.  
  176.     if (saved)
  177.         cout << "Результат записан в выходной файл!" << "\n";
  178. }
  179.  
  180. int main()
  181. {
  182.     cout << "2. Вывести все дружественные числа до 32000.\n";
  183.     int** friends = new int*[2];
  184.     friends[0] = new int[FRIEND_ARRAY_SIZE];
  185.     friends[1] = new int[FRIEND_ARRAY_SIZE];
  186.     int length;
  187.     length = calculate(friends);
  188.     printResult(friends, length);
  189.  
  190.     delete[] friends[0];
  191.     delete[] friends[1];
  192.     delete[] friends;
  193.  
  194.     return 0;
  195. }
Tags: cpp
Advertisement
Add Comment
Please, Sign In to add comment