Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.19 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4. #define _USE_MATH_DEFINES
  5. #include <cmath>
  6. #include <vector>
  7.  
  8. #define M_PI       3.14159265358979323846
  9.  
  10. class Figura {
  11. public:
  12.     virtual float pole() = 0;
  13. };
  14.  
  15. class Kwadrat : public Figura {
  16. protected:
  17.     int A; // bok kwadratu
  18.  
  19. public:
  20.     Kwadrat(int a) : A(a) {} // konstruktor wraz z listą inicjalizacyjną
  21.     float pole() { return A*A; } // implementacja metody pole() z klasy bazowej
  22. };
  23.  
  24. class Kolo : public Figura {
  25. protected:
  26.     int A; // promien kola
  27.  
  28. public:
  29.     Kolo(int a) : A(a) {} // konstruktor wraz z listą inicjalizacyjną
  30.     float pole() { return M_PI*A*A; } // implementacja metody pole() z klasy bazowej
  31. };
  32.  
  33. class Prostokat : public Figura {
  34. protected:
  35.     int A, B; // boki prostokąta
  36.  
  37. public:
  38.     Prostokat(int a, int b) : A(a), B(b) {} // konstruktor wraz z listą inicjalizacyjną
  39.     float pole() { return A*B; } // implementacja metody pole() z klasy bazowej
  40. };
  41.  
  42. void porownaj(Figura* a, Figura* b)
  43. {
  44.     if (a->pole() == b->pole())
  45.     {
  46.         std::cout << "Pola sa rowne" << std::endl;
  47.     }
  48.     else if (a->pole() > b->pole())
  49.     {
  50.         std::cout << "Pole pierwszej wylosowanej figury jest wieksze." << std::endl;
  51.     }
  52.     else
  53.     {
  54.         std::cout << "Pole drugiej wylosowanej figury jest wieksze." << std::endl;
  55.     }
  56. }
  57.  
  58. Figura * losujFigure() {
  59.     int figura;
  60.     figura = rand() % 3;
  61.  
  62.     switch (figura) {
  63.  
  64.         case 0:
  65.         {
  66.             int a = rand() % 100;
  67.             std::cout << "wylosowano kwadrat o boku: " << a << std::endl;
  68.             return new Kwadrat(a);
  69.             break;
  70.         }
  71.  
  72.         case 1:
  73.         {
  74.             int a = rand() % 100;
  75.             int b = rand() % 100;
  76.             std::cout << "wylosowano prostokat o bokach: " << a << " i " << b << std::endl;
  77.             return new Prostokat(a,b);
  78.             break;
  79.         }
  80.         case 2:
  81.         {
  82.             int a = rand() % 100;
  83.             std::cout << "wylosowano kolo o promieniu: " << a << std::endl;
  84.             return new Kolo(a);
  85.             break;
  86.         }
  87.     }
  88. }
  89.  
  90. int main(void)
  91. {
  92.     srand(time(0));
  93.  
  94.     std::vector<Figura*> figury;
  95.  
  96.     figury.push_back(losujFigure());
  97.     figury.push_back(losujFigure());
  98.  
  99.     porownaj(figury[0], figury[1]);
  100.  
  101.     for (auto v : figury) { delete v; } // usuwa dane z pamieci
  102.     figury.clear(); // usuwa elementy z contenera (zwija kontener)
  103.  
  104.     return EXIT_SUCCESS;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement