Advertisement
MadCortez

Untitled

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