usaa24/cv6/a_station.c

103 lines
2.1 KiB
C

struct station* create_station(){
struct station* station = (struct station*)calloc(1,sizeof(struct station));
station->tracks = (struct car**)calloc(STATION_SIZE, sizeof(struct car*));
station->track_count = STATION_SIZE;
return station;
}
void destroy_station(struct station* station){
free(station->tracks);
free(station);
}
int select_track(struct station* station, const char* target)
{
int i=0;
char t=0;
while(target[i]!=0)
{
t+=target[i];
}
if(t<0) {t=-t;}
i=t%10;
return i;
}
void add_target_capacity(struct station* station,const char* target, int capacity)
{
int i=select_track(station, target);
struct car *nwcar = (struct car*)calloc(STATION_SIZE, sizeof(struct car));
nwcar->next=NULL;
strcpy(nwcar->value, target);
nwcar->capacity=capacity;
if(station->tracks[i]==NULL)
{
station->tracks[i]=nwcar;
}
struct car* start = station->tracks[i];
struct car* this = start;
while(this->next != NULL)
{
this=this->next;
}
this->next=nwcar;
}
int get_target_capacity(struct station* station,const char* target)
{
int i=select_track(station, target);
struct car* start = station->tracks[i];
struct car* this = start;
while(this != NULL)
{
if(strcmp(this->value , target)==0)
{
return this->capacity;
}
this=this->next;
}
return 0;
}
int count_targets(struct station* station)
{
int k=0;
for (int i = 0 ; i< station->track_count; i++)
{
struct car* start = station->tracks[i];
struct car* this = start;
while(this != NULL)
{
k++;
this=this->next;
}
}
return k;
}
int count_capacity(struct station* station){
int k=0;
for (int i = 0 ; i< station->track_count; i++)
{
struct car* start = station->tracks[i];
struct car* this = start;
while(this != NULL)
{
k+=this->capacity;
this=this->next;
}
}
return k;
}