Advertisement
CzarnyBarszcz

zadanie3

Nov 23rd, 2020 (edited)
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.83 KB | None | 0 0
  1. ////Michal Szuleta  sem3 nst g1 dz1
  2. #include <iostream>
  3. #include <math.h>
  4.  
  5. using namespace std;
  6.  
  7. class punkt
  8. {
  9.     double X,Y;
  10. public:
  11.     punkt();
  12.     punkt(double,double);
  13.     const double& x()const {return X;}
  14.     const double& y()const {return Y;}
  15.     double& x(){return X;}
  16.     double& y(){return Y;}
  17.     double odleglosc(punkt target)
  18.     {
  19.         double d =0.0;
  20.         d=sqrt(pow(target.X - this->X,2)+pow(target.Y-this->Y,2));
  21.         return d;
  22.     }
  23.  
  24. };
  25.     punkt::punkt():X(0),Y(0){}
  26.     punkt::punkt(double x,double y)
  27.     {
  28.         this->X = x;
  29.         this->Y = y;
  30.     }
  31. class wielobok
  32. {
  33. private:
  34.     punkt *tab;
  35.     int n=0;
  36.    
  37. public:
  38.     wielobok();
  39.     wielobok(punkt* , punkt*);
  40.     double obwod()
  41.     {
  42.         double obw=0.0;
  43.         for(int i=0;i<n-1;i++)
  44.         {
  45.             obw+= tab[i].odleglosc(tab[i+1]);
  46.         }
  47.         obw+= tab[n-1].odleglosc(tab[0]);
  48.         return obw;
  49.     }
  50.     punkt& Punkt (int a=0)
  51.     {
  52.         return tab[a];
  53.     }
  54.     void wyswietl(int a=0)
  55.     {
  56.         cout<<tab[a].x()<<","<<tab[a].y()<<endl;
  57.     }
  58.     int ilosc()
  59.     {
  60.         return n;
  61.     }
  62.     void punkty (punkt *c,punkt *d)
  63.     {
  64.         n=d-c;
  65.         tab = new punkt[n];
  66.         for(int i=0;i<n;i++)
  67.         {
  68.             tab[i]= c[i];
  69.         }
  70.     }
  71.     ~wielobok()
  72.     {
  73.         delete [] tab;
  74.         cout<<"USUNIETO TABLICE"<<endl;
  75.     }
  76.  
  77. };
  78. wielobok::wielobok(){}
  79. wielobok::wielobok(punkt *c,punkt *d)
  80. {
  81.     n=d-c;
  82.     tab = new punkt[n];
  83.     for(int i=0;i<n;i++)
  84.     {
  85.         tab[i]= c[i];
  86.     }
  87. }
  88.  
  89. int main()
  90. {
  91. punkt p(2, 3);
  92. cout << p.x() << ' ' << p.y() << '\n';
  93. p.x() = 1;
  94. p.y() = 1;
  95. cout << p.x() << ' ' << p.y() << '\n';
  96. cout << p.odleglosc(punkt()) << '\n';
  97. punkt t[] = { punkt(0, 1), punkt(0, 0), punkt(1, 0), punkt(1,1) };
  98. wielobok w1(t,t+4);
  99. cout << w1.obwod() << '\n';
  100. w1.Punkt(1) = punkt(0.5, 0.5);
  101. cout << w1.obwod() << '\n';
  102. wielobok w2;
  103. w2.punkty(t, t+3);
  104. cout << w2.obwod() << '\n';
  105. for (int i = 0; i < w2.ilosc(); ++i)
  106. cout << w2.Punkt(i).x() << ' ' << w2.Punkt(i).y() << '\n';
  107.  
  108.  
  109.  
  110.     return 0;
  111. }
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement