Advertisement
Guest User

Untitled

a guest
Apr 26th, 2015
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.47 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #define list1 struct list1
  4. #define list2 struct list2
  5.  
  6. using namespace std;
  7.  
  8. int n, k;
  9.  
  10. list1 //Односвязная
  11. {
  12.     int info;
  13.     list1 *next;
  14.     list2 *down;
  15. }; list1 *head;
  16.  
  17. list2
  18. {
  19.     int info;
  20.     list2 *next;
  21.     list1   *back;
  22. };
  23.  
  24. int  menu_select()
  25. {
  26.         int c;
  27.         cout << " \n\t\t Меню:"<<endl;
  28.         cout << "\n1. Ввод списочный структуры\n";
  29.         cout << "2. Отобразить содержимое структуры\n";
  30.         cout << "3. Перемещение по структуре\n";
  31.         cout << "4. Справка\n";
  32.         cout << "5. Выход\n";
  33.         do
  34.         {
  35.             cout << "\nВаш выбор: ";
  36.             cin >> c;
  37.         }
  38.         while(c < 0 || c > 6);
  39.         return c;
  40. }
  41. list1 *appendFirst(int x) //d –число добавляемое в список
  42.   {
  43.     list1 *t=new list1;
  44.     t->down=0;
  45.     t->info=0;
  46.     t->next=0;
  47.  
  48.     list2 *p=new list2;
  49.     p->info=x;
  50.     p->next=0;
  51.     p->back=0;
  52.  
  53.     t->down=p;
  54.     return t;
  55. }
  56.  
  57. void appendOfList2(list1 **s, int x)
  58. {
  59.     list1 *start = *s;
  60.  
  61.  
  62.     list2 *downEl = start->down;
  63.  
  64.     list2 *dadd=new list2;
  65.     dadd->info=x;
  66.     dadd->back=0;
  67.     dadd->next=0;
  68.  
  69.     downEl->next=dadd;
  70.     start->down=dadd;
  71.  
  72.     list1 *nadd=new list1;
  73.     nadd->info=0;
  74.     nadd->next=0;
  75.  
  76.     if (start->next)
  77.         nadd->next=start->next;
  78.     start->next=nadd;
  79.     nadd->down=downEl;
  80. }
  81.  
  82. void appendOfList1(list1 **s, int x)
  83. {
  84.  
  85.     list1 *start = *s;
  86.     while (start->info != 0)
  87.     {
  88.         start=start->next;
  89.     }
  90.     start->info=x;
  91. }
  92. void list1display(list1 *s)
  93. {
  94.     list1 *t = s;
  95.     if (t)
  96.     {
  97.         list1display(t->next);
  98.         if (t->next)
  99.              cout << "<-";
  100.         cout << t->info;
  101.     }
  102. }
  103.  
  104. void list2display(list1 *s)
  105. {
  106.     list1 *t = s;
  107.     if (t && t->down !=0)
  108.     {
  109.         list2display(t->next);
  110.         if (t->next  && t->next->down !=0)
  111.         {     cout << "->";}
  112.         cout << t->down->info;
  113.     }
  114. }
  115.  
  116. void display(list1 *s, int z, int y) //выводит в том порядке, что и в задании на картинке
  117. {
  118.     list1 *t = s;  int i; int m = y-z;
  119.     list1display(s);
  120.     cout  << endl;  //вывод первого списка в обратном направлении
  121.  
  122.     // ниже идет оформление списка, для большей наглядности
  123.     if (m > 0)
  124.         for (i = 0; i<m; i++)
  125.             cout << "   ";
  126.  
  127.     for (i =0; i <z; i++)
  128.     {
  129.          cout <<"|  ";
  130.     }
  131.     cout << endl;
  132.     if (m > 0)
  133.         for (i = 0; i<m; i++)
  134.             cout << "   ";
  135.  
  136.     for (i =0; i <z; i++)
  137.     {
  138.         cout <<"v  ";
  139.     }
  140.     cout << endl;
  141.  
  142.    if (m > 0)
  143.     for (i = 0; i<m; i++) cout << "   ";
  144.     list2display(t); // вывод второго списка
  145. }
  146.  
  147. void append3(list1 **s, int x)
  148. {
  149.     list1 *t = *s;
  150.  
  151.     list1 *newEl = new list1;
  152.  
  153.     newEl->info=x;
  154.     newEl->next=0;
  155.     newEl->down=0;
  156.  
  157.     while (t->next)
  158.     {
  159.          t=t->next;
  160.     }
  161.     t->next=newEl;
  162. }
  163.  
  164. void input()
  165. {
  166.     int a;
  167.  
  168.     cout << "Введите элементы первого списка:" << endl;
  169.     cin >> a;
  170.     n=0;
  171.     if (a == 0)
  172.        cout << "Первый список пуст" << endl;
  173.     else
  174.     {
  175.        head=appendFirst(a);
  176.     }
  177.     cin >>a;
  178.     n++;
  179.     while (a !=0)
  180.     {
  181.         appendOfList2(&head, a);
  182.         n++;
  183.         cin>>a;
  184.     }
  185.     cout << "Введите элементы второго списка:" << endl;
  186.     cin >> a;
  187.     k=0;
  188.     while (a !=0)
  189.     {
  190.          if ( n > k)
  191.              appendOfList1(&head, a);
  192.           else
  193.              append3(&head, a);
  194.         k++;
  195.         cin>>a;
  196.     }
  197.     cout << endl;
  198. }
  199.  
  200. void moving(list1 **s)
  201. {
  202.     list1 *start =*s;
  203.     list1 *current =*s;
  204.     bool down = 0;
  205.     bool up =1;
  206.     int  state = up;
  207.     list2 *m =0;
  208.     int c;
  209.     cout << "Первый элемент: \n"<< current->info << "\nНажмите кнопку для выбора направления \n";
  210.     cin >> c;
  211.     while (c != 0)
  212.    {
  213.        if (current == start)
  214.        {
  215.            if (c  == 6 )
  216.            {
  217.                cout << "Нельзя двигаться в этом направлении\nВернуться к началу?(1 если да)" << endl ;
  218.                cin >> c;
  219.                if (c ==1)
  220.                     moving(&start);
  221.            }
  222.            else if (c == 2)
  223.            {
  224.                m=current->down;
  225.                cout << m->info << " Это последний элемент второго списка, дальше нельзя продолжить обход\nВернуться к началу?(1 если да)" << endl ;
  226.                cin >> c;
  227.                if (c ==1)
  228.                     moving(&start);
  229.                state = down;
  230.            }
  231.            else  if (c == 4 && current->next)
  232.            {
  233.                 current=current->next;
  234.                 cout << current->info << "\n";
  235.            }
  236.         }
  237.  
  238.        cin >> c; //второй шаг
  239.         if (c == 2 && state == up)
  240.         {
  241.             if (current->down)
  242.             {
  243.                 m=current->down;
  244.                 cout << m->info <<  endl;
  245.                 state=down;
  246.             }
  247.             else cout << "Нет элементов  во втором списке";
  248.         }
  249.         else if (c == 4 && state == up)
  250.         {
  251.             if (current->next)
  252.             {
  253.                 current=current->next;
  254.                 cout << current->info << endl;
  255.             }
  256.             else if (current->down)
  257.                 cout << "Это последний  первого элемент списка, Возможно продолжить движение вниз (по второму списку). Для этого нажмите 2." << endl ;
  258.             else
  259.             {
  260.                 cout << "Это последний элемент первого списка, дальше нельзя продолжить обход\nВернуться к началу?(1 если да)" << endl ;
  261.                 cin >> c;
  262.                 if (c ==1)
  263.                     moving(&start);
  264.             }
  265.         }
  266.         else if (c ==6 && state == down)
  267.         {
  268.             if (m && m->next)
  269.             {
  270.                 m=m->next;
  271.                 cout << m->info <<  endl;
  272.             }
  273.             else
  274.                  cout << m->info << " Это последний элемент второго списка, дальше нельзя продолжить обход\nВернуться к началу?(1 если да)" << endl ;
  275.         }
  276.         else if (2 && state == down)
  277.             cout << "Невозможно продолжить путь в этом направлении " << endl ;
  278.         else
  279.         {
  280.             cout << "Это последний  первого элемент списка, Возможно продолжить движение вниз (по второму списку). Для этого нажмите 2." << endl ;
  281.             cin >> c;
  282.             if (c ==1)
  283.                 moving(&start);
  284.         }
  285.     }
  286. }
  287.  
  288. void move(list1 **s)
  289. {
  290.     list1 *start =*s;
  291.      cout << "*************" << endl;
  292.      cout << "*   4 <--   *" << endl;
  293.      cout << "*   6 -->   *" << endl;
  294.      cout << "*   2  |    *" << endl;
  295.      cout << "*      v    *" << endl;
  296.      cout << "*************" << endl;
  297.      cout << "для возврата в меню нажмите кнопку '0'" << endl;
  298.  
  299.      moving(&start);
  300. }
  301.  
  302.  
  303. void help()
  304. {
  305.      cout << " Что бы создать списочную структуру необходимо выбрать первый пункт меню, и \nввести элементы первого и второго списков.  Для окончания"
  306.              " ввода числовой\nпоследовательности следует нажать кнопку '0'.\n После завершения формирования первого списка (изначально вводится второй\n(нижний) список), "
  307.       "будет совершен автоматический возврат в меню программы.\nТам можно будет выбрать пункты 'Отобразить содержимое структуры' и\n'Перемещение по структуре'. Также можно будет"
  308.      "  вновь открыть справочный\nматериал.\n Перемещение по структуре осуществляется клавишами 4 (влево), 2 (вниз) и 6\n(вправо)."
  309.      " Если движение в заданном направлении невозможно, то будет выведено\nпредупреждение. Пункты 2 и 3 недоступны до того как сформирован список." << endl;
  310. }
  311.  
  312. using namespace std;
  313.  
  314. int main()
  315. {
  316.     setlocale (0, "Rus");
  317.         head = NULL;
  318.           for(;;)
  319.           {
  320.             switch(menu_select()) {
  321.               case 1: input();
  322.                 break;
  323.               case 2: display(head,n,k);
  324.                 break;
  325.               case 3: move(&head);
  326.                 break;
  327.               case 4: help();
  328.                 break;
  329.               case 5: exit(0);
  330.             }
  331.           }
  332.           return 0;
  333. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement