Advertisement
Guest User

Untitled

a guest
May 23rd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.88 KB | None | 0 0
  1. /*1. Dodaj klasy Trojkat i Prostokat
  2.   2. Modyfikujemy FabrykeFigur
  3.   3. Modyfikujemy menu
  4.   4. Modyfikujemy funkcje ustawParametry
  5. */
  6.  
  7.  
  8.  
  9.  
  10. #include <cstdlib>
  11. #include <iostream>
  12. #include <cstring>
  13. using namespace std;
  14.  
  15. class Figura
  16. {
  17.   public:
  18.     Figura() {}
  19.     virtual double pole() { return 0; }
  20.     virtual const char * nazwa() { return "Figura"; }
  21. };
  22.  
  23. class Kwadrat : public Figura
  24. {
  25.   public:
  26.     Kwadrat( double bok = 0 ) : bok( bok ) {} //zwraca 0, gdyby nie dostał wartości; : lista inicjalizacyjna w konstruktorze
  27.     double pole() { return bok * bok; }
  28.     const char * nazwa() { return "Kwadrat"; }
  29.     double bok;
  30. };
  31.  
  32. class Kolo : public Figura
  33. {
  34.   public:
  35.     Kolo( double promien = 0 ) : promien( promien ) {}
  36.     double pole() { return 3.14 * promien * promien; }
  37.     const char * nazwa() { return "Kolo"; }
  38.     double promien;
  39. };
  40.  
  41. class Prostokat : public Figura
  42. {
  43.   public:
  44.     Prostokat( double bok1 = 0, double bok2 = 0 ) : bok1( bok1 ), bok2( bok2 ) {}
  45.     double pole() { return bok1 * bok2; }
  46.     const char * nazwa() { return "Prostokat"; }
  47.     double bok1, bok2;
  48. };
  49.  
  50. class Trojkat : public Figura
  51. {
  52.   public:
  53.     Trojkat( double podst = 0, double wys = 0 ) : podst( podst ), wys( wys ) {}
  54.     double pole() { return 0.5 * podst * wys; }
  55.     const char * nazwa() { return "Trojkat"; }
  56.     double podst, wys;
  57. };
  58.  
  59. class FabrykaFigur
  60. {
  61.   public:
  62.  
  63.   Figura * zrobFigure( int jakFigura )
  64.   {
  65.     switch( jakFigura )  // tu dopisuj nowe
  66.     {
  67.        case  1 : return new Kwadrat;
  68.        case  2 : return new Kolo;
  69.        case  3 : return new Prostokat;
  70.        case  4 : return new Trojkat;
  71.        default : return 0;
  72.     }
  73.   }
  74. };
  75.  
  76. class Program
  77. {
  78.   public:
  79.     Program( const char * nazwa = 0 )
  80.     {
  81.       cout << "\nWitaj w programie " << ( ( nazwa != 0 ) ? nazwa : "" ) << endl;
  82.     }
  83.  
  84.     ~Program()
  85.     {
  86.       cout << "\n\nPa, Pa!\nNacisnij Enter by zakonczyc...";
  87.       cin.ignore();
  88.       cin.get();
  89.     }
  90.  
  91.     int wyborFigury()
  92.     {
  93.         int wybor;
  94.         cout << "\n1. Kwadrat\n2. Kolo\n3. Prostokat\n4. Trojkat\n0. Koniec\n?> ";
  95.         cin >> wybor;
  96.         return wybor;
  97.     }
  98.  
  99.     void ustalParametry( Figura * f )
  100.     {
  101.       if( stricmp( f->nazwa(), "Kwadrat" ) == 0 )  // poriwnywanie dwoch stringow ta funkcja na oczatku, 0 jesli rowne;
  102.       {
  103.         double b;
  104.         cout << "Podaj bok: " << endl;
  105.         cin >> b;
  106.         ( ( Kwadrat * ) f )->bok = b; // Tu wartosc odczytana z klawiatury, rzutowanie tu jest
  107.       }
  108.       // Ustalanie parametrow dla innych figur
  109.       if( stricmp( f->nazwa(), "Kolo" ) == 0 )
  110.       {
  111.           double p;
  112.         cin >> p;
  113.         ( ( Kolo * ) f )->promien = p;
  114.       }
  115.  
  116.       if( stricmp( f->nazwa(), "Prostokat" ) == 0 )
  117.       {
  118.           double b1, b2;
  119.           cin >> b1;
  120.           cin >> b2;
  121.         // zapytac uzytkownika o bok
  122.         ( ( Prostokat * ) f )->bok1 = b1;
  123.         ( ( Prostokat * ) f )->bok2 = b2;
  124.       }
  125.  
  126.       if( stricmp( f->nazwa(), "Trojkat" ) == 0 )
  127.       {
  128.           double po, w;
  129.           cin >> po;
  130.           cin >> w;
  131.  
  132.         ( ( Trojkat * ) f )->podst = po;
  133.         ( ( Trojkat * ) f )->wys = w;
  134.       }
  135.     }
  136.  
  137.     void wykonajSie()
  138.     {
  139.       Figura * f = 0;
  140.       int wybor;
  141.       FabrykaFigur fabryka;
  142.  
  143.       do
  144.       {
  145.         wybor = wyborFigury();
  146.         f = fabryka.zrobFigure( wybor );
  147.         if( f != 0  )
  148.         {
  149.           ustalParametry( f );
  150.           liczIPisz( f );
  151.           delete f;
  152.         }
  153.       }
  154.       while( wybor != 0 );
  155.     }
  156.  
  157.     void liczIPisz( Figura * figura )
  158.     {
  159.       cout << "\nFigura: " << figura->nazwa();
  160.       cout << "\nPole: "  << figura->pole();
  161.     }
  162.  
  163. };
  164.  
  165. int main()
  166. {
  167.   Program * mojProgramWspanialy = new Program( "Jedyny Taki :)" );
  168.  
  169.   mojProgramWspanialy->wykonajSie();
  170.  
  171.   delete mojProgramWspanialy;
  172.  
  173.   return EXIT_SUCCESS;
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement