Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include "Header.h"
- int get_int()
- {
- int i, n;
- do
- {
- i = scanf_s("%d", &n);
- if (!i)
- {
- rewind(stdin);
- continue;
- }
- } while (!i);
- return n;
- }
- char* get_string()
- {
- char c;
- int lenght = 0;
- char* string = (char*)malloc(sizeof(char));
- while (1)
- {
- rewind(stdin);
- c = _getch();
- fputc(c, stdout);
- if (c == '\b')
- {
- fprintf(stdout, " ");
- fputc(c, stdout);
- if (lenght >= 1)
- {
- string = (char*)realloc(string, sizeof(char) * lenght - 1);
- string = (char*)realloc(string, sizeof(char) * lenght);
- lenght--;
- }
- }
- if (c != '\n' && c != '\r')
- {
- if (c != '\b')
- {
- string[lenght++] = c;
- string = (char*)realloc(string, sizeof(char) * (lenght + 1));
- }
- }
- else
- {
- string[lenght] = '\0';
- break;
- }
- }
- fprintf(stdout, "\n");
- return string;
- }
- int get_year()
- {
- int year;
- do
- {
- year = get_int();
- } while (year < 1 || year > 11);
- return year;
- }
- int get_positive()
- {
- int n;
- do
- {
- n = get_int();
- } while (n <= 0);
- return n;
- }
- int* get_ints(int size)
- {
- int* array = (int*)malloc(sizeof(int) * size);
- int i;
- for (i = 0; i < size; i++)
- {
- array[i] = get_positive();
- }
- return array;
- }
- struct student* get_student()
- {
- struct student* stud = (struct student*)malloc(sizeof(struct student));
- printf("Введите имя ученика:");
- stud->name = get_string();
- printf("Введите фамилию ученикa:");
- stud->surname = get_string();
- printf("Введите отчесто ученика:");
- stud->patronimyc = get_string();
- printf("Введите год обучения(номер класса):");
- stud->year = get_year();
- printf("Введите букву класса:");
- rewind(stdin);
- stud->letter = _getch();
- fputc(stud->letter, stdout);
- printf("\n");
- printf("Введите количество предметов:");
- stud->marks_numb = get_positive();
- printf("Введите оценки:\n");
- stud->marks = get_ints(stud->marks_numb);
- return stud;
- }
- struct student* student_delete(struct student* stud)
- {
- if (stud != NULL)
- {
- free(stud->name);
- free(stud->surname);
- free(stud->patronimyc);
- free(stud->marks);
- free(stud);
- }
- stud = NULL;
- return stud;
- }
- void student_print(struct student* stud)
- {
- if (stud == NULL)
- {
- return;
- }
- printf("%d\t%c %s\t%s\t%s", stud->year, stud->letter, stud->name, stud->surname, stud->patronimyc);
- printf("\tОценки: ");
- for (int i = 0; i < stud->marks_numb; i++)
- {
- printf("%2d ", stud->marks[i]);
- }
- printf("\n");
- }
- int students_cmp(struct student* stud1, struct student* stud2)
- {
- if (stud1 == NULL || stud2 == NULL) return 0;
- if (strcmp(stud1->name, stud2->name) != 0) return 0;
- if (strcmp(stud1->surname, stud2->surname) != 0) return 0;
- if (strcmp(stud1->patronimyc, stud2->patronimyc) != 0) return 0;
- if (stud1->year != stud2->year || stud1->letter != stud2->letter) return 0;
- if (stud1->marks_numb != stud2->marks_numb) return 0;
- for (int i = 0; i < stud1->marks_numb; i++)
- {
- if (stud1->marks[i] != stud2->marks[i]) return 0;
- }
- return 1;
- }
- void student_copy(struct student* source, struct student* destinition)
- {
- if (!source) return;
- destinition->letter = source->letter;
- destinition->marks_numb = source->marks_numb;
- destinition->year = source->year;
- destinition->name = (char*)malloc(sizeof(char) * (strlen(source->name) + 1));
- strcpy(destinition->name, source->name);
- destinition->surname = (char*)malloc(sizeof(char) * (strlen(source->surname) + 1));
- strcpy(destinition->surname, source->surname);
- destinition->patronimyc = (char*)malloc(sizeof(char) * (strlen(source->patronimyc) + 1));
- strcpy(destinition->patronimyc, source->patronimyc);
- destinition->marks = (int*)malloc(sizeof(int) * destinition->marks_numb);
- int i;
- for (i = 0; i < destinition->marks_numb; i++)
- {
- destinition->marks[i] = source->marks[i];
- }
- }
- struct list* add_element(struct list* ring, struct student* stud)
- {
- if (!ring)
- {
- ring = (struct list*)malloc(sizeof(struct list));
- ring->stud = (struct student*)malloc(sizeof(struct student));
- student_copy(stud, ring->stud);
- ring->left = ring;
- ring->right = ring;
- return ring;
- }
- struct list* temp = ring->left;
- ring->left = (struct list*)malloc(sizeof(struct list));
- ring->left->stud = (struct student*)malloc(sizeof(struct student));
- student_copy(stud, ring->left->stud);
- ring->left->left = temp;
- ring->left->right = ring;
- temp->right = ring->left;
- return ring;
- }
- struct list* move(struct list* el, char direction)
- {
- if (direction == 'l')
- {
- el = el->left;
- }
- else
- {
- el = el->right;
- }
- return el;
- }
- void show(struct list* ring, char direction)
- {
- if (!ring)
- {
- return;
- }
- struct list* temp = ring;
- printf("Класс Буква Имя Фамилия Отчество Оценки\n");
- do
- {
- student_print(temp->stud);
- temp = move(temp, direction);
- } while (temp != ring);
- }
- struct list* search(struct list* head, struct student* stud)
- {
- if (!head || students_cmp(head->stud, stud))
- {
- return head;
- }
- struct list* temp = head;
- do
- {
- if (students_cmp(temp->stud, stud))
- {
- return temp;
- }
- temp = move(temp, 'r');
- } while (temp != head);
- return NULL;
- }
- struct list* delete_node(struct list* ring, struct student* stud)
- {
- struct list* temp = search(ring, stud);
- if (!temp)
- {
- return ring;
- }
- if (temp == ring)
- {
- if (ring->left == ring)
- {
- student_delete(ring->stud);
- free(ring);
- return NULL;
- }
- ring->left->right = ring->right;
- ring->right->left = ring->left;
- ring = ring->right;
- student_delete(temp->stud);
- free(temp);
- return ring;
- }
- temp->left->right = temp->right;
- temp->right->left = temp->left;
- student_delete(temp->stud);
- free(temp);
- return ring;
- }
- void ring_free(struct list** ring)
- {
- while (*ring)
- {
- *ring = delete_node(*ring, (*ring)->stud);
- }
- }
- void sort_by_value(struct list* head)
- {
- struct list* i = head, *j;
- struct list* min_p;
- struct student* temp;
- int min;
- do
- {
- min = i->stud->year;
- min_p = i;
- for (j = i->right; j != head; j = j->right)
- {
- if (j->stud->year < min)
- {
- min = j->stud->year;
- min_p = j;
- }
- }
- if (min_p != i)
- {
- temp = i->stud;
- i->stud = min_p->stud;
- min_p->stud = temp;
- }
- i = i->right;
- } while (i->right != head);
- }
- struct list* sort_by_pointer(struct list* head)
- {
- struct list* i = head;
- struct list* min_p, *j;
- int min;
- do
- {
- min_p = i;
- min = i->stud->year;
- for (j = i->right; j != head; j = j->right)
- {
- if (min > j->stud->year)
- {
- min_p = j;
- min = j->stud->year;
- }
- }
- if (min_p != i)
- {
- if (i == head)
- {
- head = min_p;
- i = head->right;
- }
- else
- {
- min_p->left->right = min_p->right;
- min_p->right->left = min_p->left;
- i->left->right = min_p;
- min_p->right = i;
- min_p->left = i->left;
- i->left = min_p;
- }
- }
- else
- {
- i = i->right;
- }
- } while (i->right != head);
- return head;
- }
- int choose()
- {
- int n;
- do
- {
- printf("Выберите действие:\n");
- n = get_int();
- } while (n < 1 || n > 9);
- return n;
- }
- struct list* search_by_int(struct list* head, int parametr, int n)
- {
- if (head == NULL)
- return NULL;
- struct list* t = head;
- do
- {
- switch (parametr)
- {
- case 1:
- if (t->stud->year == n)
- {
- return t;
- }
- break;
- case 2:
- if (t->stud->marks_numb == n)
- {
- return t;
- }
- default:
- break;
- }
- t = t->right;
- } while (t != head);
- return NULL;
- }
- struct list* search_by_string(struct list* head, int parametr, char* n)
- {
- if (head == NULL)
- return NULL;
- struct list* t = head;
- do
- {
- switch (parametr)
- {
- case 1:
- if (strcmp(t->stud->name, n) == 0)
- {
- return t;
- }
- break;
- case 2:
- if (strcmp(t->stud->surname, n) == 0)
- {
- return t;
- }
- break;
- case 3:
- if (strcmp(t->stud->patronimyc, n) == 0)
- {
- return t;
- }
- break;
- default:
- break;
- }
- t = t->right;
- } while (t != head);
- return NULL;
- }
- struct list* search_by_char(struct list* head, char n)
- {
- if (head == NULL) return NULL;
- struct list* t = head;
- do
- {
- if (t->stud->letter == n)
- {
- return t;
- }
- t = t->right;
- } while (t != head);
- return NULL;
- }
- struct list* search_by_any(struct list* head)
- {
- printf("1)Поиск по году обучению\n");
- printf("2)Поиск по количеству оценок\n");
- printf("3)Поиск по имени\n");
- printf("4)Поиск по фамилии\n");
- printf("5)Поиск по отчеству\n");
- printf("6)Поиск по букве класса\n");
- int ch = get_int();
- int n;
- char c;
- char* str = NULL;
- struct list* temp = NULL;
- switch (ch)
- {
- case 1: //поиск по году
- printf("Введите год для поиска:\n");
- n = get_int();
- temp = search_by_int(head, 1, n);
- break;
- case 2: //поиск по количеству оценок
- printf("Введите кол-во оценок для поиска:\n");
- n = get_int();
- temp = search_by_int(head, 2, n);
- break;
- case 3: //поиск по имени
- printf("Введите имя для поиска:\n");
- str = get_string();
- temp = search_by_string(head, 1, str);
- break;
- case 4: //поиск по фамилии
- printf("Введите фамилию для поиска:\n");
- str = get_string();
- temp = search_by_string(head, 2, str);
- break;
- case 5: //поиск по фамилии
- printf("Введите отчество для поиска:\n");
- str = get_string();
- temp = search_by_string(head, 3, str);
- break;
- case 6: //поиск по букве классаЧ
- printf("Введите буквy класса для поиска:\n");
- c = _getch();
- fputc(c, stdout);
- printf("\n");
- temp = search_by_char(head, c);
- break;
- default:
- break;
- }
- return temp;
- }
- void info()
- {
- printf("1)Добавление элемента в кольцо\n");
- printf("2)Удаление элемента из кольца\n");
- printf("3)Вывод справа налево\n");
- printf("4)Вывод слева направо\n");
- printf("5)Сортировка по указателю\n");
- printf("6)Сортировка по значению\n");
- printf("7)Поиск элемента\n");
- printf("8)Закончить\n");
- printf("9)Поиск по выбранному параметру\n");
- }
- void choose(struct list** ring, struct student* stud)
- {
- struct list* temp = NULL;
- int ch;
- info();
- do
- {
- ch = choose();
- switch (ch)
- {
- case 1:
- stud = get_student();
- *ring = add_element(*ring, stud);
- student_delete(stud);
- break;
- case 2:
- stud = get_student();
- *ring = delete_node(*ring, stud);
- stud = student_delete(stud);
- break;
- case 3:
- show(*ring, 'r');
- break;
- case 4:
- show(*ring, 'l');
- break;
- case 5:
- *ring = sort_by_pointer(*ring);
- break;
- case 6:
- sort_by_value(*ring);
- break;
- case 7:
- stud = get_student();
- temp = search(*ring, stud);
- stud = student_delete(stud);
- if (!temp)
- {
- printf("Элемент не сществует\n");
- }
- else
- {
- printf("Элемент существует\n");
- }
- break;
- case 8:
- ring_free(ring);
- break;
- case 9:
- temp = search_by_any(*ring);
- if (temp == NULL)
- {
- printf("Элемент не найден\n");
- }
- else
- {
- printf("Элемент найден\n");
- printf("Класс Буква Имя Фамилия Отчество Оценки\n");
- student_print(temp->stud);
- }
- break;
- default:
- break;
- }
- } while (ch != 8);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement