Advertisement
P1punGorbach

lakunina 3

Dec 6th, 2022 (edited)
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.24 KB | None | 0 0
  1. /*Дана последовательность символов, среди которой есть двоеточие.Получить все символы, расположенные между первым и вторым двоеточием.Если второго двоеточия нет,
  2. то получить все символы, расположенные после единственного двоеточия.*/
  3.  
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6.  
  7. //структура элемента массива
  8. //методы обращения к данным можно не использовать так как
  9. //модификатор доступа для структуры public
  10. struct List_one_Node
  11. {
  12.     List_one_Node* next = NULL;
  13.     char value;
  14.  
  15.     List_one_Node* get_next()
  16.     {
  17.         return next;
  18.     }
  19.  
  20.     char get_value()
  21.     {
  22.         return value;
  23.     }
  24.  
  25.     void reset_next(List_one_Node* newnext)
  26.     {
  27.         next = newnext;
  28.     }
  29. };
  30.  
  31. // сам список
  32. struct List_one
  33. {
  34.     List_one_Node* start = NULL;
  35.     List_one_Node* current = NULL;
  36.     //переход на сл элемент
  37.     void go_next()
  38.     {
  39.         if (current->get_next() != NULL)
  40.             current = current->next;
  41.     }
  42.     //выделяет пямать под List_one_Node
  43.     List_one_Node* create_new_node()
  44.     {
  45.         List_one_Node* newp = (List_one_Node*)malloc(sizeof(List_one_Node));
  46.         if (newp == NULL)
  47.         {
  48.             exit(-1);
  49.         }
  50.         else return newp;
  51.     }
  52.     //используется для ввода строки
  53.     //устанавливает указатель на последний элемент
  54.     void append(char new_value)
  55.     {
  56.         if (current != NULL)
  57.         {
  58.             current->next = create_new_node();
  59.             /*current->reset_next(create_new_node());*/
  60.             (current->next)->next = NULL;
  61.             (current->next)->value = new_value;
  62.             go_next();
  63.         }
  64.         else
  65.         {
  66.             start = create_new_node();
  67.             current = start;
  68.             current->value = new_value;
  69.         }
  70.     }
  71.  
  72.     void print()
  73.     {
  74.         List_one_Node* st = start;
  75.         while (1) {
  76.             if (st == NULL)
  77.             {
  78.                 break;
  79.             }
  80.  
  81.             printf("%c", st->get_value());
  82.             st = st->get_next();
  83.         }
  84.  
  85.     }
  86.     void add_by_index(int index, char value)
  87.     {
  88.         List_one_Node* next = NULL;
  89.         List_one_Node* st = start;
  90.         if (index == 1)
  91.         {
  92.             next = start;
  93.             start = create_new_node();
  94.             start->next = next;
  95.             start->value = value;
  96.         }
  97.         else
  98.         {
  99.             for (int i = 1; i < index; i++)
  100.                 st = st->next;
  101.             next = st->next;
  102.             st->next = create_new_node();
  103.             st->next->value = value;
  104.             st->next->next = next;
  105.         }
  106.     }
  107.     void delete_by_index(int index) {
  108.         List_one_Node* next = NULL;
  109.         List_one_Node* st = start;
  110.         List_one_Node* nextnext = NULL;
  111.         if (index == 1)
  112.         {
  113.             next = start->next;
  114.  
  115.             free(start);
  116.             start = next;
  117.         }
  118.         else
  119.         {
  120.             for (int i = 1; i < index - 1; i++)
  121.                 st = st->next;
  122.             next = st->next;
  123.             if (next != NULL) {
  124.                 nextnext = next->next;
  125.                 st->next = nextnext;
  126.                 free(next);
  127.             }
  128.         }
  129.     }
  130.     char get_by_index(int index) {
  131.         List_one_Node* next = NULL;
  132.         List_one_Node* st = start;
  133.         if (index == 0)
  134.         {
  135.             return start->get_value();
  136.         }
  137.         else
  138.         {
  139.             for (int i = 1; i < index; i++)
  140.                 st = st->next;
  141.             next = st->next;
  142.             if (next != NULL) {
  143.                 return next->get_value();
  144.             }
  145.             return '?';
  146.         }
  147.     }
  148. };
  149.  
  150. void test()
  151. {
  152.     char c;
  153.     List_one Sentence;
  154.     int i = 0;
  155.     while (1)
  156.     {
  157.         c = getchar();
  158.         if (c == '\n')
  159.         {
  160.             break;
  161.         }
  162.         Sentence.append(c);
  163.         i++;
  164.     }
  165.     int lenght = i;
  166.  
  167.     Sentence.add_by_index(1, 'n');
  168.     Sentence.print();
  169.     Sentence.delete_by_index(2);
  170.  
  171.  
  172.  
  173. }
  174. void main()
  175. {
  176.    
  177.     char c;
  178.     List_one Sentence;
  179.     int i = 0, length = 0, first = 0, second = 0;
  180.     char simb;
  181.     printf("Enter a string: ");
  182.     while (1)
  183.     {
  184.         c = getchar();
  185.         if (c == '\n')
  186.         {
  187.             break;
  188.         }
  189.         Sentence.append(c);
  190.         i++;
  191.     }
  192.     Sentence.append(' ');
  193.     length = i + 1;
  194.      
  195.  
  196.     for (i = 1; i < length; i++)
  197.     {
  198.         if (Sentence.get_by_index(i) == ';') {
  199.             first = i;
  200.             break;
  201.         }
  202.     }
  203.     for (i = first; i < length; i++)
  204.     {
  205.         if (Sentence.get_by_index(i) == ';') {
  206.             second = i;
  207.             break;
  208.         }
  209.     }
  210.     if (second!=0)
  211.     {
  212.         for(i = first; i < second ;i++)
  213.         {
  214.             simb = Sentence.get_by_index(i);
  215.             printf("%c", simb);
  216.  
  217.         }
  218.     }
  219.     printf("%d", first);
  220.  
  221.    
  222.     test();
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement