/**** * * Implementation of solar-system.h. See that file for function descriptions. * */ #include "solar-system.h" #include #include #include void init_system(SolarSystem* ss, double diameter, char* galaxy, int num_planets) { int i; (*ss).diameter = diameter; strcpy((*ss).galaxy, galaxy); ss->planets = (Planet**) calloc(num_planets, sizeof(Planet*)); for (i = 0; i < num_planets; i++) { (*ss).planets[i] = NULL; } ss->num_planets = num_planets; } void add_planet(Planet* p, SolarSystem* ss) { int i; for (i = 0; i < ss->num_planets && !is_empty(ss->planets[i]); i++) ; if (i < ss->num_planets) { ss->planets[i] = p; } } void put_planet(Planet* p, int pos, SolarSystem* ss) { if (pos < 0 || pos >= ss->num_planets) { return; } (*ss).planets[pos] = p; } Planet* find_planet(char* name, SolarSystem ss) { int pos = find_planet_position(name, ss); if (pos >= 0) { return ss.planets[pos]; } return NULL; } int find_planet_position(char* name, SolarSystem ss) { int i; for (i = 0; i < ss.num_planets; i++) { if (ss.planets[i] == NULL) { continue; } if (strcmp(ss.planets[i]->name, name) == 0) { return i; } } return -1; } void remove_planet(char* name, SolarSystem* ss) { int pos = find_planet_position(name, *ss); if (pos >= 0) { ss->planets[pos] = NULL; } } void print_system(SolarSystem ss) { int i; for (i = 0; i < ss.num_planets; i++) { if (!is_empty(ss.planets[i])) { printf("planets[%d]:\n", i); print_planet(ss.planets[i]); printf("\n"); } } }