Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.00 KB | None | 0 0
  1. #include<iostream>
  2. #include<string>
  3. #include<fstream>
  4. #include<cmath>
  5.  
  6. using namespace std;
  7.  
  8. class figure
  9. {
  10. protected:
  11.     double h;
  12. public:
  13.     figure()
  14.     {
  15.         h = 0;
  16.     }
  17.     virtual void input(istream& is)
  18.     {
  19.         is >> h;
  20.     }
  21.     virtual void output(ostream& os)
  22.     {
  23.         os << h<<" ";
  24.     }
  25.  
  26.     virtual double get_extent() = 0;
  27. };
  28.  
  29. class paralelepiped : public figure
  30. {
  31. private:
  32.     double a;
  33.     double b;
  34. public:
  35.     paralelepiped() :figure()
  36.     {
  37.         a = 0;
  38.         b = 0;
  39.     }
  40.     void input(istream& is)
  41.     {
  42.     figure:: input(is);
  43.         is >> a >> b;
  44.     }
  45.     void output(ostream& os)
  46.     {
  47.     figure:: output(cout);
  48.     os << a << " " << b<<endl;
  49.     }
  50.     double get_extent()
  51.     {
  52.         return a*b*h;
  53.     }
  54.     double get_a()
  55.     {
  56.         return a;
  57.     }
  58.     double get_b()
  59.     {
  60.         return b;
  61.     }
  62.     double get_h()
  63.     {
  64.         return h;
  65.     }
  66. };
  67.  
  68. class piramida : public figure
  69. {
  70. private:
  71.     double a;
  72.     double b;
  73.     double c;
  74. public:
  75.     piramida() :figure()
  76.     {
  77.         a = 0;
  78.         b = 0;
  79.         c = 0;
  80.     }
  81.     void input(istream& is)
  82.     {
  83.     figure:: input(is);
  84.         is >> a >> b >> c;
  85.     }
  86.     void output(ostream& os)
  87.     {
  88.     figure:: output(cout);
  89.     os << a << " " << b << " " << c<<endl;
  90.     }
  91.     double get_extent()
  92.     {
  93.         return sqrt(((a + b + c) / 2)* ((a + b + c) / 2 - a)*((a + b + c) / 2 - b)*((a + b + c) / 2 - c))*h;
  94.     }
  95.     double get_h()
  96.     {
  97.         return h;
  98.     }
  99. };
  100.  
  101. class tetraedr : public figure
  102. {
  103. private:
  104.     double a;
  105. public:
  106.     tetraedr() :figure()
  107.     {
  108.         a = 0;
  109.     }
  110.     void input(istream& is)
  111.     {
  112.         is >> a;
  113.     }
  114.     void output(ostream& os)
  115.     {
  116.     figure:: output(cout);
  117.         os << a<<endl;
  118.     }
  119.     double get_extent()
  120.     {
  121.         return a*a*a*sqrt(2) / 12;
  122.     }
  123.  
  124. };
  125.  
  126. class kylia : public figure
  127. {
  128. private:
  129.     double r;
  130. public:
  131.     kylia() :figure()
  132.     {
  133.         r = 0;
  134.     }
  135.     void input(istream& is)
  136.     {
  137.         is >> r;
  138.     }
  139.     void output(ostream& os)
  140.     {
  141.     figure:: output(cout);
  142.     os << r << endl;
  143.     }
  144.     double get_extent()
  145.     {
  146.         return 4 * 3.141592*r*r*r / 3;
  147.     }
  148. };
  149.  
  150. /*figure* get_max_extent(figure** a, int n)
  151. {
  152.     int k;
  153.     double max = 0, extent(0);
  154.     for (int i = 0;i < n;i++)
  155.     {
  156.         extent = a[i]->get_extent();
  157.         if (max < extent)
  158.         {
  159.             max = extent;
  160.             k = i;
  161.         }
  162.     }
  163.     return a[k];
  164. }*/
  165.  
  166. figure* get_max_height(figure** a, int n)
  167. {
  168.     piramida *p;
  169.  
  170.     double height = 0, max = 0;
  171.     int k;
  172.     for (int i = 0;i < n;i++)
  173.     {
  174.         p = dynamic_cast<piramida*>(a[i]);
  175.         if (p)
  176.         {
  177.             height = p->get_h();
  178.             if (height > max)
  179.             {
  180.                 max = height;
  181.                 k = i;
  182.             }
  183.         }
  184.     }
  185.     return a[k];
  186. }
  187.  
  188.  
  189. void sort(figure** arr, int n)
  190. {
  191.     for (int i = 0; i< n - 1; ++i)
  192.     {
  193.         for (int j = i + 1; j<n; ++j)
  194.         {
  195.             if (arr[i]->get_extent() > arr[j]->get_extent())
  196.             {
  197.                 swap(arr[i], arr[j]);
  198.             }
  199.         }
  200.     }
  201. }
  202.  
  203. figure* smallest_side(figure** a, int n)
  204. {
  205.     paralelepiped* p;
  206.     double min = 1000;
  207.     int k;
  208.     for (int i = 0; i< n; ++i)
  209.     {
  210.         p = dynamic_cast<paralelepiped*>(a[i]);
  211.         if (p)
  212.         {
  213.             if ((min>p->get_b()*p->get_h()) || (min>p->get_a()*p->get_h()))
  214.             {
  215.                 if ((min > p->get_b()*p->get_h()) < (min > p->get_a()*p->get_h()))
  216.                 {
  217.                     min = min > p->get_b()*p->get_h();
  218.                     k = i;
  219.                 }
  220.                 else
  221.                 {
  222.                     min = min > p->get_a()*p->get_h();
  223.                     k = i;
  224.                 }
  225.             }
  226.         }
  227.     }
  228.     return a[k];
  229. }
  230.  
  231.  
  232.  
  233. void main()
  234. {
  235.    
  236.     int n;
  237.     fstream in("figure.txt");
  238.     in >> n;
  239.     char ch;
  240.     figure** a = new figure*[n];
  241.    
  242.     for (int i = 0; i < n; i++)
  243.     {
  244.         in >> ch;
  245.         if (ch == 'p')
  246.         {
  247.             a[i] = new paralelepiped();
  248.         }
  249.         if (ch == 'm')
  250.         {
  251.             a[i] = new piramida();
  252.         }
  253.         if (ch == 't')
  254.         {
  255.             a[i] = new tetraedr();
  256.         }
  257.         if (ch == 'k')
  258.         {
  259.             a[i] = new kylia();
  260.         }
  261.  
  262.         a[i]->input(in);
  263.     }
  264.    
  265.     for (int i = 0; i < n; ++i)
  266.     {
  267.         a[i]->output(cout);
  268.     }
  269.  
  270.     cout << "================================================================================"<<endl;
  271.     sort(a, n);
  272.  
  273.     for (int i = 0; i < n; ++i)
  274.     {
  275.         a[i]->output(cout);
  276.     }
  277.  
  278.     cout << "================================================================================"<<endl;
  279.     figure* o = smallest_side(a, n);
  280.     o->output(cout);
  281.     figure* p = get_max_height(a, n);
  282.     p->output(cout);
  283.     delete[]a;
  284.     in.close();
  285.     system("pause");
  286. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement