Advertisement
Guest User

5

a guest
Nov 20th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.86 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6.  
  7.  
  8. class point
  9. {
  10.     double tab[3];
  11.  
  12.     friend istream & operator >> (istream &, point &);
  13.  
  14. public:
  15.  
  16.     point()
  17.     {
  18.         for(unsigned i=0; i<3; i++)
  19.             tab[i]=0;
  20.     }
  21.  
  22.     // x jest referencja do tablicy 3 elementowej
  23.     point(const double (&x)[3])
  24.     {
  25.         tab[0] = x[0];
  26.         tab[1] = x[1];
  27.         tab[2] = x[2];
  28.     }
  29.  
  30.     point(const double &x, const double &y, const double &z)
  31.     {
  32.         tab[0] = x;
  33.         tab[1] = y;
  34.         tab[2] = z;
  35.     }
  36.  
  37.    const double & operator [] (const unsigned &i) const
  38.    {
  39.        if (i>2)
  40.        {
  41.            cout << "POZA ZAKRESEM!!!";
  42.        }
  43.  
  44.         return tab[i];
  45.    }
  46.  
  47.  
  48.    const double distance(const point &p) const
  49.    {
  50.        return( sqrt( (tab[0]-p[0])*(tab[0]-p[0])  + (tab[1]-p[1])*(tab[1]-p[1]) + (tab[2]-p[2])*(tab[2]-p[2])  ));
  51.    }
  52.  
  53.     // dodawanie jako metoda
  54.    const point operator + (const point &p) const
  55.    {
  56.         return point(tab[0]+p[0], tab[1]+p[1], tab[2]+p[2]);
  57.    }
  58.  
  59.     // mnozenie , te moze byc jako metoda
  60.     const point operator * (const double &P)
  61.     {
  62.         return point(tab[0]*P, tab[1]*P, tab[2]*P);
  63.     }
  64.  
  65.     // porownywanie jako metoda
  66.     bool operator == (const point &p)
  67.     {
  68.         double eps = 1e-10;
  69.  
  70.         if(abs(this->distance(point()) - p.distance(point())) < eps)
  71.             return true;
  72.         return false;
  73.     }
  74.  
  75.  
  76. };
  77.  
  78. //odejmowanie jako niemetoda
  79. const point operator - (const point &l, const point &p)
  80. {
  81.     return point(l[0]-p[0], l[1]-p[1], l[2]-p[2]);
  82. }
  83.  
  84.  
  85. // mnozenie ( z lewej strony elelement nienalezacy do klasy wiec nie moze to byc metoda)
  86. const point operator * (const double & L, const point &p)
  87. {
  88.     return point(L*p[0], L*p[1], L*p[2]);
  89. }
  90.  
  91. // MUSI BYC TU, BO LEWY ARGUMENT << NIE JEST ELEMENTEM KLASY, NIE TRZEBA ZAPRZYJAZANIAC BO MAMY METODY DOSTEPOWE []
  92. ostream & operator << (ostream &out, const point &p)
  93. {
  94.     out << p[0] << " " << p[1] << " " << p[2];
  95.     return out;
  96. }
  97.  
  98. // musialem zaprzyjaznic bo na metodach dostepowych [] nie dzialalo.
  99. istream & operator >> (istream &in, point &p)
  100. {
  101.     in >> p.tab[0] >> p.tab[1] >> p.tab[2];
  102.     return in;
  103. }
  104.  
  105.  
  106.  
  107. bool operator < (const point &l, const point &p)
  108. {
  109.     if (l.distance(point()) < p.distance(point()))
  110.         return true;
  111.  
  112.     return false;
  113. }
  114.  
  115.  
  116. int main()
  117. {
  118.     double x[2][3] = {{1.0, 1.0, 1.0},{1.0, 2.0, 3.0}};
  119.  
  120.     point p1(x[0]), p2(x[1]);
  121.  
  122.     const point p3(0.4, 0.2, 0.1);
  123.  
  124.     cout << p1 << ", " << p2 << '\n';
  125.  
  126.     cout << p1.distance(point()) << ", "<< p3.distance(p1) << '\n';
  127.  
  128.     cout << p1 + p2 << ", " << p1 - p3 << '\n';
  129.  
  130.     cout << 3.14 * p2 << ", " << p2 * 3.14 << '\n';
  131.  
  132.     cout << (p1 < p3) << ", " << (p1 == point(1.0000000001, 1.0, 1.0)) << '\n';
  133.  
  134.     cin >> p1;
  135.     cout << p1 << '\n';
  136.  
  137.  
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement