Advertisement
Guest User

Untitled

a guest
Mar 28th, 2020
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.06 KB | None | 0 0
  1. typedef enum {
  2.     NEPTUNE_LIKE,
  3.     GAS_GIANT,
  4.     TERRESTRIAL,
  5.     SUPER_EARTH,
  6.     UNKNOWN
  7. }PlanetType;
  8.  
  9. typedef struct {
  10.     char name[30];
  11.     PlanetType type;
  12.     float distanceToEarth;
  13. }Planet;
  14.  
  15. Planet createPlanet(char name[], PlanetType type, double distance) {
  16.     Planet pl;
  17.     strcpy(pl.name, name);
  18.     pl.distanceToEarth = distance;
  19.     pl.type = type;
  20.     return pl;
  21. }
  22.  
  23. typedef struct
  24. {
  25.     Planet* elems;      /** dynamic array containing the planets */
  26.     int length;         /**  actual length of the array */
  27.     int capacity;       /**  maximum capacity of the array */
  28. } PlanetRepo;
  29.  
  30. PlanetRepo createPlanetRepo(int capacity) {
  31.     /// create a new planet repo; the elems field must be dynamically allocated (malloc)
  32.     PlanetRepo r;
  33.     r.capacity = capacity;
  34.     r.length = 0;
  35.     r.elems = (Planet*) malloc(sizeof(Planet)*capacity);
  36.     return r;
  37. }
  38. bool add (PlanetRepo* repo, Planet pl) // adding a planet pl in the Repository
  39. {
  40. int pos = repo->length;
  41.     repo->elems[pos] = pl;
  42.     repo->length++;
  43.     return true; }
  44.  
  45. bool remove (PlanetRepo* repo, Planet pl) // can't figure out the implementation
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement