Advertisement
MadCortez

Untitled

Nov 14th, 2020
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.90 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> mySet);
  15. string userOutputPath();
  16. void printInFile(string path, set<int> mySet);
  17. int outputMethod();
  18. void start();
  19. set<int> eratosfen(set<int> mySet);
  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.             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 num;
  40.     cout << "Введите число, до которого нужно найти все простые числа в диапазоне " << MIN_SIZE << ".." << MAX_SIZE << ": ";
  41.     num = inputValue(MIN_SIZE, MAX_SIZE);
  42.     return num;
  43. }
  44.  
  45. set<int> eratosfen(set<int> mySet) {
  46.     set<int> ::iterator it = mySet.begin();
  47.     for (int i = 1; it != mySet.end(); i++, it++) {
  48.         set<int> ::iterator it1 = mySet.begin();
  49.         for (int j = 0; j < i; j++)
  50.             it1++;
  51.         for (int j = i + 1; it1 != mySet.end(); j++) {
  52.             if (*it1 % *it == 0)
  53.                 it1 = mySet.erase(it1);
  54.             else
  55.                 it1++;
  56.         }
  57.     }
  58.     return mySet;
  59. }
  60.  
  61. set<int> fillSet(int num) {
  62.     set<int> mySet;
  63.     for (int i = 2; i <= num; i++)
  64.         mySet.insert(i);
  65.     return mySet;
  66. }
  67.  
  68. int userInputFromFile(string path) {
  69.     int num;
  70.     ifstream file(path);
  71.     file.open(path);
  72.     file.clear();
  73.     file >> num;
  74.     file.close();
  75.     return num;
  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> mySet) {
  114.     for (auto it = mySet.begin(); it != mySet.end(); ++it)
  115.         cout << *it << " ";
  116. }
  117.  
  118. string userOutputPath() {
  119.     string path;
  120.     cout << "Введите абсолютный путь к файлу для вывода результата" << endl;
  121.     cin >> path;
  122.     cout << "Результат работа помещён в файл";
  123.     return path;
  124. }
  125.  
  126. void printInFile(string path, set<int> mySet) {
  127.     ofstream file(path);
  128.     for (auto it = mySet.begin(); it != mySet.end(); ++it)
  129.         file << *it << " ";
  130. }
  131.  
  132. int outputMethod() {
  133.     int method;
  134.     cout << "Куда хотите вывести результат?" << endl;
  135.     cout << "1 - в консоль" << endl;
  136.     cout << "2 - в файл" << endl;
  137.     do {
  138.         cin >> method;
  139.         if (method != 1 && method != 2)
  140.             cout << "Введите 1 или 2";
  141.     } while (method != 2 && method != 1);
  142.     return method;
  143. }
  144.  
  145. void printTask() {
  146.     cout << "Данная программа находит все простые числа, не превосходящие n, ";
  147.     cout << "с помощью алгоритма «решето Эратосфена»" << endl;
  148. }
  149.  
  150. int userInput() {
  151.     int num;
  152.     short method = inputMethod();
  153.     if (method == 1)
  154.         num = userInputFromConsole();
  155.     else {
  156.         string path = userInputPath();
  157.         num = userInputFromFile(path);
  158.     }
  159.     return num;
  160. }
  161.  
  162. void resultPrint(set<int> mySet, int num) {
  163.     short method = outputMethod();
  164.     if (method == 1)
  165.         printInConsole(mySet);
  166.     else {
  167.         string path = userOutputPath();
  168.         printInFile(path, mySet);
  169.     }
  170. }
  171.  
  172. void start() {
  173.     int num;
  174.     set<int> mySet;
  175.     printTask();
  176.     num = userInput();
  177.     mySet = fillSet(num);
  178.     mySet = eratosfen(mySet);
  179.     resultPrint(mySet, num);
  180. }
  181.  
  182. int main()
  183. {
  184.     setlocale(LC_ALL, "Russian");
  185.     start();
  186. }
  187. //D:\input.txt
  188. //D:\output.txt
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement