107 lines
2.2 KiB
C
107 lines
2.2 KiB
C
#include "a_station.h"
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <stdio.h>
|
|
|
|
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){
|
|
unsigned int hash = 0;
|
|
for (int i = 0; i < strlen(target); i++)
|
|
{ hash += target[i] + (hash<<6) + (hash<<16) - hash;
|
|
|
|
}
|
|
hash = hash %station->track_count;
|
|
|
|
|
|
return hash;
|
|
}
|
|
|
|
void add_target_capacity(struct station* station,const char* target, int capacity){
|
|
struct car* ptr = *station->tracks;
|
|
int count = 0;
|
|
int res = select_track(station, target);
|
|
while(ptr){
|
|
if(count == res){
|
|
break;
|
|
}
|
|
if (strcmp(ptr->value, target) ==0 ){
|
|
ptr->capacity += capacity;
|
|
return;
|
|
}
|
|
|
|
ptr = ptr->next;
|
|
count++;
|
|
|
|
}
|
|
if(count < res){
|
|
struct car* next = (struct car*)calloc(1, sizeof(struct car));
|
|
strcpy(next->value, target);
|
|
next->capacity = capacity;
|
|
if(ptr){
|
|
ptr->next = next;
|
|
return;}
|
|
else{
|
|
*station->tracks = next;
|
|
return;
|
|
}
|
|
}
|
|
else if (count == res && strcmp(ptr->value, target) != 0){
|
|
struct car* next = (struct car*)calloc(1, sizeof(struct car));
|
|
strcpy(next->value, target);
|
|
next->capacity = capacity;
|
|
if(ptr){
|
|
ptr->next = next;
|
|
return;}
|
|
else{
|
|
*station->tracks = next;
|
|
return;
|
|
}
|
|
|
|
|
|
}
|
|
ptr->capacity += capacity;
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
int get_target_capacity(struct station* station,const char* target){
|
|
struct car* ptr = *station->tracks;
|
|
while (ptr)
|
|
{ if(strcmp(ptr->value, target) == 0){
|
|
return ptr->capacity;
|
|
|
|
}
|
|
ptr = ptr->next;
|
|
|
|
/* code */
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int count_targets(struct station* station){
|
|
return 0;
|
|
}
|
|
|
|
int count_capacity(struct station* station){
|
|
int res = 0;
|
|
struct car* ptr = *station->tracks;
|
|
while (ptr){
|
|
res += ptr->capacity;
|
|
|
|
}
|
|
return res;
|
|
}
|
|
|