pclovechristian

Planet

May 8th, 2012
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.66 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cmath>
  4. #include <cstdlib>
  5. #include <string>
  6. #include <cctype>
  7.  
  8. using namespace std;
  9.  
  10. const double PI = 3.141;
  11. const double Grav =6.67; //Newtonian gravitional constant
  12.  
  13. class Planet
  14. {
  15.     public:
  16.         Planet();
  17.         int menu();
  18.         double calculateArea(int);
  19.         double calculateVolume(int);
  20.         double calculateDensity(int);
  21.         double calculategravity(int);
  22.         double getCalcuVol();
  23.         double getMass();
  24.         void setMass(double);
  25.         double getRadius();
  26.         void setRadius(double);
  27.         void AddPlanet();
  28.         void sortPlanets();
  29.         void Remove(string n);
  30.         void FindPlanets(string n);
  31.         void DisplayPlanet() const;
  32.         string GetName();
  33.         void setName(string n);
  34.  
  35.     private:
  36.         double mass;
  37.         double volume;
  38.         double Radius;
  39.         string Name;
  40.  
  41. };
  42.  
  43. vector <Planet> planet(0);
  44.  
  45. Planet::Planet()
  46. {
  47.     mass = 0.0;
  48.     Radius = 0.0;
  49.     string Name;
  50. }
  51.  
  52. string Planet::GetName()
  53. {
  54.     string::iterator it(Name.begin()); //iterator
  55.     if (it != Name.end())
  56.                 Name[0] = toupper((unsigned char)Name[0]); //Change all first letter to capital letter.
  57.  
  58.         while(++it != Name.end())
  59.         {
  60.                 *it = tolower((unsigned char)*it); //Leave everything behind lower case after first letter
  61.         }
  62.         return Name; //return value on variable name.
  63. }
  64.  
  65.  
  66. void Planet::setName(string n)
  67. {
  68.     Name=n;
  69. }
  70.  
  71. double Planet::getMass()
  72. {
  73.     return mass;
  74. }
  75.  
  76. void Planet::setMass(double m)
  77. {
  78.     mass=m;
  79. }
  80.  
  81. double Planet::getRadius()
  82. {
  83.     return Radius;
  84. }
  85.  
  86. void Planet::setRadius(double R)
  87. {
  88.     Radius=R;
  89. }
  90.  
  91. double Planet::calculateArea(int A) //Calculating the Area
  92. {
  93.     Planet s;
  94.     return (4*PI*pow(Radius, 2));
  95. }
  96.  
  97.  
  98. double Planet::calculateDensity(int D) //Volume function is already in the Denstiy formula.
  99. {
  100.     Planet s;
  101.     return mass/((4*PI*pow(Radius, 3))/3);
  102. }
  103.  
  104. double Planet::calculategravity(int G) //Calculating the Gravity due to the accerlation of the planet.
  105. {
  106.     Planet s;
  107.     return ((Grav*pow(10, -11))*mass)/(pow(Radius, 2));
  108.  
  109. }
  110.  
  111. void Planet::AddPlanet() //Adding planet.
  112. {
  113.     Planet s;
  114.  
  115.     cout << "Name of the planet: ";
  116.     cin >> Name;
  117.  
  118.     cout << "Enter mass(Kilograms): "<<endl;
  119.     cin >> mass;
  120.  
  121.     while((mass < 0) || (cin.fail() !=0))
  122.     {
  123.         if(cin.fail() !=0)
  124.         {
  125.             cout << "Error! Can't read it! \n" << endl << "Please type your mass again: "<<endl;
  126.             cin.clear();
  127.             cin.ignore(256,'\n');
  128.         }
  129.         else if(mass < 0)
  130.         {
  131.             cout << "Type value of postive" << endl; // between 1~4
  132.         }
  133.         cin >> mass;
  134.     }
  135.  
  136.     cout << "Enter Radius(meters): ";
  137.     cin >> Radius;
  138.     while((Radius < 0) || (cin.fail() !=0))
  139.     {
  140.         if(cin.fail() !=0)
  141.         {
  142.             cout << "Error! Can't read it! \n" << endl << "Please type it again.";
  143.             cin.clear();
  144.             cin.ignore(256,'\n');
  145.         }
  146.         else if(Radius < 0)
  147.         {
  148.             cout << "Type value of postive" << endl; // between 1~4
  149.         }
  150.         cin >> Radius;
  151.     }
  152.  
  153.     s.setMass(mass);
  154.     s.setRadius(Radius);
  155.     s.setName(Name);
  156.     planet.push_back(s);
  157. }
  158.  
  159.  
  160. void Planet::sortPlanets() //Sorting planet
  161. {
  162.     Planet temp;
  163.     for (int a = 0; a < planet.size(); a++)
  164.     {
  165.         for (int i = 0; i < planet.size()-1; i++)
  166.         {
  167.             if (planet[i].GetName() > planet[i+1].GetName())
  168.             {
  169.                 temp = planet[i+1];
  170.                 planet[i+1] = planet[i];
  171.                 planet[i] = temp;
  172.             }
  173.         }
  174.     }
  175. }
  176.  
  177. void Planet::Remove(string n)
  178. {
  179.     for(int i=0; i < planet.size(); i++)
  180.     {
  181.         if(planet[i].GetName()==n)
  182.         {
  183.             planet[i]=planet[planet.size()-1];
  184.             planet.pop_back();
  185.         }
  186.     }
  187.  
  188. }
  189.  
  190. void Planet::FindPlanets(string n)
  191. {
  192.     Planet s;
  193.     s.sortPlanets();
  194.     for(int i=0; i < planet.size(); i++)
  195.     {
  196.         if(planet[i].GetName()==n)
  197.         {
  198.             cout << "Your Planet: "<< planet[i].GetName() << endl;
  199.             cout << "mass: " << planet[i].getMass() << " Kilograms" << endl;
  200.             cout << "Radius: " << planet[i].getRadius() << " Meters" << endl;
  201.             cout << "Area: " << planet[i].calculateArea(i) << endl;
  202.             cout << "Density: " << planet[i].calculateDensity(i) << endl;
  203.             cout << "Gravity: " << planet[i].calculategravity(i) << endl << endl;
  204.         }
  205.     }
  206.  
  207. }
  208.  
  209. void Planet::DisplayPlanet() const
  210. {
  211.             cout << "All your Planet: " << endl;
  212.             for(int i =0; i<planet.size(); i++)
  213.             {
  214.                 cout << "Planet Name: " << planet[i].GetName() << endl;
  215.                 cout << "mass: " << planet[i].getMass() << " Kilograms" << endl;
  216.                 cout << "Radius: " << planet[i].getRadius() << " Meters" << endl;
  217.                 cout << "Area: " << planet[i].calculateArea(i) << endl;
  218.                 cout << "Density: " << planet[i].calculateDensity(i) << endl;
  219.                 cout << "Gravity: " << planet[i].calculategravity(i) << endl << endl;
  220.             }
  221. }
  222.  
  223.  
  224. int Planet::menu()
  225. {
  226.     int choice;
  227.         system("CLS"); //Flush the stream
  228.         cout << "--Menu--" << endl << endl;
  229.         cout << "1. Add Planets" << endl;
  230.         cout << "2. Sort Planet" << endl;
  231.         cout << "3. Delete Planet" <<endl;
  232.         cout << "4. Find Planet" << endl;
  233.         cout << "5. Displays all" << endl;
  234.         cout << "6. Quit" << endl;
  235.         cin >> choice;
  236.  
  237.     while((choice < 0 || choice > 6) || (cin.fail() !=0))
  238.     {
  239.         if(cin.fail() !=0)
  240.         {
  241.             cout << "Error! Can't read it! \n" << endl << "Please type it again in number(1~4).";
  242.             cin.clear();
  243.             cin.ignore(256,'\n');
  244.         }
  245.         else if(choice < 0 || choice > 6)
  246.         {
  247.             cout << "Between 1~6 please." << endl; // between 1~4
  248.         }
  249.         cout << "--Menu--" << endl << endl;
  250.         cout << "1. Add Planets" << endl;
  251.         cout << "2. Sort Planet" << endl;
  252.         cout << "3. Delete Planet" <<endl;
  253.         cout << "4. Find Planet" << endl;
  254.         cout << "5. Displays all" << endl;
  255.         cout << "6. Quit" << endl;
  256.         cin >> choice;
  257.     }
  258.     return choice;
  259. }
  260.  
  261.  
  262. int main()
  263. {
  264.     Planet s;
  265.     int m;
  266.     bool goOn = true;
  267.  
  268.     while(goOn==true)
  269.     {
  270.         m=s.menu();
  271.         switch(m)
  272.         {
  273.             case 1:// Planet
  274.             {
  275.                 s.AddPlanet();
  276.                 break;
  277.             }
  278.             case 2:// Sort Planet
  279.             {
  280.                 s.sortPlanets();
  281.                 break;
  282.             }
  283.             case 3://Delete Planet
  284.             {
  285.                 string name;
  286.                 cout << "Name to delete: ";
  287.                 cin >> name;
  288.                 s.Remove(name);
  289.                 system("pause");
  290.                 break;
  291.             }
  292.             case 4: //Find planet
  293.             {
  294.                 string name;
  295.                 cout << "Name to Find: ";
  296.                 cin >> name;
  297.                 s.FindPlanets(name);
  298.                 system("pause");
  299.                 break;
  300.             }
  301.             case 5:// Displays all
  302.             {
  303.                 s.DisplayPlanet();
  304.                 system("pause");
  305.                 break;
  306.             }
  307.  
  308.             case 6: //Quit, couldn't find any method, other than exit(0);
  309.             {
  310.                 cout << "Thank you for using the program and See you later!";
  311.                 goOn = false;
  312.                 break;
  313.             }
  314.         }
  315.     }
  316.     return 0;
  317. }
Advertisement
Add Comment
Please, Sign In to add comment