Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Эту программу надо еще улучшать и дорабатывать
- #include <iostream>
- #include <cmath>
- #include <vector>
- #include <algorithm> // Для сортировки
- using namespace std;
- // Определение класса точки в двухмерном пространстве
- class Point
- {
- private:
- double x;
- double y;
- double distance;
- public:
- Point( double X, double Y );
- void display( void );
- double get_distance( void );
- };
- //-------------------------------------------------------------------
- Point::Point( double X, double Y )
- {
- x = X;
- y = Y;
- // Вычислим расстояние по теореме Пифагора
- distance = sqrt( X*X + Y*Y );
- }
- //-------------------------------------------------------------------
- void Point::display( void )
- {
- cout << "Точка ( "<< x <<", "<< y <<" ) дистанция "<<
- distance << endl;
- }
- //-------------------------------------------------------------------
- double Point::get_distance( void )
- {
- return distance;
- }
- //-------------------------------------------------------------------
- //-------------------------------------------------------------------
- // Определение функций
- Point* create_point( void );
- bool compare_point( Point* p1, Point* p2 );
- //-------------------------------------------------------------------
- int main( int argc, char* argv[] )
- {
- // Породим вектор указателей на точки
- vector<Point*> point_storage;
- // И итератор на него
- vector<Point*>::iterator it;
- // Указатель на вновь создаваемую точку
- Point* pt = NULL;
- bool flag = true;
- while( flag )
- {
- pt = create_point();
- if( pt )
- {
- // Сохраним точку в векторе
- point_storage.push_back( pt );
- }
- else
- {
- flag = false;
- }
- }
- // Распечатаем все точки
- for( it = point_storage.begin() ; it != point_storage.end(); ++it )
- {
- (*it) -> display();
- }
- cout << "Сортировка по дистанции до начала координат" << endl;
- sort( point_storage.begin(), point_storage.end(), compare_point );
- // Распечатаем все точки
- for( it = point_storage.begin() ; it != point_storage.end(); ++it )
- {
- (*it) -> display();
- }
- return 0;
- }
- //-------------------------------------------------------------------
- Point* create_point( void )
- {
- Point* pt = NULL;
- double x;
- double y;
- // TODO - этот кусок надо переделать, чтобы ввод можно было
- // завершить по ключевому слову (например STOP) и без выдачи
- // сообщений об ошибках
- cout << "Введите координаты точки" << endl << "X: ";
- cin >> x;
- if( cin.good() )
- {
- cout << "Y: ";
- cin >> y;
- if( cin.good() )
- {
- // Попробуем породить новую точку
- pt = new Point( x, y );
- }
- else
- {
- cout << "Введен мусор вместо Y!" << endl;
- }
- }
- else
- {
- cout << "Введен мусор вместо Х!" << endl;
- }
- return pt;
- }
- //-------------------------------------------------------------------
- bool compare_point( Point* p1, Point* p2 )
- {
- bool result = false;
- if( p1->get_distance() < p2->get_distance() )
- {
- result = true;
- }
- return result;
- }
- //-------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement