Update cv6/a_station.c

This commit is contained in:
Yurii Yakovenko 2024-11-06 21:51:39 +00:00
parent c8aabc50d4
commit ba09362c7b

View File

@ -3,7 +3,8 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
struct station* create_station(){
struct station* create_station(){
struct station* station = (struct station*)calloc(1,sizeof(struct station)); struct station* station = (struct station*)calloc(1,sizeof(struct station));
station->tracks = (struct car**)calloc(STATION_SIZE, sizeof(struct car*)); station->tracks = (struct car**)calloc(STATION_SIZE, sizeof(struct car*));
station->track_count = STATION_SIZE; station->track_count = STATION_SIZE;
@ -21,40 +22,38 @@ int select_track(struct station* station, const char* target)
char t=0; char t=0;
while(target[i]!=0) while(target[i]!=0)
{ {
t+=target[i]; t+=target[i++];
} }
if(t<0) {t=-t;} if(t<0) {t=-t;}
i=t%10; i=t%10;
return i; return i;
} }
void add_target_capacity(struct station* station,const char* target, int capacity) void add_target_capacity(struct station* station, const char* target, int capacity)
{ {
int i=select_track(station, target); int i = select_track(station, target);
struct car* p = station->tracks[i];
struct car *nwcar = (struct car*)calloc(STATION_SIZE, sizeof(struct car));
nwcar->next=NULL; while (p != NULL)
strcpy(nwcar->value, target); {
nwcar->capacity=capacity; if (strcmp(p->value, target) == 0)
{
if(station->tracks[i]==NULL) p->capacity = capacity;
{ return;
station->tracks[i]=nwcar; }
p = p->next;
} }
struct car* start = station->tracks[i]; struct car* new_car = (struct car*)calloc(1, sizeof(struct car));
struct car* this = start; strcpy(new_car->value, target);
while(this->next != NULL) new_car->capacity = capacity;
{ new_car->next = station->tracks[i];
this=this->next; station->tracks[i] = new_car;
}
this->next=nwcar;
} }
int get_target_capacity(struct station* station,const char* target) int get_target_capacity(struct station* station,const char* target)
{ {
int i=select_track(station, target); int i=select_track(station, target);
@ -103,5 +102,3 @@ int count_capacity(struct station* station){
} }
return k; return k;
} }