PhotoShaman

Laba13

Feb 27th, 2017
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.61 KB | None | 0 0
  1. ----------------------------Вариант 4----------------------------
  2. #include <iostream>
  3. #include <string>
  4. #include <clocale>
  5. #include <windows.h>
  6. #include <vector>
  7.  
  8. struct books
  9. {
  10.     int UDK;
  11.     char authorName[40];
  12.     char bookName[40];
  13.     int year;
  14.     int count;
  15. };
  16.  
  17. using namespace std;
  18.  
  19. void Pause()
  20. {
  21.     cout << endl << "---Продолжить - Esc.";
  22.     while (!GetAsyncKeyState(VK_ESCAPE));
  23.     system("cls");
  24. }
  25.  
  26. int IntInput() // Проверка на Int
  27. {
  28.     int imputNumber;
  29.     while (!(cin >> imputNumber) || (cin.peek() != '\n'))
  30.     {
  31.         cin.clear();
  32.         while (cin.get() != '\n');
  33.         cout << "Это не целое число. Попробуйте еще раз.\n";
  34.     }
  35.     return imputNumber;
  36. }
  37.  
  38. void AddBook(vector <books> &Books)
  39. {
  40.     books Book;
  41.     cout << endl << "Введите УДК: ";
  42.     do
  43.     {
  44.         bool isAlreadyBe = false;
  45.         Book.UDK = IntInput();
  46.         for (int i = 0; i < (int)Books.size(); i++)
  47.             if (Books[i].UDK == Book.UDK)
  48.                 isAlreadyBe = true;
  49.         if (isAlreadyBe)
  50.             cout << endl << "Книга с таким УДК уже есть в списке. Введите другой УДК: ";
  51.         else break;
  52.     } while (true);
  53.  
  54.     cout << endl << "Введите имя автора книги: ";
  55.     cin.get(); //Типо костыль, но не совсем
  56.     cin.getline(Book.authorName, 40);
  57.     cout << endl << "Введите название книги: ";
  58.     cin.getline(Book.bookName, 40);
  59.     cout << endl << "Введите год издания книги: ";
  60.     Book.year = IntInput();
  61.     cout << endl << "Введите количество книг: ";
  62.     Book.count = IntInput();
  63.     Books.push_back(Book);
  64. }
  65.  
  66. void RemoveBook(vector <books> &Books)
  67. {
  68.     cout << "Введите индекс книги, которую нужно удалить: ";
  69.     int UDK = IntInput();
  70.     bool isHave = false;
  71.     for (int i = 0; i < (int)Books.size(); i++)
  72.     {
  73.         if (Books[i].UDK == UDK)
  74.         {
  75.             Books.erase(Books.begin() + i);
  76.             cout << "Книга удалена";
  77.             isHave = true;
  78.             break;
  79.         }
  80.     }
  81.     if (!isHave) cout << "Такой книги нет";
  82. }
  83.  
  84. void ShowBook(vector <books> &Books)
  85. {
  86.     for (int i = 0; i < (int)Books.size(); i++)
  87.     {
  88.         cout << "---Книга #" << i + 1 << endl;
  89.         cout << "УДК:         " << Books[i].UDK << endl;
  90.         cout << "Автор:       " << Books[i].authorName << endl;
  91.         cout << "Название:    " << Books[i].bookName << endl;
  92.         cout << "Год выпуска: " << Books[i].year << endl;
  93.         cout << "Количество:  " << Books[i].count << endl;
  94.     }
  95. }
  96.  
  97. void FindBooks(vector <books> &Books)
  98. {
  99.     cout << "Введите минимальное количество книг для поискового фильтра: ";
  100.     int count = IntInput();
  101.     for (int i = 0; i < (int)Books.size(); i++)
  102.     {
  103.         if (Books[i].count > count)
  104.         {
  105.             cout << "---Книга #" << i + 1 << endl;
  106.             cout << "УДК:         " << Books[i].UDK << endl;
  107.             cout << "Автор:       " << Books[i].authorName << endl;
  108.             cout << "Название:    " << Books[i].bookName << endl;
  109.             cout << "Год выпуска: " << Books[i].year << endl;
  110.             cout << "Количество:  " << Books[i].count << endl;
  111.         }
  112.     }
  113. }
  114.  
  115. int main()
  116. {
  117.     SetConsoleCP(1251);
  118.     SetConsoleOutputCP(1251);
  119.  
  120.     vector <books> Books;
  121.  
  122.     int option;
  123.     while (true)
  124.     {
  125.         system("cls");
  126.         cout << "---Выберите опцию: " << endl;
  127.         cout << "1 - Добавить книгу" << endl;
  128.         cout << "2 - Удалить книгу" << endl;
  129.         cout << "3 - Показать все книги" << endl;
  130.         cout << "4 - Найти книгу по фильтру" << endl;
  131.         cout << "5 - Выход" << endl;
  132.         option = IntInput();
  133.         system("cls");
  134.         switch (option)
  135.         {
  136.         case 1: AddBook(Books); break;
  137.         case 2: RemoveBook(Books); break;
  138.         case 3: ShowBook(Books); break;
  139.         case 4: FindBooks(Books); break;
  140.         case 5: return 0;
  141.         }
  142.         Pause();
  143.     }
  144. }
  145. ----------------------------------------Дополнительно----------------------------------------
  146. #include <iostream>
  147. #include <clocale>
  148. #include <conio.h>
  149. #include <windows.h>
  150. #include <string>
  151.  
  152. using namespace std;
  153.  
  154. int IntInput() // Проверка на Int
  155. {
  156.     int imputNumber;
  157.     while (!(cin >> imputNumber) || (cin.peek() != '\n'))
  158.     {
  159.         cin.clear();
  160.         while (cin.get() != '\n');
  161.         cout << "Это не целое число. Попробуйте еще раз.\n";
  162.     }
  163.     return imputNumber;
  164. }
  165.  
  166. class Node
  167. {
  168. public:
  169.     int UDK;
  170.     char authorName[40];
  171.     char bookName[40];
  172.     int year;
  173.     int count;
  174.     int pos;
  175.     Node* next;
  176.     Node()
  177.     {
  178.         UDK = 0;
  179.         authorName[40] = ' ';
  180.         bookName[40] = ' ';
  181.         year = 0;
  182.         count = 0;
  183.         pos = 0;
  184.         next = NULL;
  185.     }
  186. };
  187.  
  188. void Numeric(Node* head)
  189. {
  190.     Node* ptr = head;
  191.     int idx = 1;
  192.     while (ptr)
  193.     {
  194.         ptr->pos = idx;
  195.         idx++;
  196.         ptr = ptr->next;
  197.     }
  198. }
  199.  
  200. class List
  201. {
  202. private:
  203.     Node* head;
  204.     int Length;
  205. public:
  206.     List() { head = NULL; Length = 0; }
  207.     ~List()
  208.     {
  209.         Node* ptr = head, *tmp = NULL;
  210.         if (head)
  211.         {
  212.             while (ptr->next)
  213.             {
  214.                 tmp = ptr;
  215.                 delete[] ptr;
  216.                 ptr = tmp;
  217.             }
  218.             delete[] ptr;
  219.         }
  220.     }
  221.     //Методы
  222.     void Show(bool flag)
  223.     {
  224.         cout << "Текущий список:" << endl;
  225.         Node* ptr = head;
  226.         int i = 0;
  227.         if (!flag)
  228.         {
  229.             if (head == NULL)
  230.             {
  231.                 cout << "Список пуст." << endl;
  232.             }
  233.             while (ptr)
  234.             {
  235.                 cout << "---Книга: " << ++i << endl;
  236.                 cout << "УДК: " << ptr->UDK << endl;
  237.                 cout << "Автор: " << ptr->authorName << endl;
  238.                 cout << "Название: " << ptr->bookName << endl;
  239.                 cout << "Год издания: " << ptr->year << endl;
  240.                 cout << "Количество: " << ptr->count << endl;
  241.                 ptr = ptr->next;
  242.             }
  243.             cout << endl;
  244.             system("pause");
  245.         }
  246.         else
  247.         {
  248.             if (head == NULL)
  249.             {
  250.                 cout << "Список пуст." << endl;
  251.             }
  252.             else
  253.             {
  254.                 Numeric(head);
  255.                 while (ptr)
  256.                 {
  257.                     cout << "---Книга: " << ++i << endl;
  258.                     cout << "УДК: " << ptr->UDK << endl;
  259.                     cout << "Автор: " << ptr->authorName << endl;
  260.                     cout << "Название: " << ptr->bookName << endl;
  261.                     cout << "Год издания: " << ptr->year << endl;
  262.                     cout << "Количество: " << ptr->count << endl;
  263.                     ptr = ptr->next;
  264.                 }
  265.             }
  266.         }
  267.     }
  268.     void Add()
  269.     {
  270.         cin.clear();
  271.         cin.sync();
  272.         cout << "Введите УДК: ";
  273.         int UDK = IntInput();
  274.         char authorName[40], bookName[40];
  275.         cout << "Введите автора: ";
  276.         cin.get();
  277.         cin.getline(authorName, 40);
  278.         cout << "Введите название: ";
  279.         cin.getline(bookName, 40);
  280.         cout << "Введите год издания: ";
  281.         int year = IntInput();
  282.         cout << "Введите количество: ";
  283.         int count = IntInput();
  284.         Node* ptr = NULL;
  285.         ptr = new Node;
  286.         ptr->UDK = UDK;
  287.         strcpy_s(ptr->authorName, authorName);
  288.         strcpy_s(ptr->bookName, bookName);
  289.         ptr->year = year;
  290.         ptr->count = count;
  291.         if (head == NULL)
  292.             ptr->next = NULL;
  293.         else
  294.             ptr->next = head;
  295.         head = ptr;
  296.         Length++;
  297.         cout << "Книга - " << bookName << " добавлена" << endl;
  298.         cout << endl;
  299.         system("pause");
  300.     }
  301.     void Delete()
  302.     {
  303.         if (head)
  304.         {
  305.             Show(true);
  306.             cout << "Выберите элемент, введя его номер позиции: ";
  307.             cin.clear();
  308.             cin.sync();
  309.             int temp = IntInput();
  310.             if (temp > Length || temp == 0)
  311.             {
  312.                 cout << "Ввели неправильный номер элемента. Повторите ввод." << endl;
  313.             }
  314.             else
  315.             {
  316.                 Node* ptr = head, *tmp = NULL, *ToDel = NULL, *prev = NULL;
  317.                 int position = 0, Value = 0;
  318.                 while (ptr)
  319.                 {
  320.                     if (ptr->pos == temp)
  321.                     {
  322.                         ToDel = ptr;
  323.                         Length--;
  324.                         position = ptr->pos;
  325.                         Value = ptr->UDK;
  326.                         break;
  327.                     }
  328.                     else
  329.                     {
  330.                         prev = ptr;
  331.                         ptr = ptr->next;
  332.                     }
  333.                 }
  334.                 if (ToDel)
  335.                 {
  336.                     if (ToDel == head)
  337.                     {
  338.                         tmp = ToDel->next;
  339.                         delete[] ToDel;
  340.                         head = tmp;
  341.                     }
  342.                     else
  343.                     {
  344.                         if (ToDel->next == NULL)
  345.                         {
  346.                             delete[] ToDel;
  347.                             prev->next = NULL;
  348.                         }
  349.                         else
  350.                         {
  351.                             prev->next = ToDel->next;
  352.                             delete[] ToDel;
  353.                         }
  354.                     }
  355.                     cout << "Элемента списка под номером {" << position << ") " << Value << "} удален успешно." << endl;
  356.                 }
  357.             }
  358.             cout << endl;
  359.             system("pause");
  360.         }
  361.     }
  362.     void Clean()
  363.     {
  364.         Node* ptr = head, *tmp = NULL;
  365.         if (head)
  366.         {
  367.             while (ptr->next)
  368.             {
  369.                 tmp = ptr->next;
  370.                 delete[] ptr;
  371.                 ptr = tmp;
  372.             }
  373.             delete[] ptr;
  374.             head = NULL;
  375.             Length = 0;
  376.             cout << "Список успешно очищен." << endl;
  377.         }
  378.         else
  379.         {
  380.             cout << "Список пуст. Нечего очищать." << endl;
  381.         }
  382.         cout << endl;
  383.         system("pause");
  384.     }
  385.     void Find()
  386.     {
  387.         cout << "Введите минимальное количество книг для поискового фильтра: ";
  388.         int count = IntInput();
  389.         Node* ptr = head;
  390.         int i = 0;
  391.         while (ptr)
  392.         {
  393.             if (ptr->count > count)
  394.             {
  395.                 cout << "---Книга: " << ++i << endl;
  396.                 cout << "УДК: " << ptr->UDK << endl;
  397.                 cout << "Автор: " << ptr->authorName << endl;
  398.                 cout << "Название: " << ptr->bookName << endl;
  399.                 cout << "Год издания: " << ptr->year << endl;
  400.                 cout << "Количество: " << ptr->count << endl;
  401.                 ptr = ptr->next;
  402.             }
  403.         }
  404.         cout << endl;
  405.         system("pause");
  406.     }
  407. };
  408.  
  409. int main()
  410. {
  411.     SetConsoleCP(1251);
  412.     SetConsoleOutputCP(1251);
  413.  
  414.     List point;
  415.     int buf = 0;
  416.     while (true)
  417.     {
  418.         system("cls");
  419.         cout << "---Выберите опцию: " << endl;
  420.         cout << "1 - Добавление элемента в список" << endl;
  421.         cout << "2 - Удаление элемента из списка" << endl;
  422.         cout << "3 - Вывод списка" << endl;
  423.         cout << "4 - Очистка списка" << endl;
  424.         cout << "5 - Поиск по фильтру" << endl;
  425.         cout << "ESC - Выход" << endl;
  426.         switch (_getch())
  427.         {
  428.         case 49:
  429.             cout << endl;
  430.             cout << "Добавление элемента в список." << endl;
  431.             point.Add();
  432.             break;
  433.         case 50:
  434.             cout << endl;
  435.             cout << "Удаление элемента из списка." << endl;
  436.             point.Delete();
  437.             break;
  438.         case 51:
  439.             cout << endl;
  440.             cout << "Вывод списка." << endl;
  441.             point.Show(false);
  442.             break;
  443.         case 52:
  444.             cout << endl;
  445.             cout << "Очистка списка." << endl;
  446.             point.Clean();
  447.             break;
  448.         case 53:
  449.             cout << endl;
  450.             cout << "Поиск книг по фильтру." << endl;
  451.             point.Find();
  452.             break;
  453.         case 27://ESC button           
  454.             return 0;
  455.         }
  456.     }
  457.     return 0;
  458. }
Advertisement
Add Comment
Please, Sign In to add comment