Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <cmath>
- #include <cstdlib>
- #include <string>
- #include <cctype>
- using namespace std;
- const double PI = 3.141;
- const double Grav =6.67; //Newtonian gravitional constant
- class Planet
- {
- public:
- Planet();
- int menu();
- double calculateArea(int);
- double calculateVolume(int);
- double calculateDensity(int);
- double calculategravity(int);
- double getCalcuVol();
- double getMass();
- void setMass(double);
- double getRadius();
- void setRadius(double);
- void AddPlanet();
- void sortPlanets();
- void Remove(string n);
- void FindPlanets(string n);
- void DisplayPlanet() const;
- string GetName();
- void setName(string n);
- private:
- double mass;
- double volume;
- double Radius;
- string Name;
- };
- vector <Planet> planet(0);
- Planet::Planet()
- {
- mass = 0.0;
- Radius = 0.0;
- string Name;
- }
- string Planet::GetName()
- {
- string::iterator it(Name.begin()); //iterator
- if (it != Name.end())
- Name[0] = toupper((unsigned char)Name[0]); //Change all first letter to capital letter.
- while(++it != Name.end())
- {
- *it = tolower((unsigned char)*it); //Leave everything behind lower case after first letter
- }
- return Name; //return value on variable name.
- }
- void Planet::setName(string n)
- {
- Name=n;
- }
- double Planet::getMass()
- {
- return mass;
- }
- void Planet::setMass(double m)
- {
- mass=m;
- }
- double Planet::getRadius()
- {
- return Radius;
- }
- void Planet::setRadius(double R)
- {
- Radius=R;
- }
- double Planet::calculateArea(int A) //Calculating the Area
- {
- Planet s;
- return (4*PI*pow(Radius, 2));
- }
- double Planet::calculateDensity(int D) //Volume function is already in the Denstiy formula.
- {
- Planet s;
- return mass/((4*PI*pow(Radius, 3))/3);
- }
- double Planet::calculategravity(int G) //Calculating the Gravity due to the accerlation of the planet.
- {
- Planet s;
- return ((Grav*pow(10, -11))*mass)/(pow(Radius, 2));
- }
- void Planet::AddPlanet() //Adding planet.
- {
- Planet s;
- cout << "Name of the planet: ";
- cin >> Name;
- cout << "Enter mass(Kilograms): "<<endl;
- cin >> mass;
- while((mass < 0) || (cin.fail() !=0))
- {
- if(cin.fail() !=0)
- {
- cout << "Error! Can't read it! \n" << endl << "Please type your mass again: "<<endl;
- cin.clear();
- cin.ignore(256,'\n');
- }
- else if(mass < 0)
- {
- cout << "Type value of postive" << endl; // between 1~4
- }
- cin >> mass;
- }
- cout << "Enter Radius(meters): ";
- cin >> Radius;
- while((Radius < 0) || (cin.fail() !=0))
- {
- if(cin.fail() !=0)
- {
- cout << "Error! Can't read it! \n" << endl << "Please type it again.";
- cin.clear();
- cin.ignore(256,'\n');
- }
- else if(Radius < 0)
- {
- cout << "Type value of postive" << endl; // between 1~4
- }
- cin >> Radius;
- }
- s.setMass(mass);
- s.setRadius(Radius);
- s.setName(Name);
- planet.push_back(s);
- }
- void Planet::sortPlanets() //Sorting planet
- {
- Planet temp;
- for (int a = 0; a < planet.size(); a++)
- {
- for (int i = 0; i < planet.size()-1; i++)
- {
- if (planet[i].GetName() > planet[i+1].GetName())
- {
- temp = planet[i+1];
- planet[i+1] = planet[i];
- planet[i] = temp;
- }
- }
- }
- }
- void Planet::Remove(string n)
- {
- for(int i=0; i < planet.size(); i++)
- {
- if(planet[i].GetName()==n)
- {
- planet[i]=planet[planet.size()-1];
- planet.pop_back();
- }
- }
- }
- void Planet::FindPlanets(string n)
- {
- Planet s;
- s.sortPlanets();
- for(int i=0; i < planet.size(); i++)
- {
- if(planet[i].GetName()==n)
- {
- cout << "Your Planet: "<< planet[i].GetName() << endl;
- cout << "mass: " << planet[i].getMass() << " Kilograms" << endl;
- cout << "Radius: " << planet[i].getRadius() << " Meters" << endl;
- cout << "Area: " << planet[i].calculateArea(i) << endl;
- cout << "Density: " << planet[i].calculateDensity(i) << endl;
- cout << "Gravity: " << planet[i].calculategravity(i) << endl << endl;
- }
- }
- }
- void Planet::DisplayPlanet() const
- {
- cout << "All your Planet: " << endl;
- for(int i =0; i<planet.size(); i++)
- {
- cout << "Planet Name: " << planet[i].GetName() << endl;
- cout << "mass: " << planet[i].getMass() << " Kilograms" << endl;
- cout << "Radius: " << planet[i].getRadius() << " Meters" << endl;
- cout << "Area: " << planet[i].calculateArea(i) << endl;
- cout << "Density: " << planet[i].calculateDensity(i) << endl;
- cout << "Gravity: " << planet[i].calculategravity(i) << endl << endl;
- }
- }
- int Planet::menu()
- {
- int choice;
- system("CLS"); //Flush the stream
- cout << "--Menu--" << endl << endl;
- cout << "1. Add Planets" << endl;
- cout << "2. Sort Planet" << endl;
- cout << "3. Delete Planet" <<endl;
- cout << "4. Find Planet" << endl;
- cout << "5. Displays all" << endl;
- cout << "6. Quit" << endl;
- cin >> choice;
- while((choice < 0 || choice > 6) || (cin.fail() !=0))
- {
- if(cin.fail() !=0)
- {
- cout << "Error! Can't read it! \n" << endl << "Please type it again in number(1~4).";
- cin.clear();
- cin.ignore(256,'\n');
- }
- else if(choice < 0 || choice > 6)
- {
- cout << "Between 1~6 please." << endl; // between 1~4
- }
- cout << "--Menu--" << endl << endl;
- cout << "1. Add Planets" << endl;
- cout << "2. Sort Planet" << endl;
- cout << "3. Delete Planet" <<endl;
- cout << "4. Find Planet" << endl;
- cout << "5. Displays all" << endl;
- cout << "6. Quit" << endl;
- cin >> choice;
- }
- return choice;
- }
- int main()
- {
- Planet s;
- int m;
- bool goOn = true;
- while(goOn==true)
- {
- m=s.menu();
- switch(m)
- {
- case 1:// Planet
- {
- s.AddPlanet();
- break;
- }
- case 2:// Sort Planet
- {
- s.sortPlanets();
- break;
- }
- case 3://Delete Planet
- {
- string name;
- cout << "Name to delete: ";
- cin >> name;
- s.Remove(name);
- system("pause");
- break;
- }
- case 4: //Find planet
- {
- string name;
- cout << "Name to Find: ";
- cin >> name;
- s.FindPlanets(name);
- system("pause");
- break;
- }
- case 5:// Displays all
- {
- s.DisplayPlanet();
- system("pause");
- break;
- }
- case 6: //Quit, couldn't find any method, other than exit(0);
- {
- cout << "Thank you for using the program and See you later!";
- goOn = false;
- break;
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment