usaa24/cv6/main.c

107 lines
3.0 KiB
C
Raw Permalink Normal View History

2024-10-29 14:22:46 +00:00
#include <stdio.h>
2024-11-19 14:42:02 +00:00
#include <stdlib.h>
#include <string.h>
2024-10-29 14:22:46 +00:00
#include "a_station.h"
2024-11-19 14:42:02 +00:00
// Загрузка данных из файла
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);
}
// Основная функция
2024-10-29 14:22:46 +00:00
int main() {
struct station* station = create_station();
2024-11-19 14:42:02 +00:00
if (!station) {
printf("Error creating station!\n");
return 1;
}
2024-10-29 14:22:46 +00:00
2024-11-19 14:42:02 +00:00
user_menu(station);
2024-10-29 14:22:46 +00:00
destroy_station(station);
return 0;
}