Advertisement
Neveles

Untitled

Nov 13th, 2019
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.34 KB | None | 0 0
  1. // .h
  2.  
  3. #pragma once
  4. #ifndef POINT_H
  5. #define POINT_H
  6.  
  7. class Point
  8. {
  9. private:
  10. int x_;
  11. int y_;
  12. public:
  13. void setX(int x);
  14. void setY(int y);
  15. void setXY(int x, int y);
  16.  
  17. int getX() const;
  18. int getY() const;
  19.  
  20. void show() const;
  21.  
  22. friend double getDistance(const Point& a, const Point& b);
  23. friend bool isEqual(const Point &a, const Point &b);
  24.  
  25. Point();
  26. Point(int x, int y);
  27. Point(const Point &ob);
  28. ~Point();
  29.  
  30. Point operator+ (const Point& ob2); // бинарный плюс
  31. Point operator+ (int k); // унарный плюс
  32. Point operator+ ();
  33.  
  34. Point operator- (const Point& ob2); // бинарный минус
  35. Point operator- (int k);
  36. Point operator- (); // унарный минус
  37.  
  38. Point operator= (const Point& ob2); // бин
  39.  
  40. bool operator== (const Point& ob2); // бин
  41.  
  42. friend Point operator- (const Point& ob1, const Point& ob2);
  43. friend Point operator- (const Point& ob1, int k);
  44. friend Point operator- (int k, const Point& ob2);
  45. //friend ostream &operator<< (ostream& out, Point ob);
  46. //friend istream &operator>> (istream& in, Point& ob);
  47. };
  48.  
  49. #endif
  50.  
  51. // cpp
  52.  
  53. #include "Point.h"
  54. #include <cmath>
  55. #include <iostream>
  56.  
  57. using namespace std;
  58.  
  59. void Point::setX(int x)
  60. {
  61. x_ = x;
  62. }
  63. void Point::setY(int y)
  64. {
  65. y_ = y;
  66. }
  67. void Point::setXY(int x, int y)
  68. {
  69. x_ = x; y_ = y;
  70. }
  71.  
  72. int Point::getX() const
  73. {
  74. return x_;
  75. }
  76. int Point::getY() const
  77. {
  78. return y_;
  79. }
  80.  
  81. void Point::show() const
  82. {
  83. cout << "(" << x_ << "," << y_ << ")" << endl;
  84. }
  85.  
  86. double getDistance(const Point& a, const Point& b)
  87. {
  88. return sqrt(pow(a.x_ - b.x_, 2) + pow(a.y_ - b.y_, 2));
  89. }
  90. bool isEqual(const Point &a, const Point &b)
  91. {
  92. return (a.x_ == b.x_ && a.y_ == b.y_);
  93. }
  94.  
  95. Point::Point() // конструктор по умолчанию
  96. :x_(0), y_(0) {}
  97.  
  98. Point::Point(int x, int y) // конструктор
  99. :x_(x), y_(y) {}
  100.  
  101. Point::Point(const Point& ob) // конструктор копирования
  102. :x_(ob.x_), y_(ob.y_) {}
  103.  
  104. Point::~Point() {} // деструктор
  105.  
  106. Point Point::operator+ (const Point& ob2)
  107. {
  108. return Point(x_ + ob2.x_, y_ + ob2.y_);
  109. }
  110. Point Point::operator+ (int k)
  111. {
  112. return Point(x_ + k, y_ + k);
  113. }
  114. Point Point::operator+ ()
  115. {
  116. return Point(+x_, +y_);
  117. }
  118.  
  119. Point Point::operator- (const Point& ob2)
  120. {
  121. return Point(x_ - ob2.x_, y_ - ob2.y_);
  122. }
  123. Point Point::operator- (int k)
  124. {
  125. return Point(x_ - k, y_ - k);
  126. }
  127. Point Point::operator- ()
  128. {
  129. return Point(-x_, -y_);
  130. }
  131.  
  132. Point Point::operator= (const Point& ob2)
  133. {
  134. x_ = ob2.x_; // this->x_ = ob2.x_
  135. y_ = ob2.y_; // this->y_ = ob2.y_
  136. // возвращение объекта , которому присвоено значение
  137. // if (this == &ob2) return *this;
  138. return *this;
  139. }
  140.  
  141. bool Point::operator== (const Point& ob2)
  142. {
  143. return (x_ == ob2.x_) && (y_ == ob2.y_);
  144. }
  145.  
  146. Point operator- (const Point& ob1, const Point& ob2)
  147. {
  148. return Point(ob1.x_ - ob2.x_, ob1.y_ - ob2.y_);
  149. }
  150. Point operator- (const Point& ob1, int k)
  151. {
  152. return Point(ob1.x_ - k, ob1.y_ - k);
  153. }
  154. Point operator- (int k, const Point& ob2)
  155. {
  156. return Point(k - ob2.x_, k - ob2.y_);
  157. }
  158.  
  159. //ostream& operator<< (ostream& out, Point ob)
  160. //{
  161. // return (out << "x = " << ob.x_ << " y = " << ob.y_);
  162. //}
  163. //
  164. //istream& operator>> (istream& in, Point& ob)
  165. //{
  166. // return (in >> ob.x_ << ob.y_);
  167. //}
  168.  
  169. // main
  170.  
  171. #include <iostream>
  172. #include <climits>
  173. #include "Point.h"
  174. #include "Triangle.h"
  175.  
  176. using namespace std;
  177.  
  178. const string COORD_INPUT_ERROR = "Point's coordinates input error!";
  179. const string EMPTY_SEQ_ERROR = "Empty sequence of points entered!";
  180.  
  181. int main()
  182. {
  183. setlocale(LC_ALL, "rus");
  184.  
  185. try
  186. {
  187. // Point
  188.  
  189. cout << "Points' coordinates input: " << endl;
  190. int x = 0;
  191. int y = 0;
  192. cin >> x >> y;
  193. if (!cin)
  194. throw COORD_INPUT_ERROR;
  195. if (x == 0 && y == 0)
  196. throw EMPTY_SEQ_ERROR;
  197. Point pt(x, y);
  198. Point pt0;
  199. Point ptMin(x, y);
  200. double minPointRad = getDistance(ptMin, pt0);
  201. while (x != 0 || y != 0)
  202. {
  203. pt.setXY(x, y);
  204. if (getDistance(pt, pt0) < minPointRad)
  205. {
  206. minPointRad = getDistance(pt, pt0);
  207. ptMin.setXY(x, y);
  208. }
  209. cin >> x >> y;
  210. if (!cin)
  211. throw COORD_INPUT_ERROR;
  212. }
  213. cout << endl << "Point closest to the origin and the distance from the origin to it: (" << ptMin.getX() << "," << ptMin.getY() << ") - " << minPointRad << endl;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement