2024-11-07 21:14:19 +00:00
|
|
|
#include "a_station.h"
|
2024-11-07 20:39:07 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2024-11-07 21:14:19 +00:00
|
|
|
// Создаёт пустую станцию
|
2024-11-07 20:39:07 +00:00
|
|
|
struct station* create_station() {
|
2024-11-07 21:14:19 +00:00
|
|
|
struct station* station = (struct station*)calloc(1, sizeof(struct station));
|
|
|
|
station->tracks = (struct car**)calloc(STATION_SIZE, sizeof(struct car*));
|
2024-11-07 20:39:07 +00:00
|
|
|
station->track_count = STATION_SIZE;
|
|
|
|
return station;
|
|
|
|
}
|
|
|
|
|
2024-11-07 21:14:19 +00:00
|
|
|
// Освобождает всю память
|
|
|
|
void destroy_station(struct station* station) {
|
|
|
|
for (int i = 0; i < station->track_count; i++) {
|
|
|
|
struct car* current = station->tracks[i];
|
|
|
|
while (current) {
|
|
|
|
struct car* to_free = current;
|
|
|
|
current = current->next;
|
|
|
|
free(to_free);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(station->tracks);
|
|
|
|
free(station);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Хеш-функция для выбора пути на основе названия конечной станции
|
2024-11-07 20:39:07 +00:00
|
|
|
int select_track(struct station* station, const char* target) {
|
|
|
|
int hash = 0;
|
2024-11-07 21:14:19 +00:00
|
|
|
for (int i = 0; target[i] != '\0'; i++) {
|
|
|
|
hash = (hash + target[i]) % station->track_count;
|
2024-11-07 20:39:07 +00:00
|
|
|
}
|
2024-11-07 21:14:19 +00:00
|
|
|
return hash;
|
2024-11-07 20:39:07 +00:00
|
|
|
}
|
|
|
|
|
2024-11-07 21:14:19 +00:00
|
|
|
// Добавляет количество пассажиров для указанной станции
|
2024-11-07 20:39:07 +00:00
|
|
|
void add_target_capacity(struct station* station, const char* target, int capacity) {
|
2024-11-07 21:14:19 +00:00
|
|
|
int index = select_track(station, target);
|
|
|
|
struct car* current = station->tracks[index];
|
|
|
|
|
|
|
|
while (current) {
|
2024-11-07 20:39:07 +00:00
|
|
|
if (strcmp(current->value, target) == 0) {
|
2024-11-07 21:14:19 +00:00
|
|
|
current->capacity += capacity;
|
2024-11-07 20:39:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
current = current->next;
|
|
|
|
}
|
2024-11-07 21:14:19 +00:00
|
|
|
|
|
|
|
struct car* new_car = (struct car*)malloc(sizeof(struct car));
|
|
|
|
strncpy(new_car->value, target, TARGET_SIZE - 1);
|
|
|
|
new_car->value[TARGET_SIZE - 1] = '\0';
|
2024-11-07 20:39:07 +00:00
|
|
|
new_car->capacity = capacity;
|
2024-11-07 21:14:19 +00:00
|
|
|
new_car->next = station->tracks[index];
|
|
|
|
station->tracks[index] = new_car;
|
2024-11-07 20:39:07 +00:00
|
|
|
}
|
|
|
|
|
2024-11-07 21:14:19 +00:00
|
|
|
// Получает количество пассажиров для указанной станции
|
|
|
|
int get_target_capacity(struct station* station, const char* target) {
|
|
|
|
int index = select_track(station, target);
|
|
|
|
struct car* current = station->tracks[index];
|
|
|
|
|
|
|
|
while (current) {
|
2024-11-07 20:39:07 +00:00
|
|
|
if (strcmp(current->value, target) == 0) {
|
|
|
|
return current->capacity;
|
|
|
|
}
|
|
|
|
current = current->next;
|
|
|
|
}
|
2024-11-07 21:14:19 +00:00
|
|
|
return 0;
|
2024-11-07 20:39:07 +00:00
|
|
|
}
|
|
|
|
|
2024-11-07 21:14:19 +00:00
|
|
|
// Считает количество конечных станций
|
|
|
|
int count_targets(struct station* station) {
|
|
|
|
int count = 0;
|
2024-11-07 20:39:07 +00:00
|
|
|
for (int i = 0; i < station->track_count; i++) {
|
|
|
|
struct car* current = station->tracks[i];
|
2024-11-07 21:14:19 +00:00
|
|
|
while (current) {
|
|
|
|
count++;
|
2024-11-07 20:39:07 +00:00
|
|
|
current = current->next;
|
|
|
|
}
|
|
|
|
}
|
2024-11-07 21:14:19 +00:00
|
|
|
return count;
|
2024-11-07 20:39:07 +00:00
|
|
|
}
|
|
|
|
|
2024-11-07 21:14:19 +00:00
|
|
|
// Считает общую вместимость всех записей
|
|
|
|
int count_capacity(struct station* station) {
|
|
|
|
int total_capacity = 0;
|
2024-11-07 20:39:07 +00:00
|
|
|
for (int i = 0; i < station->track_count; i++) {
|
|
|
|
struct car* current = station->tracks[i];
|
2024-11-07 21:14:19 +00:00
|
|
|
while (current) {
|
|
|
|
total_capacity += current->capacity;
|
2024-11-07 20:39:07 +00:00
|
|
|
current = current->next;
|
|
|
|
}
|
|
|
|
}
|
2024-11-07 21:14:19 +00:00
|
|
|
return total_capacity;
|
2024-11-07 20:39:07 +00:00
|
|
|
}
|