final
This commit is contained in:
		
							parent
							
								
									436029f166
								
							
						
					
					
						commit
						b402f7565a
					
				| @ -1,7 +1,9 @@ | ||||
| #include <stdio.h> | ||||
| #include <stdlib.h> | ||||
| #include <string.h> | ||||
| #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++) { | ||||
|  | ||||
							
								
								
									
										102
									
								
								cv6/main.c
									
									
									
									
									
								
							
							
						
						
									
										102
									
								
								cv6/main.c
									
									
									
									
									
								
							| @ -1,16 +1,104 @@ | ||||
| #include <stdio.h> | ||||
| #include <stdlib.h> | ||||
| #include <string.h> | ||||
| #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; | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user