Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <malloc.h>
- #include <string.h>
- void create_queue(struct descriptor *descriptor); //Подпрограмма создания очереди
- void output_queue(struct descriptor *descriptor); //Подпрограмма вывода очереди на экран или в файл
- void add_element(struct descriptor *descriptor); //Подпрограмма добавления элемента в очередь
- void delete_element(struct descriptor *descriptor); //Подпрограмма выборки элемента из очереди
- void clear_queue(struct descriptor *descriptor); //Подпрограмма очистки очереди
- struct queue_element
- {
- char *data;
- struct queue_element *next;
- };
- struct descriptor
- {
- struct queue_element *head;
- struct queue_element *tail;
- int exists;
- };
- void main()
- {
- struct descriptor descriptor;
- int key;
- while (1) //Цикл меню
- {
- printf("Choose function.\n1 - create queue.\n2 - output queue.\n3 - add new element in queue.\n4 - delete element from queue.\n5 - delete queue.\n0 - exit.\nChosen function - "); //Выбор действия
- scanf("%d*[^\n]", &key); //Ввод клавиши действия
- switch (key)
- {
- case 1: // Создание очереди
- {
- create_queue(&descriptor);
- break;
- }
- case 2: //Вывод очереди на экран или в файл
- {
- output_queue(&descriptor);
- break;
- }
- case 3: //Добавление элемента очереди
- {
- add_element(&descriptor);
- break;
- }
- case 4: //Удаление (выборка) элемента очереди
- {
- delete_element(&descriptor);
- printf("Head element was successfully deleted.\n");
- break;
- }
- case 5: //Очистка (удаление) очереди
- {
- clear_queue(&descriptor);
- break;
- }
- case 0:
- {
- printf("Exiting program.\n"); //Выход из программы
- break;
- }
- default: //Если выбранной команды не существует
- {
- printf("This feature doesn't exist. Please try again.\n");
- break;
- }
- }
- if (key == 0)
- break;
- }
- }
- void create_queue(struct descriptor *descriptor) //Подпрограмма создания очереди
- {
- if (descriptor->exists == 1)
- {
- printf("Queue already exists. Please, delete it before creating a new one.");
- return;
- }
- int i, len;
- int N = 20; //Кол-во элементов жёстко задано в задании
- FILE *in;
- char str[256];
- struct queue_element *ptr;
- in = fopen("C:\\test\\SiAODlab3Input.txt", "rt");
- if (in == NULL) //Если файл не удалось открыть
- {
- printf("Couldn't open file.\nFile full name: C:\\test\\SiAODlab3Input.txt\n");
- return;
- }
- if (fgets(str, 256, in) == NULL) //Если первая строка не считалась успешно
- {
- printf("The file is empty or some error has occured while reading file.\nFile full name: C:\\test\\SiAODlab3Input.txt\n");
- fclose(in);
- return;
- }
- ptr = malloc(sizeof(struct queue_element)); //Выделение памяти под первый элемент очереди
- ptr->next = NULL; //Следующего элемента нет
- descriptor->head = ptr; //Элемент единственный, это голова очереди
- descriptor->tail = ptr; //Элемент единственный, это хвост очереди
- descriptor->exists = 1;
- len = strlen(str);
- if (str[len - 1] == '\n')
- {
- str[len - 1] = '\0';
- len = strlen(str);
- }
- ptr->data = calloc(len+1, sizeof(char)); //Выделение памяти под строку
- strcpy(ptr->data, str);
- for (i = 1; i < N; i++)
- {
- if (fgets(str, 256, in) == NULL) //Если очередная строка не считалась успешно
- {
- printf("The file is empty or some error has occured while reading file.\nFile full name: C:\\test\\SiAODlab3Input.txt\n");
- fclose(in);
- clear_queue(descriptor);
- return;
- }
- ptr = malloc(sizeof(struct queue_element));
- ptr->next = NULL; //Это последний элемент
- descriptor->tail->next = ptr; //Запись в прошлый последний элемент указателя на новый последний
- descriptor->tail = ptr; //Запись в дескриптор указателя на новый последний элемент
- len = strlen(str);
- if (str[len - 1] == '\n')
- {
- str[len - 1] = '\0';
- len = strlen(str);
- }
- ptr->data = calloc(len+1, sizeof(char)); //Выделение памяти под строку
- strcpy(ptr->data, str);
- }
- fclose(in);
- printf("Queue created succesfully.\n");
- }
- void output_queue(struct descriptor *descriptor) //Подпрограмма вывода очереди на экран или в файл
- {
- int action;
- struct queue_element *ptr;
- FILE *out;
- if (descriptor->exists == 0)
- {
- printf("The queue doesn't exist.\n");
- return;
- }
- printf("Choose the type of output:\n1 - on the screen.\n2 - in a file.\nEntered: ");
- scanf("%d", &action);
- if (action == 1) //Вывод на экран
- {
- ptr = descriptor->head;
- while (ptr != NULL)
- {
- printf("%s\n", ptr->data);
- ptr = ptr->next;
- }
- }
- else if (action == 2) //Вывод в файл
- {
- out = fopen("C:\\test\\SiAODlab3Output.txt", "wt");
- if (out == NULL)
- {
- printf("Couldn't open file");
- return;
- }
- ptr = descriptor->head;
- while (ptr != NULL)
- {
- fprintf(out, "%s", ptr->data);
- ptr = ptr->next;
- }
- fclose(out);
- printf("Output file was successfully created and filled.\nFile full name: C:\\test\\SiAODlab3Output.txt\n");
- }
- else //Некорректоный ввод
- {
- printf("Incorrect input.\n");
- return;
- }
- }
- void add_element(struct descriptor *descriptor) //Подпрограмма добавления элемента в очередь
- {
- struct queue_element *ptr;
- char str[256];
- int len;
- if (descriptor->exists == 0)
- {
- printf("The queue doesn't exist.\n");
- return;
- }
- printf("Enter data for the new queue element.\n");
- scanf("%*c");
- fgets(str, 256, stdin);
- len = strlen(str);
- if (str[len - 1] == '\n')
- {
- str[len - 1] = '\0';
- len = strlen(str);
- }
- ptr = malloc(sizeof(struct queue_element)); //Выделение памяти под новый элемент очереди
- ptr->data = calloc(len+1, sizeof(char)); //Выделение памяти под строку
- strcpy(ptr->data, str);
- descriptor->tail->next = ptr;
- ptr->next = NULL;
- descriptor->tail = ptr;
- printf("New queue element was successfully added.\n");
- }
- void delete_element(struct descriptor *descriptor) //Подпрограмма выборки элемента из очереди
- {
- struct queue_element *ptr;
- if (descriptor->exists == 0)
- {
- printf("The queue doesn't exist.\n");
- return;
- }
- if (descriptor->head == descriptor->tail)
- {
- free(descriptor->head->data);
- free(descriptor->head);
- descriptor->head = NULL;
- descriptor->tail = NULL;
- descriptor->exists = 0;
- printf("Queue was deleted.\n");
- }
- else
- {
- ptr = descriptor->head->next;
- free(descriptor->head->data);
- free(descriptor->head);
- descriptor->head = ptr;
- }
- }
- void clear_queue(struct descriptor *descriptor) //Подпрограмма очистки очереди
- {
- if (descriptor->exists == 0)
- {
- printf("The queue doesn't exist.\n");
- return;
- }
- while (descriptor->exists == 1)
- {
- delete_element(descriptor);
- }
- }
Add Comment
Please, Sign In to add comment