From d7843cedbcd8223a6b79f32b80eaecc8db43c91f Mon Sep 17 00:00:00 2001 From: Deinerovych Date: Tue, 19 Nov 2024 15:48:04 +0100 Subject: [PATCH] final --- cv4/a_train.c | 7 ++++++- cv4/main.c | 49 ++++++++++++++++++++++++++++++++++++--------- cv7/.program.c.swp | Bin 12288 -> 12288 bytes 3 files changed, 45 insertions(+), 11 deletions(-) diff --git a/cv4/a_train.c b/cv4/a_train.c index e1c3f29..6320c29 100644 --- a/cv4/a_train.c +++ b/cv4/a_train.c @@ -3,6 +3,7 @@ #include #include +// Добавление вагона в конец списка struct car* add_car(struct car* first, const char* target) { struct car* new_car = (struct car*)malloc(sizeof(struct car)); if (!new_car) { @@ -26,20 +27,23 @@ struct car* add_car(struct car* first, const char* target) { return first; } +// Вывод содержимого поезда void print_train(struct car* first) { struct car* current = first; if (current == NULL) { - printf("Vlak je prazdny.\n"); + printf("The train is empty.\n"); return; } + printf("Train composition:\n"); while (current != NULL) { printf("%s\n", current->value); current = current->next; } } +// Удаление всех вагонов, освобождение памяти void cancel_train(struct car* first) { struct car* current = first; @@ -50,6 +54,7 @@ void cancel_train(struct car* first) { } } +// Удаление вагонов с заданным направлением struct car* clear_train(struct car* first, const char* target) { struct car* current = first; struct car* prev = NULL; diff --git a/cv4/main.c b/cv4/main.c index 9e84f5c..931c238 100644 --- a/cv4/main.c +++ b/cv4/main.c @@ -1,22 +1,51 @@ #include "a_train.h" +#include #include +#include int main() { struct car* train = NULL; + char input[SIZE]; + int option; - train = add_car(train, "Bratislava"); - train = add_car(train, "Kosice"); - train = add_car(train, "Zilina"); + do { + printf("\n--- Train Management ---\n"); + printf("1. Add a car to the train\n"); + printf("2. Print the train\n"); + printf("3. Remove cars to Levoča\n"); + printf("4. Exit\n"); + printf("Select an option: "); + scanf("%d", &option); + getchar(); // Считываем символ новой строки после ввода числа - printf("Vsetky stanice vo vlaku:\n"); - print_train(train); + switch (option) { + case 1: + printf("Enter the destination: "); + fgets(input, SIZE, stdin); + input[strcspn(input, "\n")] = '\0'; // Удаляем символ новой строки + train = add_car(train, input); + printf("Car added to the train.\n"); + break; - printf("\nVyradujeme stanicu 'Kosice':\n"); - train = clear_train(train, "Kosice"); - print_train(train); + case 2: + print_train(train); + break; - printf("\nZrusenie celeho vlaku:\n"); - cancel_train(train); + case 3: + train = clear_train(train, "Levoča"); + printf("All cars to Levoča have been removed.\n"); + break; + + case 4: + printf("Exiting and releasing memory.\n"); + cancel_train(train); + train = NULL; + break; + + default: + printf("Invalid option. Please try again.\n"); + } + } while (option != 4); return 0; } diff --git a/cv7/.program.c.swp b/cv7/.program.c.swp index 036648593c2a403246f948a7cd707514300e21f4..e76b68277c4011fe9a0d29b222da09a60b2f57cb 100644 GIT binary patch delta 194 zcmZojXh_%~Ai$`-Sy13JKW_y$1A_w(0|SEq1B3QtL51m34}iR0AWi~ee<1b)Vn-l$ z;MvT`QqL``VQ6Nekd&BJl3%9Dz~G&qmzkRUXu{&M;*!#W(#h-f^@IFV3i8WRQ-DfR zffDM8>Oiuf2*fTZDk}w2x%pLj`NbtbR$@+SQa+GY&qF9mDJsoCQl