diff --git a/cv6/a_station.c b/cv6/a_station.c index 3492502..6096ca0 100644 --- a/cv6/a_station.c +++ b/cv6/a_station.c @@ -1,7 +1,9 @@ +#include #include #include #include "a_station.h" +// Создание пустой станции struct station* create_station() { struct station* new_station = malloc(sizeof(struct station)); if (!new_station) return NULL; @@ -11,6 +13,7 @@ struct station* create_station() { return new_station; } +// Освобождение памяти станции void destroy_station(struct station* station) { for (int i = 0; i < station->track_count; i++) { struct car* current = station->tracks[i]; @@ -24,6 +27,7 @@ void destroy_station(struct station* station) { free(station); } +// Хеш-функция для выбора пути int select_track(struct station* station, const char* target) { unsigned int hash = 0; for (int i = 0; target[i] != '\0'; i++) { @@ -32,6 +36,7 @@ int select_track(struct station* station, const char* target) { return hash % station->track_count; } +// Добавление или обновление записи для направления void add_target_capacity(struct station* station, const char* target, int capacity) { int index = select_track(station, target); struct car* current = station->tracks[index]; @@ -52,6 +57,7 @@ void add_target_capacity(struct station* station, const char* target, int capaci station->tracks[index] = new_car; } +// Получение ёмкости направления int get_target_capacity(struct station* station, const char* target) { int index = select_track(station, target); struct car* current = station->tracks[index]; @@ -65,6 +71,7 @@ int get_target_capacity(struct station* station, const char* target) { return 0; } +// Подсчёт количества направлений int count_targets(struct station* station) { int count = 0; for (int i = 0; i < station->track_count; i++) { @@ -77,6 +84,7 @@ int count_targets(struct station* station) { return count; } +// Подсчёт общей ёмкости int count_capacity(struct station* station) { int total_capacity = 0; for (int i = 0; i < station->track_count; i++) { diff --git a/cv6/main.c b/cv6/main.c index e20ff9c..fe8c35d 100644 --- a/cv6/main.c +++ b/cv6/main.c @@ -1,16 +1,104 @@ #include +#include +#include #include "a_station.h" +// Загрузка данных из файла +void load_station_data(struct station* station, const char* filename) { + FILE* file = fopen(filename, "r"); + if (!file) { + perror("Error opening file"); + return; + } + + char destination[TARGET_SIZE]; + int capacity; + + while (fscanf(file, "%s %d", destination, &capacity) == 2) { + add_target_capacity(station, destination, capacity); + } + + fclose(file); + printf("Data successfully loaded from file '%s'.\n", filename); +} + +// Поиск информации по направлению +void find_destination(struct station* station, const char* destination) { + int capacity = get_target_capacity(station, destination); + if (capacity > 0) { + printf("Destination: %s, Capacity: %d\n", destination, capacity); + } else { + printf("Destination '%s' not found.\n", destination); + } +} + +// Пользовательское меню +void user_menu(struct station* station) { + int choice; + char filename[100]; + char destination[TARGET_SIZE]; + int capacity; + + do { + printf("\n--- Menu ---\n"); + printf("1. Load data from file\n"); + printf("2. Add or update destination\n"); + printf("3. Find destination\n"); + printf("4. Show total number of destinations\n"); + printf("5. Show total capacity\n"); + printf("6. Exit\n"); + printf("Select an option: "); + scanf("%d", &choice); + + switch (choice) { + case 1: + printf("Enter the filename: "); + scanf("%s", filename); + load_station_data(station, filename); + break; + + case 2: + printf("Enter the destination: "); + scanf("%s", destination); + printf("Enter the capacity: "); + scanf("%d", &capacity); + add_target_capacity(station, destination, capacity); + printf("Destination '%s' has been added or updated.\n", destination); + break; + + case 3: + printf("Enter the destination to search: "); + scanf("%s", destination); + find_destination(station, destination); + break; + + case 4: + printf("Total number of destinations: %d\n", count_targets(station)); + break; + + case 5: + printf("Total capacity: %d\n", count_capacity(station)); + break; + + case 6: + printf("Exiting the program.\n"); + break; + + default: + printf("Invalid option, please try again.\n"); + } + } while (choice != 6); +} + +// Основная функция int main() { struct station* station = create_station(); + if (!station) { + printf("Error creating station!\n"); + return 1; + } - add_target_capacity(station, "Bratislava", 100); - add_target_capacity(station, "Kosice", 150); - add_target_capacity(station, "Bratislava", 50); - - printf("Capacity to Bratislava: %d\n", get_target_capacity(station, "Bratislava")); - printf("Total number of targets: %d\n", count_targets(station)); - printf("Total capacity: %d\n", count_capacity(station)); + user_menu(station); destroy_station(station); return 0;