Advertisement
Brml

Stack

May 27th, 2018
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.88 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. #include <iostream>
  4. // 9_lab.cpp: определяет точку входа для консольного приложения.
  5. //
  6. #include <string.h>
  7.  
  8. #include <iostream>
  9. #include <iomanip>
  10. using namespace std;
  11.  
  12. int const len_table = 4;
  13. struct sea
  14. {
  15.     char name[64];
  16.     int deep;
  17.     char location[64];
  18.     sea *last;
  19.     sea *head;
  20. };
  21. sea read()
  22. {
  23.     sea buf_elem;
  24.     char name[32];
  25.     char location[32];
  26.     cin >> name >> buf_elem.deep >> location ;
  27.     strcpy(buf_elem.name, name);
  28.     strcpy(buf_elem.location,location);
  29.     return buf_elem;
  30. }
  31. void show(sea seee, char sign = ' ')
  32. {
  33.     if (sign != ' ')
  34.     {
  35.         cout <<
  36.             sign <<
  37.             seee.name << setw(10) << sign <<
  38.             seee.deep << setw(10) << sign <<
  39.             seee.location << setw(10) << sign <<
  40.             endl;
  41.     }
  42.     else {
  43.  
  44.         cout <<
  45.             sign <<
  46.             seee.name << sign <<
  47.             seee.deep << sign <<
  48.             seee.location << sign <<
  49.             endl;
  50.     }
  51. }
  52. void add(sea *stack, sea elem)
  53. {
  54.     sea *buf = new sea;
  55.     strcpy(buf->name, elem.name);
  56.     buf->deep = elem.deep;
  57.     strcpy(buf->location ,elem.location);
  58.  
  59.  
  60.     buf->last = stack->head;    //тот элемент, который является головным, сейчас перемещается в предыдущий для текущего
  61.     stack->head = buf;          // текущий элемент становится головным
  62. }
  63. void delete_head_elem(sea *stack)
  64. {
  65.     if (stack->head != NULL)
  66.     {
  67.  
  68.         sea *ptr = stack->head->last;
  69.         delete stack->head;
  70.         stack->head = ptr;
  71.     }
  72.     else
  73.     {
  74.         delete stack->head;
  75.     }
  76. }
  77. void clear_all_steck(sea *stack) {
  78.     while (stack->head != NULL)
  79.     {
  80.         delete_head_elem(stack);
  81.     }
  82.     delete stack->head;
  83.     delete stack;
  84. }
  85. void tr(sea *stack)
  86. {
  87.     show(*stack->head);
  88.     delete_head_elem(stack);
  89. }
  90.  
  91. int main()
  92. {
  93.     sea seas[len_table];
  94.  
  95.     for (int i = 0; i < len_table; i++)
  96.     {
  97.         seas[i] = read();
  98.     }
  99.  
  100.     for (int i = len_table - 1; i >= 0; i--)
  101.     {
  102.         cout << "+--------------------+--------------------+--------------------+\n";
  103.         show(seas[i], '|');
  104.     }
  105.     cout << "+--------------------+--------------------+--------------------+\n";
  106.    
  107.    
  108.     sea *stack = new sea;
  109.     stack->head = NULL;
  110.     stack->last = NULL;
  111.     for (int i = 0; i < len_table; i++)
  112.     {
  113.         add(stack, seas[i]);
  114.     }
  115.     char answer[16];
  116.     cout << "Do you want to take head elements in stack? Enter \"no\" or \"yes\".\n";
  117.     while (true) {
  118.         cin >> answer;
  119.         if (strcmp(answer, "no") != 0 & strcmp(answer, "yes") != 0)
  120.         {
  121.             cout << "Error. Enter again.\n";
  122.  
  123.         }
  124.         else
  125.         {
  126.             if (strcmp(answer, "yes") == 0)
  127.             {
  128.                 if (stack->head != NULL)
  129.                 {
  130.                     tr(stack);
  131.                     cout << "Do you want to take head elements in stack again ? Enter \"no\" or \"yes\".\n";
  132.                 }
  133.                 else
  134.                 {
  135.                     cout << "You can't take head element because stack is empty\n";
  136.                     break;
  137.                 }
  138.             }
  139.             if (strcmp(answer, "no") == 0) {
  140.                 break;
  141.             }
  142.         }
  143.     }
  144.     clear_all_steck(stack);
  145.     return 0;
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement