SHARE
TWEET

Untitled

a guest Sep 16th, 2019 94 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "Planet.h"
  2. #include "Star.h"
  3. #include <stdlib.h>
  4. #include <cstdio>
  5. #include <iostream>
  6.  
  7. using namespace std;
  8.  
  9. Star::Star(){
  10.     current_planets = 0;
  11.     next_id = 1;
  12.     planets = NULL;
  13. }
  14.  
  15. Star::~Star(){
  16.     for(int i = 0; i < (int)current_planets; i++){
  17.         delete planets[i];
  18.     }
  19.     delete[] planets;
  20. }
  21.  
  22. int Star::addPlanet(){
  23.     Planet * p = new Planet((unsigned int)(rand() % 3001), next_id);
  24.     Planet ** pList = new Planet * [current_planets + 1];
  25.     for(int i = 0; i < (int)current_planets; i++){
  26.         pList[i] = planets[i];
  27.     }
  28.     pList[current_planets] = p;
  29.     current_planets = current_planets + 1;
  30.     delete[] planets;
  31.     planets = pList;
  32.     return next_id++;
  33. }
  34.  
  35. bool Star::removePlanet(int i){
  36.     for(int j = 0; j < (int)current_planets; j++){
  37.         if(planets[j]->getID() == i){
  38.             Planet ** pList = new Planet * [current_planets - 1];
  39.             for(int k = 0; k < j; k++){
  40.                 pList[k] = planets[k];
  41.             }
  42.             for(int k = j + 1; k < (int)current_planets; k++){
  43.                 pList[k - 1] = planets[k];
  44.             }
  45.             current_planets = current_planets - 1;
  46.             delete[] planets;
  47.             planets = pList;
  48.             return true;
  49.         }
  50.     }
  51.     return false;
  52. }
  53.  
  54. Planet * Star::getPlanet(int i){
  55.     for(int j = 0; j < (int)current_planets; ++j){
  56.         if(planets[i]->getID() == i){
  57.             return (planets[j]);
  58.         }
  59.     }
  60.     return NULL;
  61. }
  62.  
  63. void Star::orbit(){
  64.     for (int i = 0; (unsigned int)i < current_planets; ++i){
  65.         planets[i]->orbit();
  66.     }
  67. }
  68.  
  69. void Star::printStarInfo(){
  70.     printf("This star currently has %u.\n", current_planets);
  71.     for(int i = 0; (unsigned int)i < current_planets; i++){
  72.         printf("Planet id %c%d\n", planets[i]->getType(), planets[i]->getID());
  73.     }
  74. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top