Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <assert.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- struct Node
- {
- char* data;
- Node* next;
- int* marks;
- int k;
- };
- struct List
- {
- Node* head;
- };
- FILE *S;
- //нахождение последнего элемента
- Node* getLastNodeIfNotEmpty(List* list)
- {
- /*assert(list != NULL);
- assert(!isEmpty(list));*/
- Node* i = list->head;
- while (i->next != NULL)
- {
- i = i->next;
- }
- return i;
- }
- // нахождение предпоследнего
- Node* getLastButOne(List* list)
- {
- /*assert(list != NULL);
- assert(!isEmpty(list));*/
- Node* i = list->head;
- while (i->next != getLastNodeIfNotEmpty(list))
- {
- i = i->next;
- }
- return i;
- }
- void Marks(FILE* S)
- {
- char ch[1];
- ch[0] = fgetc(S);
- printf("\n ch! = %c\n ", ch);
- Node* tmp = (Node*)malloc(sizeof(Node*));
- int* a = NULL;
- int* b;
- int i = 0;
- int k = 0;
- if (!feof(S))
- {
- do
- {
- //printf("\n ch = %c\n", ch[0]);
- if (ch[0] == '/')
- {
- continue;
- }
- b = (int*)realloc(a, (i + 1) * sizeof(int));
- a = b;
- *a = atoi(ch);
- printf("%d", *a);
- i++;
- k++;
- } while (((ch[0] = fgetc(S)) != '#') && (!feof(S)));
- tmp->marks = a;
- tmp->k = k;
- }
- }
- //чтение файла
- void readfile(List* list, FILE *S)
- {
- fopen_s(&S, "input.txt", "rt");
- Node* tmp = (Node*)malloc(sizeof(Node));
- char * tmpc;
- tmp->data = (char*)malloc(1000);
- while (!feof(S))
- {
- //фамилия
- tmpc = tmp->data;
- int ch;
- while(((ch = fgetc(S)) != ' ') && (!feof(S)))
- {
- *(tmpc) = ch;
- tmpc++;
- printf("end");
- }
- *(tmpc) = '\0';
- // оценки
- Marks(S);
- tmp->next = NULL;
- if (list->head == NULL)
- {
- list->head = tmp;
- }
- else
- {
- Node* last = getLastNodeIfNotEmpty(list);
- last->next = tmp;
- }
- }
- fclose(S);
- }
- //enter после чтения файла
- void Enter(List* list)
- {
- Node* p = getLastNodeIfNotEmpty(list);
- char* str = p->data;
- int lengStr = strlen(str);
- if (str[lengStr] == '\0')
- {
- str[lengStr] = '\n';
- str[lengStr + 1] = '\0';
- }
- }
- // вставка в конец
- void insertBack(List* list, char* value, int* assessment)
- {
- assert(list != NULL);
- Node* tmp = (Node*)malloc(sizeof(Node));
- tmp->data = value;
- tmp->marks = assessment;
- tmp->next = NULL;
- if (list->head == NULL)
- {
- list->head = tmp;
- }
- else
- {
- Node* last = getLastNodeIfNotEmpty(list);
- last->next = tmp;
- }
- }
- // пустой список
- bool isEmpty(List* list)
- {
- assert(list != NULL);
- if (list->head == NULL)
- return true;
- else
- return false;
- }
- //размер списка
- int getSize(List* list)
- {
- assert(list != NULL);
- int size = 0;
- if (list->head != NULL)
- {
- size++;
- Node* p = list->head;
- while (p->next != NULL)
- {
- size++;
- p = p->next;
- }
- }
- return size;
- }
- //вставка в любое место
- void insertMiddle(List* list, char* value, int number)
- {
- char *s;
- Node* tmp = (Node*)malloc(sizeof(Node));
- tmp->data = value;
- Node* current = list->head;
- for (int i = 0; i < number; i++)
- {
- current = current->next;
- }
- tmp->next = current->next->next;
- current->next = tmp;
- }
- void swap(char* a, char* b)
- {
- char c;
- c = *a;
- *a = *b;
- *b = c;
- }
- bool sort_two(Node* first_node)
- {
- assert(first_node != NULL);
- assert(first_node->next != NULL);
- if (strcmp(first_node->data, first_node->next->data) > 0)
- //if (first_node->data > first_node->next->data)
- {
- swap(first_node->data, first_node->next->data);
- return true;
- }
- return false;
- }
- //нахождение последнего элемента
- bool isLast(Node* node)
- {
- if (node->next == NULL)
- return true;
- else
- return false;
- }
- void sort(List* list)
- {
- if (isEmpty(list))
- return;
- int size = getSize(list);
- if (size == 1)
- return;
- else if (size > 1)
- {
- for (int k = 0; k < size; k++)
- {
- Node* i = list->head;
- while (!isLast(i))
- {
- sort_two(i);
- i = i->next;
- }
- }
- }
- }
- void printList(List* list)
- {
- printf("Zahod");
- assert(list != NULL);
- Node* i = list->head;
- while (i != NULL)
- {
- printf("%s ", i->data);
- for (int m = 0; m < i->k; m++)
- {
- printf("m");
- printf("%d", i->marks);
- }
- i = i->next;
- }
- }
- List makeEmptyList()
- {
- List empty_list;
- empty_list.head = NULL;
- return empty_list;
- }
- void writeList(List* list, int number)
- {
- for (int i = 0; i < number; i++)
- {
- printf("Vvedite element: ");
- Node* UsingMem = (Node*)malloc(sizeof(Node));
- char* str = (char*)malloc(1000);
- gets_s(str, 1000);
- UsingMem->data = str;
- insertBack(list, UsingMem->data, UsingMem->marks);
- }
- }
- void Delete(List* list, int number)
- {
- Node* current = list->head;
- if (number == 0)
- {
- list->head = current->next;
- free(current);
- }
- else
- {
- if (number == getSize(list))
- {
- Node* p = getLastButOne(list);// нахождение препоследнего элемента
- Node* d = p->next; //хранение адреса последнего элемента
- p->next = NULL;
- free(d);
- }
- else
- {
- for (int i = 0; i < number; i++)
- {
- current = current->next;
- }
- Node* elm = current->next;
- current->next = elm->next;
- free(elm);
- }
- }
- }
- //Разделение строки
- void SplitStr(char* str)
- {
- char sep[10] = " ";
- char* istr;
- printf("Исходная строка: ", str);
- //printf("Разделенная строка: ");
- char* next_token1 = NULL;
- istr = strtok_s(str, sep, &next_token1);
- /*while (istr != NULL)
- {
- // Вывод очередной выделенной части
- printf("%s\n", i str);
- // Выделение очередной части строки
- istr = strtok_s(NULL, sep, &next_token1);
- }*/
- }
- int main()
- {
- List l = makeEmptyList();
- //isEmpty(&l) == true;
- //Marks(&l);
- readfile(&l, S);
- //Enter(&l);
- printList(&l);
- printf("Vvedite element: ");
- //gets_s(value, 1000);
- char* str = (char*)malloc(1000);
- char* value = (char*)malloc(1000);
- //char value[];
- printf("Vvedite element: ");
- gets_s(str, 1000);
- gets_s(value, 1000);
- int value1 = atoi(value);
- insertBack(&l, str, &value1);
- printList(&l);
- int n;
- scanf_s("%d", &n);
- Delete(&l, n);
- printList(&l);
- int number;
- scanf_s("%d", &number);
- printf("Before sort:\n ");
- printList(&l);
- sort(&l);
- printf("After sort:\n ");
- printList(&l);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement