Advertisement
Guest User

Untitled

a guest
Jan 20th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.76 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <ctime>
  4. #include <cstdlib>
  5. using namespace std;
  6.  
  7.  
  8. class Figura {
  9. public:
  10.     Figura *next;
  11.     virtual void pole() {};
  12.     virtual int pole2() { return 0; };
  13.     virtual void obwod() {};
  14. };
  15.  
  16. class Kwadrat :public Figura
  17. {
  18. public:
  19.     int a;
  20.     Kwadrat()
  21.     {
  22.  
  23.         a = rand() % 10;
  24.  
  25.     }
  26.  
  27.     void pole()
  28.     {
  29.  
  30.         cout << "Pole kwadratu: " << a*a << endl;
  31.  
  32.     }
  33.     void obwod()
  34.     {
  35.  
  36.         cout << "Obwod kwadratu: " <<4*a <<endl;
  37.  
  38.  
  39.     }
  40.  
  41.     int pole2()
  42.     {
  43.  
  44.         return a*a;
  45.  
  46.     }
  47.  
  48. };
  49.  
  50. class Kolo :public Figura
  51. {
  52. public:
  53.     int r;
  54.  
  55.     Kolo()
  56.     {
  57.  
  58.         r = rand() % 10;
  59.  
  60.     }
  61.  
  62.     void pole()
  63.     {
  64.  
  65.         cout << "Pole kola: " << 3.14*r*r << endl;
  66.  
  67.     }
  68.     void obwod()
  69.     {
  70.  
  71.         cout << "Obwod kola: " << 2 * 3.14*r << endl;
  72.  
  73.     }
  74.  
  75.  
  76.     int pole2()
  77.     {
  78.  
  79.         return 3.14*r*r;
  80.     }
  81.  
  82. };
  83.  
  84. class Prostokat :public Figura
  85. {
  86. public:
  87.     int a, b;
  88.     Prostokat()
  89.     {
  90.  
  91.         a = rand() % 10;
  92.         b = rand() % 10;
  93.  
  94.     }
  95.  
  96.     void pole()
  97.     {
  98.  
  99.         cout << "Pole prostokata: " <<a*b<< endl;
  100.  
  101.     }
  102.     void obwod()
  103.     {
  104.  
  105.         cout << "Obwod prostokata: " << (2 * a) + (2 * b) << endl;
  106.  
  107.     }
  108.     int pole2()
  109.     {
  110.  
  111.         return a*b;
  112.     }
  113.  
  114.  
  115. };
  116. int main()
  117.  
  118. {
  119.     int suma=0;
  120.     Figura *tmp, *H = NULL;
  121.     srand(time(NULL));
  122.    
  123.  
  124.     for (int i = 0; i < 20; i++)
  125.     {
  126.  
  127.         switch (rand() % 3)
  128.         {
  129.         case 0:
  130.             tmp = new Kwadrat();
  131.             tmp->pole();
  132.             tmp->obwod();
  133.             break;
  134.         case 1:
  135.             tmp = new Kolo();
  136.             tmp->pole();
  137.             tmp->obwod();
  138.             break;
  139.         case 2:
  140.             tmp = new Prostokat();
  141.             tmp->pole();
  142.             tmp->obwod();
  143.             break;
  144.  
  145.         }
  146.         cout << endl;
  147.  
  148.         tmp->next = H;
  149.         H = tmp;
  150.  
  151.     }
  152.  
  153.     Figura *M = H;
  154.     Figura *MM = H;
  155.  
  156.     while (M)
  157.     {
  158.         suma += (M->pole2());
  159.         M = M->next;
  160.  
  161.     }
  162.     cout << "SREDNIA: " << suma/20<< endl;
  163.  
  164.  
  165.     system("pause");
  166.     return 0;
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement