Advertisement
MadCortez

Untitled

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