Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Дана последовательность символов, среди которой есть двоеточие.Получить все символы, расположенные между первым и вторым двоеточием.Если второго двоеточия нет,
- то получить все символы, расположенные после единственного двоеточия.*/
- #include <stdlib.h>
- #include <stdio.h>
- //структура элемента массива
- //методы обращения к данным можно не использовать так как
- //модификатор доступа для структуры public
- struct List_one_Node
- {
- List_one_Node* next = NULL;
- char value;
- List_one_Node* get_next()
- {
- return next;
- }
- char get_value()
- {
- return value;
- }
- void reset_next(List_one_Node* newnext)
- {
- next = newnext;
- }
- };
- // сам список
- struct List_one
- {
- List_one_Node* start = NULL;
- List_one_Node* current = NULL;
- //переход на сл элемент
- void go_next()
- {
- if (current->get_next() != NULL)
- current = current->next;
- }
- //выделяет пямать под List_one_Node
- List_one_Node* create_new_node()
- {
- List_one_Node* newp = (List_one_Node*)malloc(sizeof(List_one_Node));
- if (newp == NULL)
- {
- exit(-1);
- }
- else return newp;
- }
- //используется для ввода строки
- //устанавливает указатель на последний элемент
- void append(char new_value)
- {
- if (current != NULL)
- {
- current->next = create_new_node();
- /*current->reset_next(create_new_node());*/
- (current->next)->next = NULL;
- (current->next)->value = new_value;
- go_next();
- }
- else
- {
- start = create_new_node();
- current = start;
- current->value = new_value;
- }
- }
- void print()
- {
- List_one_Node* st = start;
- while (1) {
- if (st == NULL)
- {
- break;
- }
- printf("%c", st->get_value());
- st = st->get_next();
- }
- }
- void add_by_index(int index, char value)
- {
- List_one_Node* next = NULL;
- List_one_Node* st = start;
- if (index == 1)
- {
- next = start;
- start = create_new_node();
- start->next = next;
- start->value = value;
- }
- else
- {
- for (int i = 1; i < index; i++)
- st = st->next;
- next = st->next;
- st->next = create_new_node();
- st->next->value = value;
- st->next->next = next;
- }
- }
- void delete_by_index(int index) {
- List_one_Node* next = NULL;
- List_one_Node* st = start;
- List_one_Node* nextnext = NULL;
- if (index == 1)
- {
- next = start->next;
- free(start);
- start = next;
- }
- else
- {
- for (int i = 1; i < index - 1; i++)
- st = st->next;
- next = st->next;
- if (next != NULL) {
- nextnext = next->next;
- st->next = nextnext;
- free(next);
- }
- }
- }
- char get_by_index(int index) {
- List_one_Node* next = NULL;
- List_one_Node* st = start;
- if (index == 0)
- {
- return start->get_value();
- }
- else
- {
- for (int i = 1; i < index; i++)
- st = st->next;
- next = st->next;
- if (next != NULL) {
- return next->get_value();
- }
- return '?';
- }
- }
- };
- void test()
- {
- char c;
- List_one Sentence;
- int i = 0;
- while (1)
- {
- c = getchar();
- if (c == '\n')
- {
- break;
- }
- Sentence.append(c);
- i++;
- }
- int lenght = i;
- Sentence.add_by_index(1, 'n');
- Sentence.print();
- Sentence.delete_by_index(2);
- }
- void main()
- {
- char c;
- List_one Sentence;
- int i = 0, length = 0, first = 0, second = 0;
- char simb;
- printf("Enter a string: ");
- while (1)
- {
- c = getchar();
- if (c == '\n')
- {
- break;
- }
- Sentence.append(c);
- i++;
- }
- Sentence.append(' ');
- length = i + 1;
- for (i = 1; i < length; i++)
- {
- if (Sentence.get_by_index(i) == ';') {
- first = i;
- break;
- }
- }
- for (i = first; i < length; i++)
- {
- if (Sentence.get_by_index(i) == ';') {
- second = i;
- break;
- }
- }
- if (second!=0)
- {
- for(i = first; i < second ;i++)
- {
- simb = Sentence.get_by_index(i);
- printf("%c", simb);
- }
- }
- printf("%d", first);
- test();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement