Advertisement
Guest User

Сортировка точек на плоскости по расстоянию до центра

a guest
Nov 20th, 2018
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.50 KB | None | 0 0
  1. //  Эту программу надо еще улучшать и дорабатывать
  2. #include <iostream>
  3. #include <cmath>
  4. #include <vector>
  5. #include <algorithm>    //  Для сортировки
  6.  
  7. using   namespace   std;
  8.  
  9. //  Определение класса точки в двухмерном пространстве
  10. class   Point
  11. {
  12.     private:
  13.         double  x;
  14.         double  y;
  15.         double  distance;
  16.  
  17.     public:
  18.         Point( double X, double Y );
  19.         void    display( void );
  20.         double  get_distance( void );
  21. };
  22. //-------------------------------------------------------------------
  23. Point::Point( double X, double Y )
  24. {
  25.     x = X;
  26.     y = Y;
  27.     //  Вычислим расстояние по теореме Пифагора
  28.     distance = sqrt( X*X + Y*Y );
  29. }
  30. //-------------------------------------------------------------------
  31. void    Point::display( void )
  32. {
  33.     cout << "Точка ( "<< x <<", "<< y <<" ) дистанция "<<
  34.         distance << endl;
  35. }
  36. //-------------------------------------------------------------------
  37. double  Point::get_distance( void )
  38. {
  39.     return  distance;
  40. }
  41. //-------------------------------------------------------------------
  42.  
  43. //-------------------------------------------------------------------
  44. //  Определение функций
  45. Point*  create_point( void );
  46. bool    compare_point( Point* p1, Point* p2 );
  47. //-------------------------------------------------------------------
  48.  
  49. int main( int argc, char* argv[] )
  50. {
  51.     //  Породим вектор указателей на точки
  52.     vector<Point*>  point_storage;
  53.     //  И итератор на него
  54.     vector<Point*>::iterator    it;
  55.     //  Указатель на вновь создаваемую точку
  56.     Point*  pt = NULL;
  57.     bool    flag = true;
  58.  
  59.     while( flag )
  60.     {
  61.         pt = create_point();
  62.         if( pt )
  63.         {
  64.             //  Сохраним точку в векторе
  65.             point_storage.push_back( pt );
  66.         }
  67.         else
  68.         {
  69.             flag = false;
  70.         }
  71.     }
  72.  
  73.     //  Распечатаем все точки
  74.     for( it = point_storage.begin() ; it != point_storage.end(); ++it )
  75.     {
  76.         (*it) -> display();
  77.     }
  78.  
  79.     cout << "Сортировка по дистанции до начала координат" << endl;
  80.     sort( point_storage.begin(), point_storage.end(), compare_point );
  81.  
  82.     //  Распечатаем все точки
  83.     for( it = point_storage.begin() ; it != point_storage.end(); ++it )
  84.     {
  85.         (*it) -> display();
  86.     }
  87.    
  88.     return  0;
  89. }
  90. //-------------------------------------------------------------------
  91. Point*  create_point( void )
  92. {
  93.     Point*  pt = NULL;
  94.     double  x;
  95.     double  y;
  96.  
  97.     //  TODO - этот кусок надо переделать, чтобы ввод можно было
  98.     //  завершить по ключевому слову (например STOP) и без выдачи
  99.     //  сообщений об ошибках
  100.     cout << "Введите координаты точки" << endl << "X: ";
  101.     cin >> x;
  102.     if( cin.good() )
  103.     {
  104.         cout << "Y: ";
  105.         cin >> y;
  106.         if( cin.good() )
  107.         {
  108.             //  Попробуем породить новую точку
  109.             pt = new Point( x, y );
  110.         }
  111.         else
  112.         {
  113.             cout << "Введен мусор вместо Y!" << endl;
  114.         }
  115.     }
  116.     else
  117.     {
  118.         cout << "Введен мусор вместо Х!" << endl;
  119.     }
  120.  
  121.     return  pt;
  122. }
  123. //-------------------------------------------------------------------
  124. bool    compare_point( Point* p1, Point* p2 )
  125. {
  126.     bool    result = false;
  127.  
  128.     if( p1->get_distance() < p2->get_distance() )
  129.     {
  130.         result = true;
  131.     }
  132.  
  133.     return  result;
  134. }
  135. //-------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement