Facebook
From Cream Lemur, 5 Years ago, written in C++.
Embed
Download Paste or View Raw
Hits: 231
  1. #include <iostream>
  2. #include <math.h>
  3. using namespace std;
  4.  
  5. struct point
  6. {
  7.     float x;
  8.     float y;
  9.  
  10.     friend istream &operator >> (istream&, point&);
  11. };
  12.  
  13. istream &operator >> (istream& wejscie, point& ex)
  14. {
  15.     wejscie >> ex.x >> ex.y;
  16.     return wejscie;
  17. }
  18.  
  19. class points
  20. {
  21. public:
  22.     point **tab;
  23.     int n;
  24.  
  25.     friend ostream& operator<<(ostream&, points const&);
  26.     points() {}
  27.     points(int size)
  28.     {
  29.         tab= new point*[size];
  30.         for(int i=0;i<size;i++)
  31.         {
  32.             tab[i]=new point;
  33.             tab[i]->x=i;
  34.             tab[i]->y=i+1;
  35.             cout<<tab[i]->x<<" "<<tab[i]->y<<endl;
  36.         }
  37.     }
  38. };
  39.  
  40.  
  41. ostream& operator<<(ostream& wyjscie, points const& ex)
  42. {
  43.     for(int i=0;i<ex.n;i++)
  44.     {
  45.         wyjscie<<ex.tab[i]->x<<endl<<ex.tab[i]->y<<endl;
  46.     }
  47.     return wyjscie;
  48. }
  49.  
  50. class pointsopertions
  51. {
  52. public:
  53.     friend class points;
  54.     static point *closesttothecenter(const points &p)
  55.     {
  56.         float s1=0, s2=0;
  57.         for(int i=0;i<p.n;i++)
  58.         {
  59.             s1 += p.tab[i]->x;
  60.             s2 += p.tab[i]->y;
  61.         }
  62.         s1 /=p.n;
  63.         s2 /=p.n;
  64.  
  65.         point *result = new point;
  66.         float x = sqrt(pow(s1-p.tab[0]->x, 2) + pow(s2-p.tab[0]->y, 2));
  67.         result = p.tab[0];
  68.         for(int i=1;i<p.n; i++){
  69.             if (x > sqrt(pow(s1 - p.tab[i]->x, 2) + pow(s2 - p.tab[i]->y, 2))) {
  70.                             x = sqrt(pow(s1 - p.tab[i]->x, 2) + pow(s2 - p.tab[i]->y, 2));
  71.                             result = p.tab[i];
  72.         }
  73.     }
  74.         return result;
  75. }
  76. };
  77.  
  78. int main(){
  79. point p;
  80.     p.x = 10;
  81.     p.y = 11;
  82.     point p2;
  83.     p.x = 10;
  84.     p.y = 11;
  85.     point p3;
  86.     p.x = 10;
  87.     p.y = 11;
  88.     cout << p.x << endl << p.y << endl;
  89.     points pp(5);
  90.     cout << pp;
  91.     point **tab = new point *[3];
  92.     tab[0] = &p;
  93.     tab[1] = &p2;
  94.     tab[2] = &p3;
  95.     points ptab;
  96.     ptab.n = 3;
  97.     ptab.tab = tab;
  98.  
  99.     return 0;
  100. }
  101.