This commit is contained in:
Deinerovych 2024-10-29 15:27:15 +01:00
parent 5b78da9114
commit 67da704635

View File

@ -25,11 +25,11 @@ void destroy_station(struct station* station) {
} }
int select_track(struct station* station, const char* target) { int select_track(struct station* station, const char* target) {
int hash = 0; unsigned int hash = 0;
for (int i = 0; target[i] != '\0'; i++) { for (int i = 0; target[i] != '\0'; i++) {
hash = (hash * 31 + target[i]) % station->track_count; hash = (hash * 31 + target[i]);
} }
return hash; return hash % station->track_count;
} }
void add_target_capacity(struct station* station, const char* target, int capacity) { void add_target_capacity(struct station* station, const char* target, int capacity) {
@ -46,6 +46,7 @@ void add_target_capacity(struct station* station, const char* target, int capaci
struct car* new_car = malloc(sizeof(struct car)); struct car* new_car = malloc(sizeof(struct car));
strncpy(new_car->value, target, TARGET_SIZE); strncpy(new_car->value, target, TARGET_SIZE);
new_car->value[TARGET_SIZE - 1] = '\0'; // Гарантия завершения строки
new_car->capacity = capacity; new_car->capacity = capacity;
new_car->next = station->tracks[index]; new_car->next = station->tracks[index];
station->tracks[index] = new_car; station->tracks[index] = new_car;