Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2014
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.21 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4.  
  5. class Shape3D
  6. {
  7. public:
  8.     virtual double area() const = 0;
  9.     virtual double volume() const = 0;
  10. };
  11.  
  12. class s : public Shape3D
  13. {
  14. public:
  15.     s(double r = 1) : _r(r) { }
  16.  
  17.     double area() const { return 4 * 3.14 * _r * _r; }
  18.     double volume() const { return 4.0/3 * 3.14 * _r * _r * _r; }
  19.  
  20. private:
  21.     double _r;
  22. };
  23.  
  24. class c : public Shape3D
  25. {
  26. public:
  27.     c(double r = 1, double h = 1) : _r(r), _h(h) { }
  28.  
  29.     double area() const { return 3.14 * _r * (_r + sqrt(pow(_r, 2) + pow(_h, 2))); }
  30.     double volume() const { return 1.0/3 * 3.14 * _r * _r; }
  31.  
  32. private:
  33.     double _r;
  34.     double _h;
  35. };
  36.  
  37. class p : public Shape3D
  38. {
  39. public:
  40.     p(double a = 1, double b = 1, double h = 1) : _a(a), _b(b), _h(h) { }
  41.  
  42.     double area() const { return (_a + _b) * _h + _a * _b; }
  43.     double volume() const { return 1.0/3 * _h * ((_a + _b) * _h + _a * _b); }
  44.  
  45. private:
  46.     double _a;
  47.     double _b;
  48.     double _h;
  49. };
  50.  
  51. class d : public Shape3D
  52. {
  53. public:
  54.     d(double r = 1, double h = 1) : _r(r), _h(h) { }
  55.  
  56.     double area() const { return 2 * 3.14 * _r*(_r + _h); }
  57.     double volume() const { return (2 * 3.14 * _r*(_r + _h)) * _h; }
  58.  
  59. private:
  60.     double _r;
  61.     double _h;
  62. };
  63.  
  64. class t : public Shape3D
  65. {
  66. public:
  67.     t(double a = 1) : _a(a) { }
  68.  
  69.     double area() const { return sqrt(3) *_a * _a; }
  70.     double volume() const { return  sqrt(2)/12 * _a * _a * _a; }
  71.  
  72. private:
  73.     double _a;
  74. };
  75.  
  76. int main()
  77. {
  78.     int n;
  79.     int z, x, y;
  80.     char a[20];
  81.     ifstream f;
  82.     f.open("D:\\Shape3D.txt");
  83.     f >> n;
  84.     Shape3D **shapes;
  85.     shapes = new Shape3D*[n];
  86.     while (!f.eof())
  87.     {
  88.         for (int i = 0; i < n; i++)
  89.         {
  90.             f >> a;
  91.             switch (*a)
  92.             {
  93.             case 's': f >> z; shapes[i] = new s(z); break;
  94.             case 'c': f >> z; f >> x; shapes[i] = new c(z, x); break;
  95.             case 'p': f >> z; f >> x; f >> y; shapes[i] = new p(z, x, y); break;
  96.             case 'd': f >> z; f >> x; shapes[i] = new d(z, x); break;
  97.             case 't': f >> z;  shapes[i] = new t(z); break;
  98.             }
  99.         }
  100.     }
  101.     f.close();
  102.     for (int i = 0; i < 6; ++i)
  103.     {
  104.         cout << shapes[i]->area() << endl;
  105.         cout << shapes[i]->volume() << endl;
  106.     }
  107.  
  108.     for (int i = 0; i < 5; ++i)
  109.         delete shapes[i];
  110.                  delete[] shapes;
  111.    
  112.     system("pause");
  113.     return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement