Advertisement
KimSergey1

Untitled

May 24th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.23 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<string>
  4. #include<windows.h>
  5. using namespace std;
  6. const int MAX = 50;
  7. int a_a = 0;
  8. int*a = new int[];
  9. struct htype
  10. {
  11.     string passport;
  12.     string FIO;
  13.     string BirthYear;
  14.     string WhyCome;
  15.     htype*next;
  16. }primary[MAX];
  17. void init()
  18. {
  19.     for (int i = 0; i < MAX; i++)
  20.     {
  21.         primary[i].passport.empty();
  22.         primary[i].FIO.empty();
  23.         primary[i].BirthYear.empty();
  24.         primary[i].WhyCome.empty();
  25.         primary[i].next = NULL;
  26.     }
  27. }
  28. int hash_func(string pass)
  29. {
  30.     int loc = 0, h;
  31.     for (int i = 0; i < pass.size(); i++)
  32.     {
  33.         loc += int(pass[i]);
  34.     }
  35.     h = loc%MAX;
  36.     return h;
  37. }
  38. void add(string pass, string FIO, string Birth, string why)
  39. {
  40.     int h;
  41.     htype *p0;
  42.     htype *p = new htype;
  43.     h = hash_func(pass);
  44.     if (primary[h].passport.empty())
  45.     {
  46.         primary[h].passport = pass;
  47.         primary[h].FIO = FIO;
  48.         primary[h].BirthYear = Birth;
  49.         primary[h].WhyCome = why;
  50.         a_a++;
  51.         a[a_a] = h;
  52.         return;
  53.     }
  54.     else if (!primary[h].passport.empty())
  55.     {
  56.         p0 = primary[h].next;
  57.         primary[h].next = p;
  58.         p->passport = pass;
  59.         p->FIO = FIO;
  60.         p->BirthYear = Birth;
  61.         p->WhyCome = why;
  62.         p->next = p0;
  63.         return;
  64.     }
  65. }
  66. void find(string pass)
  67. {
  68.     int h;
  69.     htype*p;
  70.     h = hash_func(pass);
  71.     if (primary[h].passport == pass)
  72.     {
  73.         cout << "Пасспорт: " << primary[h].passport << endl;
  74.         cout << "ФИО: " << primary[h].FIO << endl;
  75.         cout << "Год: " << primary[h].BirthYear << endl;
  76.         cout << "Цель прибывания: " << primary[h].WhyCome << endl;
  77.     }
  78.     else
  79.     {
  80.         p = primary[h].next;
  81.         while (p)
  82.         {
  83.             if (p->passport == pass)
  84.             {
  85.                 cout << "Пасспорт: " << p->passport << endl;
  86.                 cout << "ФИО: " << p->FIO << endl;
  87.                 cout << "Год: " << p->BirthYear << endl;
  88.                 cout << "Цель прибывания: " << p->WhyCome << endl;
  89.             }
  90.             p = p->next;
  91.         }
  92.         cout << "Такого постояльца нет!" << endl;
  93.     }
  94. }
  95. void HDel_elem(string pass)
  96. {
  97.     int h;
  98.     htype*p;
  99.     h = hash_func(pass);
  100.     if (primary[h].passport == pass)
  101.     {
  102.         cout << "Пасспорт: " << primary[h].passport << endl;
  103.         cout << "ФИО: " << primary[h].FIO << endl;
  104.         cout << "Год: " << primary[h].BirthYear << endl;
  105.         cout << "Цель прибывания: " << primary[h].WhyCome << endl;
  106.         primary[h].passport.clear();
  107.         primary[h].FIO.clear();
  108.         primary[h].BirthYear.clear();
  109.         primary[h].WhyCome.clear();
  110.         primary[h].next = NULL;
  111.         cout << "Данные удалены!" << endl;
  112.     }
  113.     else
  114.     {
  115.         p = primary[h].next;
  116.         while (p)
  117.         {
  118.             if (p->passport == pass)
  119.             {
  120.                 cout << "Пасспорт: " << p->passport << endl;
  121.                 cout << "ФИО: " << p->FIO << endl;
  122.                 cout << "Год: " << p->BirthYear << endl;
  123.                 cout << "Цель прибывания: " << p->WhyCome << endl;
  124.                 p->passport = p->passport.empty();
  125.                 p->FIO = p->FIO.empty();
  126.                 p->BirthYear = p->BirthYear.empty();
  127.                 p->WhyCome = p->WhyCome.empty();
  128.                 primary[h].next = NULL;
  129.                 cout << "Данные удалены!" << endl;
  130.             }
  131.             p = p->next;
  132.         }
  133.         cout << "Такого постояльца нет!" << endl;
  134.     }
  135. }
  136. void HdelAll()
  137. {
  138.     for (int i = MAX - 1; i >= 0; i--)
  139.     {
  140.         primary[i].passport.clear();
  141.         primary[i].FIO.clear();
  142.         primary[i].BirthYear.clear();
  143.         primary[i].WhyCome.clear();
  144.         primary[i].next = NULL;
  145.     }
  146. }
  147. void HShowALL()
  148. {
  149.     htype*p;
  150.     cout << endl;
  151.     for (int i = 0; i<MAX; i++)
  152.     {
  153.         cout << i + 1 << ") " << endl;
  154.         cout << "Пасспорт: " << primary[i].passport << endl;
  155.         cout << "ФИО: " << primary[i].FIO << endl;
  156.         cout << "Год рождения: " << primary[i].BirthYear << endl;
  157.         cout << "Причина прибывания" << primary[i].WhyCome << endl;
  158.         p = primary[i].next;
  159.         while (p != NULL)
  160.         {
  161.             cout << i + 1 << ") " << p->passport;
  162.             cout << p->FIO;
  163.             cout << p->BirthYear;
  164.             cout << p->WhyCome;
  165.             p = p->next;
  166.         }
  167.         cout << endl;
  168.     }
  169. }
  170. struct Tree
  171. {
  172.     string Number;
  173.     int Place;
  174.     int Rooms;
  175.     bool WC;
  176.     string Equipments;
  177.     Tree*Left;
  178.     Tree*Right;
  179.     int height;
  180. };
  181. int height_tree(Tree*tree)
  182. {
  183.     int temp;
  184.     if (tree == NULL)
  185.     {
  186.         return(-1);
  187.     }
  188.     else
  189.     {
  190.         temp = tree->height;
  191.         return temp;
  192.     }
  193. }
  194. int Max(int temp1, int temp2)
  195. {
  196.     return ((temp1 > temp2) ? temp1 : temp2);
  197. }
  198. Tree srl(Tree*&tree)
  199. {
  200.     Tree* t2;
  201.     t2 = tree->Left;
  202.     tree->Left = t2->Right;
  203.     t2->Right = tree;
  204.     tree->height = Max(height_tree(tree->Left), height_tree(tree->Right)) + 1;
  205.     t2->height = Max(height_tree(tree->Left), t2->height) + 1;
  206.     return *t2;
  207. }
  208. Tree srr(Tree*&tree)
  209. {
  210.     Tree* tree2;
  211.     tree2 = tree->Right;
  212.     tree->Right = tree2->Left;
  213.     tree2->Left = tree;
  214.     tree->height = Max(height_tree(tree->Left), height_tree(tree->Right)) + 1;
  215.     tree2->height = Max(tree->height, height_tree(tree2->Right)) + 1;
  216.     return *tree2;
  217. }
  218. Tree drl(Tree*&tree)
  219. {
  220.     *tree->Left = srr(tree->Right);
  221.     return srr(tree);
  222. }
  223. Tree drr(Tree*&tree)
  224. {
  225.     *tree->Right = srl(tree->Right);
  226.     return srr(tree);
  227. }
  228. int temp_of(string Numb)
  229. {
  230.     int loc = 0;
  231.     loc = ((Numb[1] - 48) * 100 + (Numb[2] - 48) * 10 - (Numb[3] - 48) * 1) + 6;
  232.     return loc;
  233. }
  234. void add_tree(string Number, int Place, int Rooms, bool WC, string Equipments, Tree*tree)
  235. {
  236.     if (tree == NULL)
  237.     {
  238.         tree = new Tree;
  239.         tree->Number = Number;
  240.         tree->Place = Place;
  241.         tree->Rooms = Rooms;
  242.         tree->WC = WC;
  243.         tree->Equipments = Equipments;
  244.         tree->Left = NULL;
  245.         tree->Right = NULL;
  246.         tree->height = 0;
  247.     }
  248.     else
  249.     {
  250.         if (temp_of(Number) < temp_of(tree->Number))
  251.         {
  252.             add_tree(Number, Place, Rooms, WC, Equipments, tree->Left);
  253.             if (height_tree(tree->Left) - height_tree(tree->Right) == 2)
  254.             {
  255.                 if (temp_of(Number)<temp_of(tree->Number))
  256.                 {
  257.                     *tree = srl(tree);
  258.                 }
  259.                 else
  260.                 {
  261.                     *tree = srl(tree);
  262.                 }
  263.             }
  264.         }
  265.         else if (temp_of(Number) > temp_of(tree->Number))
  266.         {
  267.             add_tree(Number, Place, Rooms, WC, Equipments, tree->Right);
  268.             if (height_tree(tree->Right) - height_tree(tree->Left) == 2)
  269.             {
  270.                 if (temp_of(Number) > temp_of(tree->Number))
  271.                 {
  272.                     *tree = srr(tree);
  273.                 }
  274.                 else
  275.                 {
  276.                     *tree = drr(tree);
  277.                 }
  278.             }
  279.         }
  280.         else
  281.         {
  282.             cout << "Такой номер уже существует!" << endl;
  283.         }
  284.     }
  285. }
  286. void find_tree(string elem, Tree*&tree)
  287. {
  288.     if (tree == NULL)
  289.     {
  290.         cout << "База номеров пуста, пожалуйста добавте номер!" << endl;
  291.     }
  292.     else
  293.     {
  294.         while (elem != tree->Number)
  295.         {
  296.             if (elem < tree->Number)
  297.             {
  298.                 find_tree(elem, tree->Left);
  299.                 cout << "Номер " << endl;
  300.                 cout << tree->Number << endl;
  301.                 cout << "Мест: " << endl;
  302.                 cout << tree->Place << endl;
  303.                 cout << "Rоличество комнат: " << endl;
  304.                 cout << tree->Rooms << endl;
  305.                 cout << "Наличие санузла \n 1 - есть \n 0 - нет" << endl;
  306.                 cout << tree->WC << endl;
  307.             }
  308.             else if (elem>tree->Number)
  309.             {
  310.                 find_tree(elem, tree->Right);
  311.                 cout << "Номер " << endl;
  312.                 cout << tree->Number << endl;
  313.                 cout << "Мест: " << endl;
  314.                 cout << tree->Place << endl;
  315.                 cout << "Rоличество комнат: " << endl;
  316.                 cout << tree->Rooms << endl;
  317.                 cout << "Наличие санузла \n 1 - есть \n 0 - нет" << endl;
  318.                 cout << tree->WC << endl;
  319.             }
  320.             else
  321.             {
  322.                 cout << "Номер " << endl;
  323.                 cout << tree->Number << endl;
  324.                 cout << "Мест: " << endl;
  325.                 cout << tree->Place << endl;
  326.                 cout << "Rоличество комнат: " << endl;
  327.                 cout << tree->Rooms << endl;
  328.                 cout << "Наличие санузла \n 1 - есть \n 0 - нет" << endl;
  329.                 cout << tree->WC << endl;
  330.             }
  331.         }
  332.     }
  333. }
  334. void direct_by_pass(Tree*&tree)
  335. {
  336.     if (tree != NULL)
  337.     {
  338.         cout << "Номер " << endl;
  339.         cout << tree->Number << endl;
  340.         cout << "Мест: " << endl;
  341.         cout << tree->Place << endl;
  342.         cout << "Rоличество комнат: " << endl;
  343.         cout << tree->Rooms << endl;
  344.         cout << "Наличие санузла \n 1 - есть \n 0 - нет" << endl;
  345.         cout << tree->WC << endl;
  346.         direct_by_pass(tree->Left);
  347.         direct_by_pass(tree->Right);
  348.     }
  349. }
  350. void Delete_Elem_tree(string elem, Tree*&tree)
  351. {
  352.     Tree*del;
  353.     if (tree == NULL)
  354.     {
  355.         cout << "Такого номера нет!" << endl;
  356.     }
  357.     else if (elem < tree->Number)
  358.     {
  359.         Delete_Elem_tree(elem, tree->Left);
  360.     }
  361.     else if (elem > tree->Number)
  362.     {
  363.         Delete_Elem_tree(elem, tree->Right);
  364.     }
  365.     else if ((tree->Left == NULL) && (tree->Right == NULL))
  366.     {
  367.         del = tree;
  368.         free(del);
  369.         tree = NULL;
  370.         cout << "Номер удален!" << endl;
  371.     }
  372.     else if (tree->Left == NULL)
  373.     {
  374.         del = tree;
  375.         free(tree);
  376.         tree = tree->Right;
  377.         cout << "Номер удален!" << endl;
  378.     }
  379.     else if (tree->Right == NULL)
  380.     {
  381.         del = tree;
  382.         free(tree);
  383.         tree = tree->Left;
  384.         cout << "Номер удален!" << endl;
  385.     }
  386. }
  387. void Del_All_tree(Tree*&tree)
  388. {
  389.     delete tree;
  390. }
  391. struct Check
  392. {
  393.     string numPas;
  394.     string numRoom;
  395.     string dateIn;
  396.     string dateOut;
  397.     Check* next;
  398. };
  399. struct List
  400. {
  401.     Check* head;
  402.     Check* tail;
  403.     List()
  404.     {
  405.         head = 0;
  406.         tail = 0;
  407.     }
  408. };
  409. List* insert(List* p[], int i, string passport, string room, string dateIn, string dateOut) // Добавление данных
  410. {
  411.     Check* tmp = new Check();
  412.     tmp->numPas = passport;
  413.     tmp->numRoom = room;
  414.     tmp->dateIn = dateIn;
  415.     tmp->dateOut = dateOut;
  416.     tmp->next = 0;
  417.     if (p[i]->head != 0)
  418.     {
  419.         p[i]->tail->next = tmp;
  420.         p[i]->tail = tmp;
  421.     }
  422.     else p[i]->tail = p[i]->head = tmp;
  423.     return p[i];
  424. }
  425. List* deleting(List* p[], string passport, int i) // Удаление элемента
  426. {
  427.     bool flag = 0;
  428.     Check* tmp = p[i]->head;
  429.     Check* tmp1 = p[i]->head;
  430.     if (!tmp) return 0;
  431.     if (p[i]->head->numPas == passport) // Голова
  432.     {
  433.         flag = 1;
  434.         tmp = p[i]->head->next;
  435.         delete p[i]->head;
  436.         p[i]->head = tmp;
  437.     }
  438.     else if (p[i]->tail->numPas == passport) // Хвост
  439.     {
  440.         flag = 1;
  441.         tmp = p[i]->head;
  442.         while (tmp->next != p[i]->tail->next)
  443.         {
  444.             if (tmp->next == p[i]->tail)
  445.             {
  446.                 delete p[i]->tail;
  447.                 p[i]->tail = tmp;
  448.             }
  449.             else tmp = tmp->next;
  450.         }
  451.     }
  452.     else // Иные эл.
  453.     {
  454.         while (tmp->next != p[i]->tail->next)
  455.         {
  456.             if (tmp->next->numPas == passport)
  457.             {
  458.                 flag = 1;
  459.                 tmp1 = tmp->next;
  460.                 tmp->next = tmp->next->next;
  461.                 delete tmp1;
  462.                 break;
  463.             }
  464.             else tmp = tmp->next;
  465.         }
  466.     }
  467.     if (!flag)
  468.         cout << "Нет такой записи" << endl;
  469.     return p[i];
  470. }
  471. void deleting(List* p[]) // Удаление всего списка
  472. {
  473.     Check* tmp = 0;
  474.     for (int i = 0; i < 4; i++)
  475.     {
  476.         tmp = p[i]->head;
  477.         while (tmp != p[i]->tail->next)
  478.         {
  479.             tmp = p[i]->head;
  480.             p[i]->head = p[i]->head->next;
  481.             delete tmp;
  482.         }
  483.     }
  484. }
  485. int skip(string room) // Определение номера указателя
  486. {
  487.     if (room[0] == 'Л') return 0;
  488.     if (room[0] == 'П') return 1;
  489.     if (room[0] == 'О') return 2;
  490.     if (room[0] == 'М') return 3;
  491.     else return -1;
  492. }
  493.  
  494. int main()
  495. {
  496.     system("cls");
  497.     SetConsoleOutputCP(1251);
  498.     SetConsoleCP(1251);
  499.     int choose = -1;
  500.     string pass, FIO, Why, Birth;
  501.     cout << "1-Добавить гостя" << endl;
  502.     cout << "2-Удалить гостя" << endl;
  503.     cout << "3-Показать всех гостей" << endl;
  504.     cout << "4-Найти гостя" << endl;
  505.     cout << "5-Удалить всех гостей" << endl;
  506.     cout << "6-Поиск гостя по ФИО" << endl;
  507.     init();
  508.     cin >> choose;
  509.     switch (choose)
  510.     {
  511.     case 1:
  512.         system("cls");
  513.         cout << "Введите номер пасспорта" << endl;
  514.         cin >> pass;
  515.         cout << "Введите ФИО" << endl;
  516.         cin >> FIO;
  517.         cout << "Введите год рождения" << endl;
  518.         cin >> Birth;
  519.         cout << "Введите цель прибывания" << endl;
  520.         cin >> Why;
  521.         add(pass, FIO, Birth, Why);
  522.         break;
  523.     case 2:
  524.         system("cls");
  525.         cout << "Введите номер паспорта для поиска" << endl;
  526.         cin >> pass;
  527.         HDel_elem(pass);
  528.         system("pause");
  529.         break;
  530.     case 3:
  531.         system("cls");
  532.         HShowALL();
  533.         system("pause");
  534.         break;
  535.     case 4:
  536.         system("cls");
  537.         cout << "Введите номер паспорта для поиска" << endl;
  538.         cin >> pass;
  539.         find(pass);
  540.         system("pause");
  541.         break;
  542.     case 5:
  543.         system("cls");
  544.         HdelAll();
  545.         cout << "Все данные были удалены!" << endl;
  546.         system("pause");
  547.         break;
  548.     }
  549.     return main();
  550. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement