Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <cmath>
- #include <cstdlib>
- using namespace std;
- const double PI = 3.141;
- class Sphere
- {
- public:
- Sphere();
- int menu();
- double calculateArea(int);
- double calculateVolume(int);
- double getRadius();
- void setRadius(double);
- void AddSphere();
- void sortSpheres();
- void DisplaySphere() const;
- private:
- double radius;
- };
- vector <Sphere> sphere(0);
- Sphere::Sphere()
- {
- radius = 0.0;
- }
- double Sphere::getRadius()
- {
- return radius;
- }
- void Sphere::setRadius(double r)
- {
- radius=r;
- }
- double Sphere::calculateArea(int i)
- {
- Sphere s; //using Sphere class.
- return (4*PI*pow(radius, 2));
- }
- double Sphere::calculateVolume(int i)
- {
- Sphere s;
- return ((4*PI*pow(radius, 3))/3);
- }
- void Sphere::AddSphere()
- {
- Sphere s;
- double radius;
- cout << "Enter radius ";
- 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.setRadius(radius);
- sphere.push_back(s);
- }
- void Sphere::sortSpheres()
- {
- Sphere temp;
- for (int a = 0; a < sphere.size(); a++)
- {
- for (int i = 0; i < sphere.size()-1; i++)
- {
- if (sphere[i].calculateVolume(i) > sphere[i+1].calculateVolume(i+1))
- {
- temp = sphere[i+1];
- sphere[i+1] = sphere[i];
- sphere[i] = temp;
- }
- }
- }
- }
- void Sphere::DisplaySphere() const
- {
- Sphere s;
- cout << "All your Sphere: " << endl;
- for(int i =0; i<sphere.size(); i++)
- {
- cout << "Sphere: " << i+1 << endl << "Radius: " << sphere[i].getRadius() << endl;
- cout << "Volume: " << sphere[i].calculateVolume(i) << endl;
- cout << "Area: " << sphere[i].calculateArea(i) << endl << endl;
- }
- }
- int Sphere::menu()
- {
- int choice;
- system("CLS"); //Flush the stream
- cout << "--Menu--" << endl << endl;
- cout << "1. Add spheres" << endl;
- cout << "2. Sort sphere" << endl;
- cout << "3. Displays all" << endl;
- cout << "4. Quit" << endl;
- cin >> choice;
- while((choice < 0 || choice > 4) || (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 > 4)
- {
- cout << "Between 1~4 please." << endl; // between 1~4
- }
- cout << "--Menu--" << endl << endl;
- cout << "1. Add spheres" << endl;
- cout << "2. Sort sphere" << endl;
- cout << "3. Displays all" << endl;
- cout << "4. Quit" << endl;
- cin >> choice;
- }
- return choice;
- }
- int main()
- {
- Sphere s;
- int m;
- bool goOn = true;
- while(goOn==true)
- {
- m=s.menu();
- switch(m)
- {
- case 1:// Sphere
- {
- s.AddSphere();
- break;
- }
- case 2:// Sort sphere
- {
- s.sortSpheres();
- break;
- }
- case 3:// Displays all
- {
- s.DisplaySphere();
- system("pause");
- break;
- }
- case 4: //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