Advertisement
MadCortez

Untitled

Nov 19th, 2020
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.07 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <set>
  5.  
  6. using namespace std;
  7.  
  8. int getInputValue(int min, int max);
  9. int getUserInputFromConsole();
  10. int getUserInputFromFile(string path);
  11. bool checkPath(string path);
  12. string getUserInputPath();
  13. int getInputMethod();
  14. void printInConsole(set<int> mySet);
  15. string getUserOutputPath();
  16. void printInFile(string path, set<int> mySet);
  17. int getOutputMethod();
  18. void start();
  19. set<int> eratosfen(set<int> mySet);
  20. set<int> fillSet(int n);
  21. void printTask();
  22.  
  23. const int USE_CONSOLE = 1;
  24. const int USE_FILE = 2;
  25.  
  26. int getInputValue(int min, int max) {
  27.     int currentValue;
  28.     bool isNotValid = true;
  29.     do {
  30.         cin >> currentValue;
  31.         if (currentValue >= min && currentValue <= max)
  32.             isNotValid = false;
  33.         else
  34.             cout << "Введите число в заданном диапазоне\n";
  35.     } while (isNotValid);
  36.     return currentValue;
  37. }
  38.  
  39. int getUserInputFromConsole() {
  40.     const int MIN_SIZE = 2;
  41.     const int MAX_SIZE = 255;
  42.     int num;
  43.     cout << "Введите число, до которого нужно найти все простые числа в диапазоне " << MIN_SIZE << ".." << MAX_SIZE << ": ";
  44.     num = getInputValue(MIN_SIZE, MAX_SIZE);
  45.     return num;
  46. }
  47.  
  48. set<int> eratosfen(set<int> mySet) {
  49.     set<int> ::iterator it = mySet.begin();
  50.     for (int i = 1; it != mySet.end(); i++, it++) {
  51.         set<int> ::iterator it1 = mySet.begin();
  52.         for (int j = 0; j < i; j++)
  53.             it1++;
  54.         for (int j = i + 1; it1 != mySet.end(); j++) {
  55.             if (*it1 % *it == 0)
  56.                 it1 = mySet.erase(it1);
  57.             else
  58.                 it1++;
  59.         }
  60.     }
  61.     return mySet;
  62. }
  63.  
  64. set<int> fillSet(int num) {
  65.     set<int> mySet;
  66.     for (int i = 2; i <= num; i++)
  67.         mySet.insert(i);
  68.     return mySet;
  69. }
  70.  
  71. int getUserInputFromFile(string path) {
  72.     int num;
  73.     ifstream file(path);
  74.     file.open(path);
  75.     file.clear();
  76.     file >> num;
  77.     file.close();
  78.     return num;
  79. }
  80.  
  81. bool checkPath(string path) {
  82.     ifstream file(path);
  83.     if (file.is_open()) {
  84.         cout << path << " найден" << endl;
  85.         return true;
  86.     }
  87.     else {
  88.         cout << path << " не найден" << endl;
  89.         return false;
  90.     }
  91. }
  92.  
  93. string getUserInputPath() {
  94.     string path;
  95.     bool isNotValid = false;
  96.     do {
  97.         cout << "Введите абсолютный путь к файлу с входными данными" << endl;
  98.         cin >> path;
  99.     } while (!checkPath(path));
  100.     return path;
  101. }
  102.  
  103. int getInputMethod() {
  104.     int method;
  105.     cout << "Каким способом хотите ввести данные?" << endl;
  106.     cout << "1 - с помощью консоли" << endl;
  107.     cout << "2 - с помощью файла" << endl;
  108.     do {
  109.         cin >> method;
  110.         if (method != USE_CONSOLE && method != USE_FILE)
  111.             cout << "Введите 1 или 2";
  112.     } while (method != USE_CONSOLE && method != USE_FILE);
  113.     return method;
  114. }
  115.  
  116. void printInConsole(set<int> mySet) {
  117.     for (auto it = mySet.begin(); it != mySet.end(); ++it)
  118.         cout << *it << " ";
  119. }
  120.  
  121. string getUserOutputPath() {
  122.     string path;
  123.     cout << "Введите абсолютный путь к файлу для вывода результата" << endl;
  124.     cin >> path;
  125.     cout << "Результат работа помещён в файл";
  126.     return path;
  127. }
  128.  
  129. void printInFile(string path, set<int> mySet) {
  130.     ofstream file(path);
  131.     for (auto it = mySet.begin(); it != mySet.end(); ++it)
  132.         file << *it << " ";
  133. }
  134.  
  135. int getOutputMethod() {
  136.     int method;
  137.     cout << "Куда хотите вывести результат?" << endl;
  138.     cout << "1 - в консоль" << endl;
  139.     cout << "2 - в файл" << endl;
  140.     do {
  141.         cin >> method;
  142.         if (method != USE_CONSOLE && method != USE_FILE)
  143.             cout << "Введите 1 или 2";
  144.     } while (method != USE_CONSOLE && method != USE_FILE);
  145.     return method;
  146. }
  147.  
  148. void printTask() {
  149.     cout << "Данная программа находит все простые числа, не превосходящие n, ";
  150.     cout << "с помощью алгоритма «решето Эратосфена»" << endl;
  151. }
  152.  
  153. int getUserInput() {
  154.     int num;
  155.     short method = getInputMethod();
  156.     if (method == USE_CONSOLE)
  157.         num = getUserInputFromConsole();
  158.     else {
  159.         string path = getUserInputPath();
  160.         num = getUserInputFromFile(path);
  161.     }
  162.     return num;
  163. }
  164.  
  165. void printResult(set<int> mySet, int num) {
  166.     short method = getOutputMethod();
  167.     if (method == USE_CONSOLE)
  168.         printInConsole(mySet);
  169.     else {
  170.         string path = getUserOutputPath();
  171.         printInFile(path, mySet);
  172.     }
  173. }
  174.  
  175. void start() {
  176.     int num;
  177.     set<int> mySet;
  178.     printTask();
  179.     num = getUserInput();
  180.     mySet = fillSet(num);
  181.     mySet = eratosfen(mySet);
  182.     printResult(mySet, num);
  183. }
  184.  
  185. int main()
  186. {
  187.     setlocale(LC_ALL, "Russian");
  188.     start();
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement