SHOW:
|
|
- or go back to the newest paste.
| 1 | #define _CRT_SECURE_NO_WARNINGS | |
| 2 | #include <stdio.h> | |
| 3 | #include <malloc.h> | |
| 4 | #include <string.h> | |
| 5 | ||
| 6 | void create_queue(struct descriptor *descriptor); //Подпрограмма создания очереди | |
| 7 | void output_queue(struct descriptor *descriptor); //Подпрограмма вывода очереди на экран или в файл | |
| 8 | void add_element(struct descriptor *descriptor); //Подпрограмма добавления элемента в очередь | |
| 9 | void delete_element(struct descriptor *descriptor); //Подпрограмма выборки элемента из очереди | |
| 10 | void clear_queue(struct descriptor *descriptor); //Подпрограмма очистки очереди | |
| 11 | ||
| 12 | struct queue_element | |
| 13 | {
| |
| 14 | char *data; | |
| 15 | struct queue_element *next; | |
| 16 | }; | |
| 17 | struct descriptor | |
| 18 | {
| |
| 19 | struct queue_element *head; | |
| 20 | struct queue_element *tail; | |
| 21 | int exists; | |
| 22 | }; | |
| 23 | ||
| 24 | void main() | |
| 25 | {
| |
| 26 | struct descriptor descriptor; | |
| 27 | int key; | |
| 28 | while (1) //Цикл меню | |
| 29 | {
| |
| 30 | 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 - "); //Выбор действия
| |
| 31 | scanf("%d*[^\n]", &key); //Ввод клавиши действия
| |
| 32 | switch (key) | |
| 33 | {
| |
| 34 | case 1: // Создание очереди | |
| 35 | {
| |
| 36 | create_queue(&descriptor); | |
| 37 | break; | |
| 38 | } | |
| 39 | case 2: //Вывод очереди на экран или в файл | |
| 40 | {
| |
| 41 | output_queue(&descriptor); | |
| 42 | break; | |
| 43 | } | |
| 44 | case 3: //Добавление элемента очереди | |
| 45 | {
| |
| 46 | add_element(&descriptor); | |
| 47 | break; | |
| 48 | } | |
| 49 | case 4: //Удаление (выборка) элемента очереди | |
| 50 | {
| |
| 51 | delete_element(&descriptor); | |
| 52 | printf("Head element was successfully deleted.\n");
| |
| 53 | break; | |
| 54 | } | |
| 55 | case 5: //Очистка (удаление) очереди | |
| 56 | {
| |
| 57 | clear_queue(&descriptor); | |
| 58 | break; | |
| 59 | } | |
| 60 | case 0: | |
| 61 | {
| |
| 62 | printf("Exiting program.\n"); //Выход из программы
| |
| 63 | break; | |
| 64 | } | |
| 65 | default: //Если выбранной команды не существует | |
| 66 | {
| |
| 67 | printf("This feature doesn't exist. Please try again.\n");
| |
| 68 | break; | |
| 69 | } | |
| 70 | } | |
| 71 | if (key == 0) | |
| 72 | break; | |
| 73 | } | |
| 74 | } | |
| 75 | void create_queue(struct descriptor *descriptor) //Подпрограмма создания очереди | |
| 76 | {
| |
| 77 | if (descriptor->exists == 1) | |
| 78 | {
| |
| 79 | printf("Queue already exists. Please, delete it before creating a new one.");
| |
| 80 | return; | |
| 81 | } | |
| 82 | int i, len; | |
| 83 | int N = 20; //Кол-во элементов жёстко задано в задании | |
| 84 | FILE *in; | |
| 85 | char str[256]; | |
| 86 | struct queue_element *ptr; | |
| 87 | ||
| 88 | in = fopen("C:\\test\\SiAODlab3Input.txt", "rt");
| |
| 89 | if (in == NULL) //Если файл не удалось открыть | |
| 90 | {
| |
| 91 | printf("Couldn't open file.\nFile full name: C:\\test\\SiAODlab3Input.txt\n");
| |
| 92 | return; | |
| 93 | } | |
| 94 | ||
| 95 | if (fgets(str, 256, in) == NULL) //Если первая строка не считалась успешно | |
| 96 | {
| |
| 97 | printf("The file is empty or some error has occured while reading file.\nFile full name: C:\\test\\SiAODlab3Input.txt\n");
| |
| 98 | fclose(in); | |
| 99 | return; | |
| 100 | } | |
| 101 | ptr = malloc(sizeof(struct queue_element)); //Выделение памяти под первый элемент очереди | |
| 102 | ptr->next = NULL; //Следующего элемента нет | |
| 103 | descriptor->head = ptr; //Элемент единственный, это голова очереди | |
| 104 | descriptor->tail = ptr; //Элемент единственный, это хвост очереди | |
| 105 | descriptor->exists = 1; | |
| 106 | len = strlen(str); | |
| 107 | if (str[len - 1] == '\n') | |
| 108 | {
| |
| 109 | str[len - 1] = '\0'; | |
| 110 | len = strlen(str); | |
| 111 | } | |
| 112 | ptr->data = calloc(len+1, sizeof(char)); //Выделение памяти под строку | |
| 113 | strcpy(ptr->data, str); | |
| 114 | ||
| 115 | for (i = 1; i < N; i++) | |
| 116 | {
| |
| 117 | if (fgets(str, 256, in) == NULL) //Если очередная строка не считалась успешно | |
| 118 | {
| |
| 119 | printf("The file is empty or some error has occured while reading file.\nFile full name: C:\\test\\SiAODlab3Input.txt\n");
| |
| 120 | fclose(in); | |
| 121 | clear_queue(descriptor); | |
| 122 | return; | |
| 123 | } | |
| 124 | ptr = malloc(sizeof(struct queue_element)); | |
| 125 | ptr->next = NULL; //Это последний элемент | |
| 126 | descriptor->tail->next = ptr; //Запись в прошлый последний элемент указателя на новый последний | |
| 127 | descriptor->tail = ptr; //Запись в дескриптор указателя на новый последний элемент | |
| 128 | len = strlen(str); | |
| 129 | if (str[len - 1] == '\n') | |
| 130 | {
| |
| 131 | str[len - 1] = '\0'; | |
| 132 | len = strlen(str); | |
| 133 | } | |
| 134 | ptr->data = calloc(len+1, sizeof(char)); //Выделение памяти под строку | |
| 135 | strcpy(ptr->data, str); | |
| 136 | } | |
| 137 | fclose(in); | |
| 138 | printf("Queue created succesfully.\n");
| |
| 139 | } | |
| 140 | ||
| 141 | void output_queue(struct descriptor *descriptor) //Подпрограмма вывода очереди на экран или в файл | |
| 142 | {
| |
| 143 | int action; | |
| 144 | struct queue_element *ptr; | |
| 145 | FILE *out; | |
| 146 | ||
| 147 | if (descriptor->exists == 0) | |
| 148 | {
| |
| 149 | printf("The queue doesn't exist.\n");
| |
| 150 | return; | |
| 151 | } | |
| 152 | ||
| 153 | printf("Choose the type of output:\n1 - on the screen.\n2 - in a file.\n");
| |
| 154 | scanf("%d", &action);
| |
| 155 | ||
| 156 | if (action == 1) //Вывод на экран | |
| 157 | {
| |
| 158 | ptr = descriptor->head; | |
| 159 | while (ptr != NULL) | |
| 160 | {
| |
| 161 | printf("%s\n", ptr->data);
| |
| 162 | ptr = ptr->next; | |
| 163 | } | |
| 164 | } | |
| 165 | else if (action == 2) //Вывод в файл | |
| 166 | {
| |
| 167 | out = fopen("C:\\test\\SiAODlab3Output.txt", "wt");
| |
| 168 | if (out == NULL) | |
| 169 | {
| |
| 170 | printf("Couldn't open file");
| |
| 171 | return; | |
| 172 | } | |
| 173 | ptr = descriptor->head; | |
| 174 | while (ptr != NULL) | |
| 175 | {
| |
| 176 | fprintf(out, "%s", ptr->data); | |
| 177 | ptr = ptr->next; | |
| 178 | } | |
| 179 | fclose(out); | |
| 180 | printf("Output file was successfully created and filled.\nFile full name: C:\\test\\SiAODlab3Output.txt\n");
| |
| 181 | } | |
| 182 | else //Некорректоный ввод | |
| 183 | {
| |
| 184 | printf("Incorrect input.\n");
| |
| 185 | return; | |
| 186 | } | |
| 187 | } | |
| 188 | void add_element(struct descriptor *descriptor) //Подпрограмма добавления элемента в очередь | |
| 189 | {
| |
| 190 | struct queue_element *ptr; | |
| 191 | char str[256]; | |
| 192 | int len; | |
| 193 | ||
| 194 | if (descriptor->exists == 0) | |
| 195 | {
| |
| 196 | printf("The queue doesn't exist.\n");
| |
| 197 | return; | |
| 198 | } | |
| 199 | ||
| 200 | printf("Enter data for the new queue element.\n");
| |
| 201 | scanf("%*c");
| |
| 202 | fgets(str, 256, stdin); | |
| 203 | len = strlen(str); | |
| 204 | if (str[len - 1] == '\n') | |
| 205 | {
| |
| 206 | str[len - 1] = '\0'; | |
| 207 | len = strlen(str); | |
| 208 | } | |
| 209 | ptr = malloc(sizeof(struct queue_element)); //Выделение памяти под новый элемент очереди | |
| 210 | ptr->data = calloc(len+1, sizeof(char)); //Выделение памяти под строку | |
| 211 | strcpy(ptr->data, str); | |
| 212 | descriptor->tail->next = ptr; | |
| 213 | ptr->next = NULL; | |
| 214 | descriptor->tail = ptr; | |
| 215 | printf("New queue element was successfully added.\n");
| |
| 216 | } | |
| 217 | void delete_element(struct descriptor *descriptor) //Подпрограмма выборки элемента из очереди | |
| 218 | {
| |
| 219 | struct queue_element *ptr; | |
| 220 | ||
| 221 | if (descriptor->exists == 0) | |
| 222 | {
| |
| 223 | printf("The queue doesn't exist.\n");
| |
| 224 | return; | |
| 225 | } | |
| 226 | ||
| 227 | if (descriptor->head == descriptor->tail) | |
| 228 | {
| |
| 229 | free(descriptor->head->data); | |
| 230 | free(descriptor->head); | |
| 231 | descriptor->head = NULL; | |
| 232 | descriptor->tail = NULL; | |
| 233 | descriptor->exists = 0; | |
| 234 | printf("There was only one queue element, so the whole queue was deleted.\n");
| |
| 235 | } | |
| 236 | else | |
| 237 | {
| |
| 238 | ptr = descriptor->head->next; | |
| 239 | free(descriptor->head->data); | |
| 240 | free(descriptor->head); | |
| 241 | descriptor->head = ptr; | |
| 242 | } | |
| 243 | } | |
| 244 | void clear_queue(struct descriptor *descriptor) //Подпрограмма очистки очереди | |
| 245 | {
| |
| 246 | if (descriptor->exists == 0) | |
| 247 | {
| |
| 248 | printf("The queue doesn't exist.\n");
| |
| 249 | return; | |
| 250 | } | |
| 251 | ||
| 252 | while (descriptor->exists == 1) | |
| 253 | {
| |
| 254 | delete_element(descriptor); | |
| 255 | } | |
| 256 | } |