pclovechristian

Sphere store

Apr 27th, 2012
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.05 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cmath>
  4. #include <cstdlib>
  5.  
  6. using namespace std;
  7.  
  8. const double PI = 3.141;
  9.  
  10. class Sphere
  11. {
  12.     public:
  13.         Sphere();
  14.         int menu();
  15.         double calculateArea(int);
  16.         double calculateVolume(int);
  17.         double getRadius();
  18.         void setRadius(double);
  19.         void AddSphere();
  20.         void sortSpheres();
  21.         void DisplaySphere() const;
  22.        
  23.     private:
  24.         double radius;
  25. };
  26.  
  27. vector <Sphere> sphere(0);
  28.  
  29. Sphere::Sphere()
  30. {
  31.     radius = 0.0;
  32. }
  33.  
  34. double Sphere::getRadius()
  35. {
  36.     return radius;
  37. }
  38.  
  39. void Sphere::setRadius(double r)
  40. {
  41.     radius=r;
  42. }
  43.  
  44. double Sphere::calculateArea(int i)
  45. {
  46.     Sphere s; //using Sphere class.
  47.     return (4*PI*pow(radius, 2));
  48. }
  49.  
  50. double Sphere::calculateVolume(int i)
  51. {
  52.     Sphere s;
  53.     return ((4*PI*pow(radius, 3))/3);
  54. }
  55.  
  56. void Sphere::AddSphere()
  57. {
  58.     Sphere s;
  59.     double radius;
  60.     cout << "Enter radius ";
  61.     cin >> radius;
  62.     while((radius < 0) || (cin.fail() !=0))
  63.     {
  64.         if(cin.fail() !=0)
  65.         {
  66.             cout << "Error! Can't read it! \n" << endl << "Please type it again.";
  67.             cin.clear();
  68.             cin.ignore(256,'\n');
  69.         }
  70.         else if(radius < 0)
  71.         {
  72.             cout << "Type value of postive" << endl; // between 1~4
  73.         }
  74.         cin >> radius;
  75.     }
  76.     s.setRadius(radius);
  77.     sphere.push_back(s);
  78. }
  79.  
  80.  
  81. void Sphere::sortSpheres()
  82. {
  83.     Sphere temp;
  84.     for (int a = 0; a < sphere.size(); a++)
  85.     {
  86.         for (int i = 0; i < sphere.size()-1; i++)
  87.         {
  88.             if (sphere[i].calculateVolume(i) > sphere[i+1].calculateVolume(i+1))
  89.             {
  90.                 temp = sphere[i+1];
  91.                 sphere[i+1] = sphere[i];
  92.                 sphere[i] = temp;
  93.             }
  94.         }
  95.     }
  96. }
  97.  
  98. void Sphere::DisplaySphere() const
  99. {
  100.             Sphere s;
  101.             cout << "All your Sphere: " << endl;
  102.             for(int i =0; i<sphere.size(); i++)
  103.             {
  104.                 cout << "Sphere: " << i+1 << endl << "Radius: " << sphere[i].getRadius() << endl;
  105.                 cout << "Volume: " << sphere[i].calculateVolume(i) << endl;
  106.                 cout << "Area: " << sphere[i].calculateArea(i) << endl << endl;
  107.             }
  108. }
  109.  
  110.  
  111.  
  112.  
  113. int Sphere::menu()
  114. {
  115.     int choice;
  116.         system("CLS"); //Flush the stream
  117.         cout << "--Menu--" << endl << endl;
  118.         cout << "1. Add spheres" << endl;
  119.         cout << "2. Sort sphere" << endl;
  120.         cout << "3. Displays all" << endl;
  121.         cout << "4. Quit" << endl;
  122.         cin >> choice;
  123.  
  124.     while((choice < 0 || choice > 4) || (cin.fail() !=0))
  125.     {
  126.         if(cin.fail() !=0)
  127.         {
  128.             cout << "Error! Can't read it! \n" << endl << "Please type it again in number(1~4).";
  129.             cin.clear();
  130.             cin.ignore(256,'\n');
  131.         }
  132.         else if(choice < 0 || choice > 4)
  133.         {
  134.             cout << "Between 1~4 please." << endl; // between 1~4
  135.         }
  136.         cout << "--Menu--" << endl << endl;
  137.         cout << "1. Add spheres" << endl;
  138.         cout << "2. Sort sphere" << endl;
  139.         cout << "3. Displays all" << endl;
  140.         cout << "4. Quit" << endl;
  141.         cin >> choice;
  142.     }
  143.     return choice;
  144. }
  145.  
  146.  
  147. int main()
  148. {
  149.     Sphere s;
  150.     int m;
  151.     bool goOn = true;
  152.  
  153.     while(goOn==true)
  154.     {
  155.         m=s.menu();
  156.         switch(m)
  157.         {
  158.             case 1:// Sphere
  159.             {
  160.                 s.AddSphere();
  161.                 break;
  162.             }
  163.             case 2:// Sort sphere
  164.             {
  165.                 s.sortSpheres();
  166.                 break;
  167.             }
  168.             case 3:// Displays all
  169.             {
  170.                 s.DisplaySphere();
  171.                 system("pause");
  172.                 break;
  173.             }
  174.             case 4: //Quit, couldn't find any method, other than exit(0);
  175.             {
  176.                 cout << "Thank you for using the program and See you later!";
  177.                 goOn = false;
  178.                 break;
  179.             }
  180.         }
  181.     }
  182.     return 0;
  183. }
Advertisement
Add Comment
Please, Sign In to add comment