Update cv6/a_station.c

This commit is contained in:
Viktor Daniv 2024-11-06 11:14:33 +00:00
parent a79a52b3cc
commit a8b18d2864

View File

@ -2,32 +2,74 @@
#include <stdlib.h>
#include <string.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;
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){
void destroy_station(struct station* station) {
if (station == NULL) return;
for (int i = 0; i < station->track_count; i++) {
free(station->tracks[i]);
}
free(station->tracks);
free(station);
}
int select_track(struct station* station, const char* target){
int select_track(struct station* station, const char* target) {
for (int i = 0; i < station->track_count; i++) {
if (station->tracks[i] != NULL && strcmp(station->tracks[i]->target, target) == 0) {
return i;
}
}
return -1;
}
void add_target_capacity(struct station* station, const char* target, int capacity) {
int track_index = select_track(station, target);
if (track_index != -1) {
station->tracks[track_index]->capacity += capacity;
} else {
for (int i = 0; i < station->track_count; i++) {
if (station->tracks[i] == NULL) {
station->tracks[i] = (struct car*)malloc(sizeof(struct car));
strncpy(station->tracks[i]->target, target, sizeof(station->tracks[i]->target) - 1);
station->tracks[i]->target[sizeof(station->tracks[i]->target) - 1] = '\0';
station->tracks[i]->capacity = capacity;
break;
}
}
}
}
int get_target_capacity(struct station* station, const char* target) {
int track_index = select_track(station, target);
if (track_index != -1) {
return station->tracks[track_index]->capacity;
}
return 0;
}
void add_target_capacity(struct station* station,const char* target, int capacity){
}
int get_target_capacity(struct station* station,const char* target){
return 0;
}
int count_targets(struct station* station){
return 0;
}
int count_capacity(struct station* station){
return 0;
int count_targets(struct station* station) {
int count = 0;
for (int i = 0; i < station->track_count; i++) {
if (station->tracks[i] != NULL) {
count++;
}
}
return count;
}
int count_capacity(struct station* station) {
int total_capacity = 0;
for (int i = 0; i < station->track_count; i++) {
if (station->tracks[i] != NULL) {
total_capacity += station->tracks[i]->capacity;
}
}
return total_capacity;
}