usaa24/cv6/a_station.c

105 lines
2.2 KiB
C
Raw Permalink Normal View History

2024-11-06 21:07:29 +00:00
#include <stdio.h>
#include "a_station.h"
#include <stdlib.h>
#include <string.h>
2024-11-06 21:51:39 +00:00
struct station* create_station(){
2024-11-06 21:04:37 +00:00
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;
}
2024-10-28 17:01:03 +00:00
2024-11-06 21:04:37 +00:00
void destroy_station(struct station* station){
free(station->tracks);
free(station);
}
2024-10-28 17:01:03 +00:00
2024-11-06 21:04:37 +00:00
int select_track(struct station* station, const char* target)
{
int i=0;
char t=0;
while(target[i]!=0)
{
2024-11-06 21:51:39 +00:00
t+=target[i++];
2024-11-06 21:04:37 +00:00
}
if(t<0) {t=-t;}
2024-11-06 21:51:39 +00:00
i=t%10;
2024-11-06 21:05:21 +00:00
return i;
2024-11-06 21:04:37 +00:00
}
2024-10-28 17:01:03 +00:00
2024-11-06 21:51:39 +00:00
void add_target_capacity(struct station* station, const char* target, int capacity)
2024-11-06 21:04:37 +00:00
{
2024-11-06 21:51:39 +00:00
int i = select_track(station, target);
struct car* p = station->tracks[i];
while (p != NULL)
{
if (strcmp(p->value, target) == 0)
{
2024-11-06 22:09:04 +00:00
p->capacity += capacity;
2024-11-06 21:51:39 +00:00
return;
}
p = p->next;
2024-11-06 21:04:37 +00:00
}
2024-10-28 17:01:03 +00:00
2024-11-06 21:51:39 +00:00
struct car* new_car = (struct car*)calloc(1, sizeof(struct car));
strcpy(new_car->value, target);
new_car->capacity = capacity;
new_car->next = station->tracks[i];
station->tracks[i] = new_car;
2024-11-06 21:04:37 +00:00
}
2024-10-28 17:01:03 +00:00
2024-11-06 21:51:39 +00:00
2024-11-06 21:04:37 +00:00
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;
}
2024-10-28 17:01:03 +00:00
2024-11-06 21:04:37 +00:00
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;
}