Advertisement
Guest User

Untitled

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