Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // .h
- #pragma once
- #ifndef POINT_H
- #define POINT_H
- class Point
- {
- private:
- int x_;
- int y_;
- public:
- void setX(int x);
- void setY(int y);
- void setXY(int x, int y);
- int getX() const;
- int getY() const;
- void show() const;
- friend double getDistance(const Point& a, const Point& b);
- friend bool isEqual(const Point &a, const Point &b);
- Point();
- Point(int x, int y);
- Point(const Point &ob);
- ~Point();
- Point operator+ (const Point& ob2); // бинарный плюс
- Point operator+ (int k); // унарный плюс
- Point operator+ ();
- Point operator- (const Point& ob2); // бинарный минус
- Point operator- (int k);
- Point operator- (); // унарный минус
- Point operator= (const Point& ob2); // бин
- bool operator== (const Point& ob2); // бин
- friend Point operator- (const Point& ob1, const Point& ob2);
- friend Point operator- (const Point& ob1, int k);
- friend Point operator- (int k, const Point& ob2);
- //friend ostream &operator<< (ostream& out, Point ob);
- //friend istream &operator>> (istream& in, Point& ob);
- };
- #endif
- // cpp
- #include "Point.h"
- #include <cmath>
- #include <iostream>
- using namespace std;
- void Point::setX(int x)
- {
- x_ = x;
- }
- void Point::setY(int y)
- {
- y_ = y;
- }
- void Point::setXY(int x, int y)
- {
- x_ = x; y_ = y;
- }
- int Point::getX() const
- {
- return x_;
- }
- int Point::getY() const
- {
- return y_;
- }
- void Point::show() const
- {
- cout << "(" << x_ << "," << y_ << ")" << endl;
- }
- double getDistance(const Point& a, const Point& b)
- {
- return sqrt(pow(a.x_ - b.x_, 2) + pow(a.y_ - b.y_, 2));
- }
- bool isEqual(const Point &a, const Point &b)
- {
- return (a.x_ == b.x_ && a.y_ == b.y_);
- }
- Point::Point() // конструктор по умолчанию
- :x_(0), y_(0) {}
- Point::Point(int x, int y) // конструктор
- :x_(x), y_(y) {}
- Point::Point(const Point& ob) // конструктор копирования
- :x_(ob.x_), y_(ob.y_) {}
- Point::~Point() {} // деструктор
- Point Point::operator+ (const Point& ob2)
- {
- return Point(x_ + ob2.x_, y_ + ob2.y_);
- }
- Point Point::operator+ (int k)
- {
- return Point(x_ + k, y_ + k);
- }
- Point Point::operator+ ()
- {
- return Point(+x_, +y_);
- }
- Point Point::operator- (const Point& ob2)
- {
- return Point(x_ - ob2.x_, y_ - ob2.y_);
- }
- Point Point::operator- (int k)
- {
- return Point(x_ - k, y_ - k);
- }
- Point Point::operator- ()
- {
- return Point(-x_, -y_);
- }
- Point Point::operator= (const Point& ob2)
- {
- x_ = ob2.x_; // this->x_ = ob2.x_
- y_ = ob2.y_; // this->y_ = ob2.y_
- // возвращение объекта , которому присвоено значение
- // if (this == &ob2) return *this;
- return *this;
- }
- bool Point::operator== (const Point& ob2)
- {
- return (x_ == ob2.x_) && (y_ == ob2.y_);
- }
- Point operator- (const Point& ob1, const Point& ob2)
- {
- return Point(ob1.x_ - ob2.x_, ob1.y_ - ob2.y_);
- }
- Point operator- (const Point& ob1, int k)
- {
- return Point(ob1.x_ - k, ob1.y_ - k);
- }
- Point operator- (int k, const Point& ob2)
- {
- return Point(k - ob2.x_, k - ob2.y_);
- }
- //ostream& operator<< (ostream& out, Point ob)
- //{
- // return (out << "x = " << ob.x_ << " y = " << ob.y_);
- //}
- //
- //istream& operator>> (istream& in, Point& ob)
- //{
- // return (in >> ob.x_ << ob.y_);
- //}
- // main
- #include <iostream>
- #include <climits>
- #include "Point.h"
- #include "Triangle.h"
- using namespace std;
- const string COORD_INPUT_ERROR = "Point's coordinates input error!";
- const string EMPTY_SEQ_ERROR = "Empty sequence of points entered!";
- int main()
- {
- setlocale(LC_ALL, "rus");
- try
- {
- // Point
- cout << "Points' coordinates input: " << endl;
- int x = 0;
- int y = 0;
- cin >> x >> y;
- if (!cin)
- throw COORD_INPUT_ERROR;
- if (x == 0 && y == 0)
- throw EMPTY_SEQ_ERROR;
- Point pt(x, y);
- Point pt0;
- Point ptMin(x, y);
- double minPointRad = getDistance(ptMin, pt0);
- while (x != 0 || y != 0)
- {
- pt.setXY(x, y);
- if (getDistance(pt, pt0) < minPointRad)
- {
- minPointRad = getDistance(pt, pt0);
- ptMin.setXY(x, y);
- }
- cin >> x >> y;
- if (!cin)
- throw COORD_INPUT_ERROR;
- }
- 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