This commit is contained in:
Deinerovych 2024-11-19 15:48:04 +01:00
parent b402f7565a
commit d7843cedbc
3 changed files with 45 additions and 11 deletions

View File

@ -3,6 +3,7 @@
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
// Добавление вагона в конец списка
struct car* add_car(struct car* first, const char* target) { struct car* add_car(struct car* first, const char* target) {
struct car* new_car = (struct car*)malloc(sizeof(struct car)); struct car* new_car = (struct car*)malloc(sizeof(struct car));
if (!new_car) { if (!new_car) {
@ -26,20 +27,23 @@ struct car* add_car(struct car* first, const char* target) {
return first; return first;
} }
// Вывод содержимого поезда
void print_train(struct car* first) { void print_train(struct car* first) {
struct car* current = first; struct car* current = first;
if (current == NULL) { if (current == NULL) {
printf("Vlak je prazdny.\n"); printf("The train is empty.\n");
return; return;
} }
printf("Train composition:\n");
while (current != NULL) { while (current != NULL) {
printf("%s\n", current->value); printf("%s\n", current->value);
current = current->next; current = current->next;
} }
} }
// Удаление всех вагонов, освобождение памяти
void cancel_train(struct car* first) { void cancel_train(struct car* first) {
struct car* current = 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* clear_train(struct car* first, const char* target) {
struct car* current = first; struct car* current = first;
struct car* prev = NULL; struct car* prev = NULL;

View File

@ -1,22 +1,51 @@
#include "a_train.h" #include "a_train.h"
#include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <string.h>
int main() { int main() {
struct car* train = NULL; struct car* train = NULL;
char input[SIZE];
int option;
train = add_car(train, "Bratislava"); do {
train = add_car(train, "Kosice"); printf("\n--- Train Management ---\n");
train = add_car(train, "Zilina"); 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"); switch (option) {
print_train(train); 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"); case 2:
train = clear_train(train, "Kosice"); print_train(train);
print_train(train); break;
printf("\nZrusenie celeho vlaku:\n"); case 3:
cancel_train(train); 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; return 0;
} }

Binary file not shown.