Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.20 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. struct punkt {
  4.         int x;
  5.         int y;
  6. };
  7. ostream &operator<<(ostream &os, punkt p)
  8. {
  9.     os << p.x << " " << p.y << endl;    // przeciazenie operatora ( wyzej)
  10.     return os;
  11. }
  12.  
  13. using namespace std;
  14. class Figura  {
  15.     public:
  16.     Figura(){}
  17.      virtual int pole()=0;
  18.      virtual int obwod()=0;
  19.      virtual void wierzcholki()=0;
  20.            
  21. };
  22.  
  23. class Trojkat : public Figura {
  24.     private:
  25.      punkt WT1;
  26.      punkt WT2;
  27.      punkt WT3;
  28.      
  29.     public:
  30.     Trojkat(punkt WT1, punkt WT2, punkt WT3);
  31.     int pole(){
  32.                   int bok1;
  33.                   int bok2;
  34.                   bok1=(this->WT2.x)-(this->WT1.x);
  35.                   bok2=(this->WT3.y)-(this->WT1.y);
  36.                   return bok1*bok2;
  37. }
  38.     int obwod(){
  39.               int bok1;
  40.                    int bok2;
  41.                    int bok3;
  42.                    bok1=(this->WT2.x)-(this->WT1.x);
  43.                    bok2=(this->WT3.y)-(this->WT1.y);
  44.                    bok3=sqrt((pow(bok1,2))+(pow(bok2,2)));
  45.                    return bok1+bok2+bok3;
  46.                    };
  47. }
  48.     void wierzcholki(){cout << "Trojkat\n" << WT1 << WT2 << WT3 << endl;}
  49. };
  50.         Trojkat::Trojkat(punkt WT1,punkt WT2, punkt WT3) : Figura()
  51. {
  52.     this->WT1=WT1;
  53.     this->WT2=WT2;
  54.     this->WT3=WT3;
  55. };
  56.  
  57. class Czworokat : public Figura {
  58.     private:
  59.     punkt WC1;
  60.     punkt WC2;
  61.     punkt WC3;
  62.     punkt WC4;
  63.    
  64.     public:
  65.     Czworkat(punkt WC1, punkt WC2, punkt WC3, punkt WC4);
  66.     int pole(){
  67.               int bok1;
  68.                   int bok2;
  69.                   bok1=(this->WC2.x)-(this->WC1.x);
  70.                   bok2=(this->WC4.y)-(this->WC1.y);
  71.                   return bok1*bok2;
  72.                   };
  73.  
  74.     int obwod(){
  75.                    int bok1;
  76.                    int bok2;
  77.                    bok1=(this->WC2.x)-(this->WC1.x);
  78.                    bok2=(this->WC4.y)-(this->WC1.y);
  79.                    return 2*bok1+2*bok2;
  80.                    };
  81.  
  82.     void wierzcholki(){cout << "Czworokat\n" << WC1 << WC2 << WC3 << WC4 << endl;}
  83. };
  84.    
  85.         Czworokat::Czworokat(punkt WC1,punkt WC2, punkt WC3, punkt WC4) : Figura()
  86. {
  87.     this->WC1=WC1;
  88.     this->WC2=WC2;
  89.     this->WC3=WC3;
  90.     this->WC4=WC4;
  91. };
  92.  
  93. class Kwadrat : public Figura {
  94.     private:
  95.     punkt WK1;
  96.     punkt WK2;
  97.     punkt WK3;
  98.     punkt WK4;
  99.      bool CzyKwadrat;
  100.      
  101.     public:
  102.     Kwadrat(punkt WK1, punkt WK2, punkt WK3, punkt WK4);
  103.     int pole(){
  104.                    int bok;
  105.                    bok=(this->WK3.x)-(this->WK1.x);
  106.                    return bok*bok;
  107.                    };
  108.  
  109.     int obwod(){
  110.                int bok;
  111.                    bok=(this->WK3.x)-(this->WK1.x);
  112.                    return 4*bok;}
  113.     void wierzcholki(){cout << "Kwadrat\n" << WK1 << WK2 << WK3 << WK4 << endl;}
  114.    
  115. };
  116.  
  117. Kwadrat: :Kwadrat(punkt WK1, punkt WK2, punky WK3, punkt WK4 ) : Figura()
  118. ( this->WK1=WK1;
  119.   this->WK2=WK2;
  120.   this->WK3=WK3;
  121.   this->WK4=WK4;
  122. )
  123. int main()
  124. (
  125.     vector<Figura*>figury;
  126.     int ile;
  127.     cout<< "Podaj liczbe figur" << endl;
  128.     cin>> ile;
  129.     char wybor;
  130.      while(ile--){
  131.     cout<<"Podaj typ figury (t- trojkat, c- czworokat, k- kwadrat)" << endl;
  132.     cin>> wybor;
  133.     switch (wybor)
  134.     {
  135.     case 't':
  136.         figury.push_back(new Trojkat ({1,2},{2,3},{1,4}));
  137.         break;
  138.     case 'k':
  139.         figury.push_back(new Kwadrat({1,2}, {1,4},{3,4},{3,2}))
  140.         break
  141.     case 'c':
  142.         figury.push_back(new Czworokat({4,1},{4,4},{7,4},{7,1}));
  143.        
  144.     }
  145.     }
  146.    
  147.     cout << "Czy chcesz wyswietlic dane o figurze?" << endl;
  148.         char dane;
  149.         cin >> dane;
  150.     switch(dane)
  151.         {
  152.      case 't':
  153.         for(int i=0;i<figury.size();i++)
  154.         {
  155.             figury[i]->Wierzcholki();
  156.         }
  157.         }
  158.     cout << "Dla ktorej figury obliczyc pole i obwod?" << endl;
  159.     cin >> wybor;
  160.         switch(wybor)
  161.         {
  162.         case '0':
  163.             cout << "Pole figury wynosi:" << figury[0]->Pole() << "\nObwod figury wynosi:" << figury[0]->Obwod() << endl; break;
  164.         case '1':
  165.             cout << "Pole figury wynosi:" << figury[1]->Pole() << "\nObwod figury wynosi:" << figury[1]->Obwod() << endl; break;
  166.         case '2':
  167.             cout << "Pole figury wynosi:" << figury[2]->Pole() << "\nObwod figury wynosi:" << figury[2]->Obwod() << endl; break;
  168.         }
  169.  
  170.     }
  171.     cout << endl;
  172.    
  173.      return 0;
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement