Advertisement
Guest User

Untitled

a guest
May 23rd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <cstring>
  4. using namespace std;
  5.  
  6. class Figura
  7. {
  8. public:
  9. Figura() {}
  10. virtual double pole() { return 0; }
  11. virtual const char * nazwa() { return "Figura"; }
  12. };
  13.  
  14. class Kwadrat : public Figura
  15. {
  16. public:
  17. Kwadrat( double bok = 0 ) : bok( bok ) {}
  18. double pole() { return bok * bok; }
  19. const char * nazwa() { return "Kwadrat"; }
  20. double bok;
  21. };
  22.  
  23. class Kolo : public Figura
  24. {
  25. public:
  26. Kolo( double promien = 0 ) : promien( promien ) {}
  27. double pole() { return 3.14 * promien * promien; }
  28. const char * nazwa() { return "Kolo"; }
  29. double promien;
  30. };
  31.  
  32. class Trojkat : public Figura
  33. {
  34. public:
  35.  
  36. Trojkat(double wysokosc = 0, double bok = 0): wysokosc( wysokosc ) , bok( bok ) {}
  37. double pole () { return wysokosc * bok; }
  38. const char * nazwa () {return "Trojkat"; }
  39. double wysokosc;
  40. double bok;
  41. };
  42.  
  43. class FabrykaFigur
  44. {
  45. public:
  46.  
  47. Figura * zrobFigure( int jakFigura )
  48. {
  49. switch( jakFigura )
  50. {
  51. case 1 : return new Kwadrat;
  52. case 2 : return new Kolo;
  53. case 3 : return new Trojkat;
  54. default : return 0;
  55. }
  56. }
  57. };
  58.  
  59. class Program
  60. {
  61. public:
  62. Program( const char * nazwa = 0 )
  63. {
  64. cout << "\nWitaj w programie " << ( ( nazwa != 0 ) ? nazwa : "" ) << endl;
  65. }
  66.  
  67. ~Program()
  68. {
  69. cout << "\n\nPa, Pa!\nNacisnij Enter by zakonczyc...";
  70. cin.ignore();
  71. cin.get();
  72. }
  73.  
  74. int wyborFigury()
  75. {
  76. int wybor;
  77. cout << "\n1. Kwadrat\n2. Kolo\n3. Trojkat\n4. Costam\n0. Koniec\n?> ";
  78. cin >> wybor;
  79. return wybor;
  80. }
  81.  
  82. void ustalParametry( Figura * f )
  83. {
  84. if( stricmp( f->nazwa(), "Kwadrat" ) == 0 )
  85. {
  86. double wartosc ;
  87. cout << "Podaj dlugosc boku kwadratu";
  88. cin >> wartosc ;
  89. ( ( Kwadrat * ) f )->bok = wartosc;
  90. }
  91.  
  92.  
  93. if ( stricmp (f->nazwa(), "Kolo") ==0)
  94. {
  95. double pr;
  96. cout <<"Podaj dlugosc promienia kola";
  97. cin >> pr;
  98. ( (Kolo *) f )->promien = pr;
  99. }
  100. if (stricmp (f->nazwa(), "Trojkat") ==0)
  101. {
  102. double pod;
  103. cout <<"Podaj podstawe trojkata";
  104. cin >> pod;
  105. ((Trojkat *) f)->bok = pod;
  106. double wys;
  107. cout <<"Podaj dlugosc wysokosci";
  108. cin >> wys;
  109. ((Trojkat *) f)->wysokosc = wys;
  110.  
  111. }
  112. }
  113.  
  114. void wykonajSie()
  115. {
  116. Figura * f = 0;
  117. int wybor;
  118. FabrykaFigur fabryka;
  119.  
  120. do
  121. {
  122. wybor = wyborFigury();
  123. f = fabryka.zrobFigure( wybor );
  124. if( f != 0 )
  125. {
  126. ustalParametry( f );
  127. liczIPisz( f );
  128. delete f;
  129. }
  130. }
  131. while( wybor != 0 );
  132. }
  133.  
  134. void liczIPisz( Figura * figura )
  135. {
  136. cout << "\nFigura: " << figura->nazwa();
  137. cout << "\nPole: " << figura->pole();
  138. }
  139.  
  140. };
  141.  
  142. int main()
  143. {
  144. Program * mojProgramWspanialy = new Program( "Jedyny Taki :)" );
  145.  
  146. mojProgramWspanialy->wykonajSie();
  147.  
  148. delete mojProgramWspanialy;
  149.  
  150. return EXIT_SUCCESS;
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement