push
This commit is contained in:
parent
5c3bf43da4
commit
d1b91c4949
93
du6/a_station.c
Normal file
93
du6/a_station.c
Normal file
@ -0,0 +1,93 @@
|
||||
#include "a_station.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
struct station* create_station(){
|
||||
struct station* stanica = calloc(1, sizeof(struct station));
|
||||
stanica->tracks = calloc(STATION_SIZE, sizeof(struct car*));
|
||||
stanica->track_count = STATION_SIZE;
|
||||
return stanica;
|
||||
}
|
||||
|
||||
void destroy_station(struct station* stanica){
|
||||
for (int i = 0; i < stanica->track_count; i++){
|
||||
struct car* aktualny = stanica->tracks[i];
|
||||
while (aktualny != NULL){
|
||||
struct car* nasledujuci = aktualny->next;
|
||||
free(aktualny);
|
||||
aktualny = nasledujuci;
|
||||
}
|
||||
}
|
||||
free(stanica->tracks);
|
||||
free(stanica);
|
||||
}
|
||||
|
||||
int select_track(struct station* stanica, const char* ciel){
|
||||
unsigned long heslo = 5381;
|
||||
int znak;
|
||||
while ((znak = *ciel++)){
|
||||
heslo = ((heslo << 5) + heslo) + znak;
|
||||
}
|
||||
return heslo % stanica->track_count;
|
||||
}
|
||||
|
||||
void add_target_capacity(struct station* stanica, const char* ciel, int kapacita){
|
||||
int index = select_track(stanica, ciel);
|
||||
struct car* aktualny = stanica->tracks[index];
|
||||
|
||||
while (aktualny != NULL){
|
||||
if (strcmp(aktualny->value, ciel) == 0){
|
||||
aktualny->capacity += kapacita;
|
||||
return;
|
||||
}
|
||||
aktualny = aktualny->next;
|
||||
}
|
||||
|
||||
struct car* novy = calloc(1, sizeof(struct car));
|
||||
strncpy(novy->value, ciel, TARGET_SIZE - 1);
|
||||
novy->capacity = kapacita;
|
||||
novy->next = stanica->tracks[index];
|
||||
stanica->tracks[index] = novy;
|
||||
}
|
||||
|
||||
int get_target_capacity(struct station* stanica, const char* ciel){
|
||||
int index = select_track(stanica, ciel);
|
||||
struct car* aktualny = stanica->tracks[index];
|
||||
|
||||
while (aktualny != NULL){
|
||||
if (strcmp(aktualny->value, ciel) == 0){
|
||||
return aktualny->capacity;
|
||||
}
|
||||
aktualny = aktualny->next;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int count_targets(struct station* stanica){
|
||||
int pocet = 0;
|
||||
|
||||
for (int i = 0; i < stanica->track_count; i++){
|
||||
struct car* aktualny = stanica->tracks[i];
|
||||
while (aktualny != NULL){
|
||||
pocet++;
|
||||
aktualny = aktualny->next;
|
||||
}
|
||||
}
|
||||
|
||||
return pocet;
|
||||
}
|
||||
|
||||
int count_capacity(struct station* stanica){
|
||||
int celkovo = 0;
|
||||
|
||||
for (int i = 0; i < stanica->track_count; i++){
|
||||
struct car* aktualny = stanica->tracks[i];
|
||||
while (aktualny != NULL){
|
||||
celkovo += aktualny->capacity;
|
||||
aktualny = aktualny->next;
|
||||
}
|
||||
}
|
||||
|
||||
return celkovo;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user