Advertisement
MadCortez

Untitled

Nov 13th, 2020
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.54 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 = 10000;
  39.     int n;
  40.     cout << "Введите кол-во элементов массива в диапазоне " << MIN_SIZE << ".." << MAX_SIZE << ": ";
  41.     n = inputValue(MIN_SIZE, MAX_SIZE);
  42.     return n;
  43. }
  44.  
  45. int* userInputArrayFromConsole(int n) {
  46.     const int MIN_VALUE = -10000;
  47.     const int MAX_VALUE = 10000;
  48.     cout << "Введите элементы массива в диапазоне " << MIN_VALUE << ".." << MAX_VALUE << ": ";
  49.     int* a = new int[n];
  50.     for (int i = 0; i < n; i++)
  51.         a[i] = inputValue(MIN_VALUE, MAX_VALUE);
  52.     return a;
  53. }
  54.  
  55. int* userInputFromFile(string path) {
  56.     int n;
  57.     ifstream file(path);
  58.     file.open(path);
  59.     file.clear();
  60.     file >> n;
  61.     int* a = new int[n];
  62.     for (int i = 0; i < n; i++)
  63.         file >> a[i];
  64.     file.close();
  65.     return a;
  66. }
  67.  
  68. bool checkPath(string path) {
  69.     ifstream file(path);
  70.     if (file.is_open()) {
  71.         cout << path << " найден" << endl;
  72.         return true;
  73.     }
  74.     else {
  75.         cout << path << " не найден" << endl;
  76.         return false;
  77.     }
  78. }
  79.  
  80. string userInputPath() {
  81.     string path;
  82.     bool isNotValid = false;
  83.     do {
  84.         cout << "Введите абсолютный путь к файлу с входными данными" << endl;
  85.         cin >> path;
  86.     } while (!checkPath(path));
  87.     return path;
  88. }
  89.  
  90. int inputMethod() {
  91.     int method;
  92.     cout << "Каким способом хотите ввести данные?" << endl;
  93.     cout << "1 - с помощью консоли" << endl;
  94.     cout << "2 - с помощью файла" << endl;
  95.     do {
  96.         cin >> method;
  97.         if (method != 1 && method != 2)
  98.             cout << "Введите 1 или 2" << endl;
  99.     } while (method != 2 && method != 1);
  100.     return method;
  101. }
  102.  
  103. void printInConsole(int** steps) {
  104.     for (int i = 0; i < _msize(steps) / sizeof(steps[0]) - 1; i++) {
  105.         for (int j = 0; j < _msize(steps) / sizeof(steps[0]); j++)
  106.             cout << steps[i][j] << " ";
  107.         cout << endl;
  108.     }
  109. }
  110.  
  111. string userOutputPath() {
  112.     string path;
  113.     cout << "Введите абсолютный путь к файлу для вывода результата" << endl;
  114.     cin >> path;
  115.     cout << "Результат работа помещён в файл";
  116.     return path;
  117. }
  118.  
  119. void printInFile(int** steps, string path) {
  120.     ofstream file(path);
  121.     for (int i = 0; i < _msize(steps) / sizeof(steps[0]); i++) {
  122.         for (int j = 0; j < _msize(steps[i]) / sizeof(steps[i][0]); j++)
  123.             file << steps[i][j] << " ";
  124.         file << endl;
  125.     }
  126. }
  127.  
  128. int outputMethod() {
  129.     int method;
  130.     cout << "Куда хотите вывести результат?" << endl;
  131.     cout << "1 - в консоль" << endl;
  132.     cout << "2 - в файл" << endl;
  133.     do {
  134.         cin >> method;
  135.         if (method != 1 && method != 2)
  136.             cout << "Введите 1 или 2" << endl;
  137.     } while (method != 2 && method != 1);
  138.     return method;
  139. }
  140.  
  141. int** sort(int* a) {
  142.     int** steps = new int* [_msize(a) / sizeof(a[0])];
  143.     for (int i = 1; i < _msize(a) / sizeof(a[0]); i++) {
  144.         int temp = a[i];
  145.         int left = 0;
  146.         int right = i - 1;
  147.         while (left <= right) {
  148.             int mid = (left + right) / 2;
  149.             if (temp < a[mid])
  150.                 right = mid - 1;
  151.             else
  152.                 left = mid + 1;
  153.         }
  154.         for (int j = i - 1; j >= left; j--)
  155.             a[j + 1] = a[j];
  156.         a[left] = temp;
  157.         steps[i - 1] = new int[_msize(a) / sizeof(a[0])];
  158.         for (int j = 0; j < _msize(a) / sizeof(a[0]); j++)
  159.             steps[i - 1][j] = a[j];
  160.     }
  161.     return steps;
  162. }
  163.  
  164. int* userInput() {
  165.     int n;
  166.     int* a;
  167.     short method = inputMethod();
  168.     if (method == 1) {
  169.         n = userInputFromConsole();
  170.         a = userInputArrayFromConsole(n);
  171.     }
  172.     else {
  173.         string path = userInputPath();
  174.         a = userInputFromFile(path);
  175.     }
  176.     return a;
  177. }
  178.  
  179. void resultPrint(int** steps) {
  180.     short method = outputMethod();
  181.     if (method == 1)
  182.         printInConsole(steps);
  183.     else {
  184.         string path = userOutputPath();
  185.         printInFile(steps, path);
  186.     }
  187. }
  188.  
  189. void printTask() {
  190.     cout << "Данная программа выполняет сортировку бинарными вставками" << endl;
  191. }
  192.  
  193. void start() {
  194.     int n;
  195.     int* a;
  196.     int** steps;
  197.     printTask();
  198.     a = userInput();
  199.     steps = sort(a);
  200.     resultPrint(steps);
  201. }
  202.  
  203. int main()
  204. {
  205.     setlocale(LC_ALL, "Russian");
  206.     start();
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement