Advertisement
MadCortez

Untitled

Mar 27th, 2021
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.61 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. struct Node {
  8.     int data;
  9.     Node* next;
  10. };
  11.  
  12. int inputValue(int min, int max);
  13. pair<int, int> userInputFromConsole();
  14. pair<int, int> userInputFromFile(string path);
  15. bool checkPath(string path);
  16. string userInputPath();
  17. short inputMethod();
  18. void printInConsole(string ans);
  19. string userOutputPath();
  20. void printInFile(string ans, string path);
  21. short outputMethod();
  22. void start();
  23. void printTask();
  24.  
  25. int inputValue(int min, int max) {
  26.     int currentValue;
  27.     bool isNotValid = true;
  28.     do {
  29.         cin >> currentValue;
  30.         if (currentValue >= min && currentValue <= max)
  31.             isNotValid = false;
  32.         else
  33.             cout << "Введите число в заданном диапазоне\n";
  34.     } while (isNotValid);
  35.     return currentValue;
  36. }
  37.  
  38. pair<int, int> userInputFromConsole() {
  39.     const int MIN_SIZE = 1;
  40.     const int MAX_SIZE = 20;
  41.     int n, m;
  42.     cout << "Введите N и M в диапазоне " << MIN_SIZE << ".." << MAX_SIZE << ": ";
  43.     n = inputValue(MIN_SIZE, MAX_SIZE);
  44.     m = inputValue(MIN_SIZE, MAX_SIZE);
  45.     return make_pair(n, m);
  46. }
  47.  
  48. pair<int, int> userInputFromFile(string path) {
  49.     int n, m;
  50.     ifstream file(path);
  51.     file.open(path);
  52.     file.clear();
  53.     file >> n;
  54.     file >> m;
  55.     file.close();
  56.     return make_pair(n, m);
  57. }
  58.  
  59. bool checkPath(string path) {
  60.     ifstream file(path);
  61.     if (file.is_open()) {
  62.         cout << path << " найден" << endl;
  63.         return true;
  64.     }
  65.     else {
  66.         cout << path << " не найден" << endl;
  67.         return false;
  68.     }
  69. }
  70.  
  71. string userInputPath() {
  72.     string path;
  73.     bool isNotValid = false;
  74.     do {
  75.         cout << "Введите абсолютный путь к файлу с входными данными" << endl;
  76.         cin >> path;
  77.     } while (!checkPath(path));
  78.     return path;
  79. }
  80.  
  81. short inputMethod() {
  82.     short method;
  83.     cout << "Каким способом хотите ввести данные?" << endl;
  84.     cout << "1 - с помощью консоли" << endl;
  85.     cout << "2 - с помощью файла" << endl;
  86.     do {
  87.         cin >> method;
  88.         if (method != 1 && method != 2)
  89.             cout << "Введите 1 или 2" << endl;
  90.     } while (method != 2 && method != 1);
  91.     return method;
  92. }
  93.  
  94. void printInConsole(string ans) {
  95.     cout << ans;
  96. }
  97.  
  98. string userOutputPath() {
  99.     string path;
  100.     cout << "Введите абсолютный путь к файлу для вывода результата" << endl;
  101.     cin >> path;
  102.     return path;
  103. }
  104.  
  105. void printInFile(string ans, string path) {
  106.     ofstream file(path);
  107.     file << ans;
  108.     cout << "Результат работы помещён в файл";
  109. }
  110.  
  111. short outputMethod() {
  112.     short method;
  113.     cout << "Куда хотите вывести результат?" << endl;
  114.     cout << "1 - в консоль" << endl;
  115.     cout << "2 - в файл" << endl;
  116.     do {
  117.         cin >> method;
  118.         if (method != 1 && method != 2)
  119.             cout << "Введите 1 или 2" << endl;
  120.     } while (method != 2 && method != 1);
  121.     return method;
  122. }
  123.  
  124. pair<int, int> userInput() {
  125.     pair<int, int> size;
  126.     short method = inputMethod();
  127.     if (method == 1) {
  128.         size = userInputFromConsole();
  129.     }
  130.     else {
  131.         string path = userInputPath();
  132.         size = userInputFromFile(path);
  133.     }
  134.     return size;
  135. }
  136.  
  137. void printResult(string ans) {
  138.     short method = outputMethod();
  139.     if (method == 1)
  140.         printInConsole(ans);
  141.     else {
  142.         string path = userOutputPath();
  143.         printInFile(ans, path);
  144.     }
  145. }
  146.  
  147. Node* add(Node *head, Node *list, int info) {
  148.     Node* addList = (Node*)malloc(sizeof(Node));
  149.     addList->data = info;
  150.     addList->next = NULL;
  151.     if (head == NULL) {
  152.         head = addList;
  153.         list = addList;
  154.     }
  155.     else {
  156.         list = head;
  157.         while (list->next != NULL)
  158.             list = list->next;
  159.         list->next = addList;
  160.     }
  161.     return head;
  162. }
  163.  
  164. Node* deleteList(Node* list, Node* head) {
  165.     Node* temp = head;
  166.     while (temp->next != list)
  167.         temp = temp->next;
  168.     temp->next = temp->next->next;
  169.     return temp->next;
  170. }
  171.  
  172. Node* createList(int n) {
  173.     Node* list = NULL;
  174.     Node* head = NULL;
  175.     for (int i = 0; i < n; i++) {
  176.         list = add(list, list, i + 1);
  177.     }
  178.     head = list;
  179.     while (list->next != NULL)
  180.         list = list->next;
  181.     list->next = head;
  182.     list = head;
  183.     return list;
  184. }
  185.  
  186. void printTask() {
  187.     cout << "N ребят встали в круг. Каждый раз, начиная с первого, из круга выводится каждый M-й. Вывести номера в порядке выбывания и номер последнего оставшегося." << endl;
  188. }
  189.  
  190. void start() {
  191.     printTask();
  192.     pair<int, int> size = userInput();
  193.     int n = size.first;
  194.     int m = size.second;
  195.     Node* list = createList(n);
  196.     Node* head = list;
  197.     int now = 0;
  198.     string ans = "Номера в порядке выбывания: \n";
  199.     while (n > 1) {
  200.         now++;
  201.         if (now == m) {
  202.             ans += to_string(list->data) + " ";
  203.             list = deleteList(list, head);
  204.             head = list;
  205.             now = 0;
  206.             n--;
  207.         }
  208.         else
  209.             list = list->next;
  210.     }
  211.     ans += "\nНомер последнего оставшегося: " + to_string(list->data);
  212.     printResult(ans);
  213. }
  214.  
  215. int main()
  216. {
  217.     setlocale(LC_ALL, "Russian");
  218.     start();
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement