Advertisement
CzarnyBarszcz

zadanie5

Dec 17th, 2020
1,182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.70 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3. #include <iomanip>
  4.  
  5. using namespace std;
  6.  
  7. class point
  8. {
  9.     double xyz[3];
  10.    
  11. public:
  12.     point ()
  13.     {
  14.         this->xyz[0] = 0.0;
  15.         this->xyz[1] = 0.0;
  16.         this->xyz[2] = 0.0;
  17.     }
  18.     point (double *xyz)
  19.     {
  20.         this->xyz[0] = xyz[0];
  21.         this->xyz[1] = xyz[1];
  22.         this->xyz[2] = xyz[2];
  23.     }
  24.     point (double x,double y, double z)
  25.     {
  26.         this->xyz[0]=x;
  27.         this->xyz[1]=y;
  28.         this->xyz[2]=z;
  29.     }
  30.     friend ostream& operator<<(ostream& out,const point& p)
  31.     {
  32.         return out<<p.xyz[0]<<"\t"<<p.xyz[1]<<"\t"<<p.xyz[2]<<"\n";
  33.     }
  34.     friend istream& operator >> (istream& s, point& p1)
  35. {
  36.     double x,y,z;
  37.     s >> x>> y>> z;
  38.     p1.xyz[0]=x;
  39.     p1.xyz[1]=y;
  40.     p1.xyz[2]=z;
  41.     return s;
  42. }
  43.     const double& operator []  (int a) const
  44.     {
  45.         return xyz[a];
  46.     }
  47.     double distance(point p) const
  48.     {
  49.         double x1,y1,z1,x2,y2,z2,d;
  50.         x1=this->xyz[0];
  51.         y1=this->xyz[1];
  52.         z1=this->xyz[2];
  53.         x2=p.xyz[0];
  54.         y2=p.xyz[1];
  55.         z2=p.xyz[2];
  56.         d= sqrt(pow((x1-x2),2)+pow((y1-y2),2)+pow((z1-z2),2));
  57.         return d;
  58.     }
  59.  
  60.     point operator + (const point p) const
  61.     {
  62.         point pp(this->xyz[0]+p.xyz[0],this->xyz[1]+p.xyz[1],this->xyz[2]+p.xyz[2]);
  63.         return pp;
  64.     }
  65.     point operator - (const point p) const
  66.     {
  67.         point pp(this->xyz[0]-p.xyz[0],this->xyz[1]-p.xyz[1],this->xyz[2]-p.xyz[2]);
  68.         return pp;
  69.     }
  70. };
  71. point operator * (double a, point &p)
  72. {
  73.     return point(p[0]*a,p[1]*a,p[2]*a);
  74. }
  75. point operator * (point &p, double a)
  76. {
  77.     return point(p[0]*a,p[1]*a,p[2]*a);
  78. }
  79. string operator < (point& p1,const point& p2)
  80. {
  81.     if (p1.distance(point())<p2.distance(point()))
  82.         return  "Prawy punkt jest wiekszy";
  83.     else
  84.         return "Lewy punkt jest wiekszy";
  85. }
  86. string operator > (point& p1,const point& p2)
  87. {
  88.     if (p1.distance(point())>p2.distance(point()))
  89.         return  "Lewy punkt jest wiekszy";
  90.     else
  91.         return "Prawy punkt jest wiekszy";
  92. }
  93. string operator == (point& p1,const point& p2)
  94. {
  95.     if (p1.distance(point())==p2.distance(point()))
  96.         return  "Punkty sa rowne";
  97.     else
  98.         return "Punkty sa rozne";
  99. }
  100.  
  101.  
  102. int main()
  103. {
  104. double x[2][3] = {{1.0, 1.0, 1.0},
  105.  {1.0, 2.0, 3.0}};
  106. point p1(x[0]), p2(x[1]);
  107. const point p3(0.4, 0.2, 0.1);
  108. cout << p1 << ", " << p2 << '\n';
  109. cout << p3[0] << ' ' << p3[1] << ' ' << p3[2] << '\n';
  110. cout << p1.distance(point()) << ", "<< p3.distance(p1) << '\n';
  111. cout << p1 + p2 << ", " << p1 - p3 << '\n';
  112. cout << 3.14 * p2 << ", " << p2 * 3.14 << '\n';
  113. cout << (p1 < p3) << ", " << (p1 == point(1.0, 1.0, 1.0)) << '\n';
  114. cin >> p1;
  115. cout << p1 << '\n';
  116.  
  117.  
  118.  
  119.  
  120.     return 0;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement