Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- using namespace std;
- class point
- {
- double tab[3];
- friend istream & operator >> (istream &, point &);
- public:
- point()
- {
- for(unsigned i=0; i<3; i++)
- tab[i]=0;
- }
- // x jest referencja do tablicy 3 elementowej
- point(const double (&x)[3])
- {
- tab[0] = x[0];
- tab[1] = x[1];
- tab[2] = x[2];
- }
- point(const double &x, const double &y, const double &z)
- {
- tab[0] = x;
- tab[1] = y;
- tab[2] = z;
- }
- const double & operator [] (const unsigned &i) const
- {
- if (i>2)
- {
- cout << "POZA ZAKRESEM!!!";
- }
- return tab[i];
- }
- const double distance(const point &p) const
- {
- 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]) ));
- }
- // dodawanie jako metoda
- const point operator + (const point &p) const
- {
- return point(tab[0]+p[0], tab[1]+p[1], tab[2]+p[2]);
- }
- // mnozenie , te moze byc jako metoda
- const point operator * (const double &P)
- {
- return point(tab[0]*P, tab[1]*P, tab[2]*P);
- }
- // porownywanie jako metoda
- bool operator == (const point &p)
- {
- double eps = 1e-10;
- if(abs(this->distance(point()) - p.distance(point())) < eps)
- return true;
- return false;
- }
- };
- //odejmowanie jako niemetoda
- const point operator - (const point &l, const point &p)
- {
- return point(l[0]-p[0], l[1]-p[1], l[2]-p[2]);
- }
- // mnozenie ( z lewej strony elelement nienalezacy do klasy wiec nie moze to byc metoda)
- const point operator * (const double & L, const point &p)
- {
- return point(L*p[0], L*p[1], L*p[2]);
- }
- // MUSI BYC TU, BO LEWY ARGUMENT << NIE JEST ELEMENTEM KLASY, NIE TRZEBA ZAPRZYJAZANIAC BO MAMY METODY DOSTEPOWE []
- ostream & operator << (ostream &out, const point &p)
- {
- out << p[0] << " " << p[1] << " " << p[2];
- return out;
- }
- // musialem zaprzyjaznic bo na metodach dostepowych [] nie dzialalo.
- istream & operator >> (istream &in, point &p)
- {
- in >> p.tab[0] >> p.tab[1] >> p.tab[2];
- return in;
- }
- bool operator < (const point &l, const point &p)
- {
- if (l.distance(point()) < p.distance(point()))
- return true;
- return false;
- }
- int main()
- {
- double x[2][3] = {{1.0, 1.0, 1.0},{1.0, 2.0, 3.0}};
- point p1(x[0]), p2(x[1]);
- const point p3(0.4, 0.2, 0.1);
- cout << p1 << ", " << p2 << '\n';
- cout << p1.distance(point()) << ", "<< p3.distance(p1) << '\n';
- cout << p1 + p2 << ", " << p1 - p3 << '\n';
- cout << 3.14 * p2 << ", " << p2 * 3.14 << '\n';
- cout << (p1 < p3) << ", " << (p1 == point(1.0000000001, 1.0, 1.0)) << '\n';
- cin >> p1;
- cout << p1 << '\n';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement