Advertisement
Maksud3

Creatures.cpp

Oct 16th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.30 KB | None | 0 0
  1. #include "Creatures.h"
  2.  
  3. #include <iostream>
  4. #include <iomanip>
  5.  
  6. void PrintChar(char c, int count)
  7. {
  8.     for (int i(0); i < count; i++)
  9.         printf("%c", c);
  10.     printf("\n");
  11. }
  12.  
  13.  
  14. void InputCtgMenu(int from, int to, int* pTask)
  15. {
  16.     setlocale(LC_ALL, "rus");
  17.     if (!pTask)
  18.         return;
  19.  
  20.     while (true)
  21.     {
  22.         std::cout << "Введите номер операции: [" << from << "-" << to << "]: ";
  23.         std::cin >> *pTask;
  24.  
  25.         if (std::cin.good())
  26.         {
  27.             std::cin.ignore(100, '\n');
  28.             if (from <= *pTask && *pTask <= to)
  29.                 break;
  30.  
  31.             std::cout << "Значения должны быть в диапазоне: [" << from << "-" << to << "]: " << std::endl;
  32.             continue;
  33.         }
  34.  
  35.         std::cin.clear();
  36.         std::cout << "Вы ввели недопустимые символы." << std::endl;
  37.         std::cin.ignore(100, '\n');
  38.     }
  39. }
  40. bool Menu(int* pTask)
  41. {
  42.     setlocale(LC_ALL, "rus");
  43.  
  44.     std::cout << "Меню программы" << std::endl;
  45.     std::cout << "1. Вывести на экран список шедевров искусства" << std::endl;
  46.     std::cout << "2. Добавить новый шедевр искусства" << std::endl;
  47.     std::cout << "3. Удалить существующий шедерв искусства" << std::endl;
  48.     std::cout << "0. Выход\n" << std::endl;
  49.  
  50.     InputCtgMenu(MENU_EXIT, MENU_DEL, pTask);
  51.  
  52.     return *pTask != MENU_EXIT;
  53. }
  54.  
  55. void InsertList(List** begin, Creature* C)
  56. {
  57.     List* Ins = new List;
  58.  
  59.     Ins->C = *C;
  60.  
  61.     List* End = new List;
  62.     End = *begin;
  63.  
  64.     if (*begin == NULL)
  65.     {
  66.         Ins->next = NULL;
  67.         *begin = Ins;
  68.         End = *begin;
  69.         return;
  70.     }
  71.  
  72.     while (End->next != NULL)
  73.         End = End->next;
  74.     List* newList = new List;
  75.     newList->C = *C;
  76.     newList->next = NULL;
  77.     End->next = newList;
  78. }
  79.  
  80. void PrintList(List *L)
  81. {
  82.     List *Print = L;
  83.     PrintChar('-', 80);
  84.     std::cout << "Год" << std::setw(15) << "Название" << std::setw(15) << "Автор" << std::setw(15) << "Музей" << std::endl;
  85.     PrintChar('-', 80);
  86.     while (Print)
  87.     {
  88.         std::cout << Print->C.year << std::setw(15) << Print->C.name << std::setw(15) << Print->C.author << std::setw(15) << Print->C.museum << std::endl;
  89.         Print = Print->next;
  90.     }
  91.     PrintChar('-', 80);
  92. }
  93.  
  94. void InputCreature(Creature* pCreature)
  95. {
  96.     setlocale(LC_ALL, "rus");
  97.     if (!pCreature)
  98.         return;
  99.  
  100.     int year;
  101.  
  102.     std::cout << "Введите год: ";
  103.     std::cin >> year;
  104.  
  105.     if (std::cin.good())
  106.         pCreature->year = year;
  107.  
  108.  
  109.     std::cin.clear();
  110.     std::cout << "Введите автора: ";
  111.     std::cin >> pCreature->author;
  112.  
  113.     std::cout << "Введите имя шедевра: ";
  114.  
  115.     std::cin >> pCreature->name;
  116.  
  117.     std::cout << "Введите музей: ";
  118.     std::cin >> pCreature->museum;
  119. }
  120.  
  121. void AddCreature(List** begin)
  122. {
  123.     Creature temp;
  124.  
  125.     InputCreature(&temp);
  126.     InsertList(begin, &temp);
  127. }
  128.  
  129. void DeleteCreature(List** begin)
  130. {
  131.     int key;
  132.     std::cout << "Введите год шедевра, который хотите удалить: ";
  133.     std::cin >> key;
  134.  
  135.     if (*begin == NULL)
  136.         return;
  137.  
  138.     List* temp = *begin;
  139.  
  140.     if (temp->C.year == key)
  141.     {
  142.         *begin = temp->next;
  143.         delete temp;
  144.         return;
  145.     }
  146.    
  147.     List* temp2 = temp->next;
  148.  
  149.     while (temp2)
  150.     {
  151.         if (temp2->C.year == key)
  152.         {
  153.             temp->next = temp2->next;
  154.             delete temp2;
  155.             return;
  156.         }
  157.         temp = temp2;
  158.         temp2 = temp2->next;
  159.     }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement