Guest User

Untitled

a guest
May 23rd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.51 KB | None | 0 0
  1.  
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. struct Vector
  7. {
  8.     double x;
  9.     double y;
  10.     bool operator<(const Vector&);
  11.     friend istream& operator>>(istream&, Vector&);
  12.     friend ostream& operator<<(ostream&, const Vector&);
  13. };
  14.  
  15. bool Vector::operator<(const Vector& v)
  16. {
  17.     return (x*x+y*y) < (v.x*v.x+v.y*v.y);
  18. }
  19.  
  20. istream& operator>>(istream& input, Vector& v)
  21. {
  22.     return input >> v.x >> v.y;
  23. }
  24.  
  25. ostream& operator<<(ostream& output, const Vector& v)
  26. {
  27.     return output << '(' << v.x << ", " << v.y << ')';
  28. }
  29.  
  30. template <class T>
  31. void selection_sort(T list[], int len)
  32. {
  33.     for (int i = 0; i < len - 1; i++)
  34.     {
  35.         int min = i;
  36.         for (int j = i + 1; j < len; j++)
  37.             if (list[j] < list[min])
  38.                 min = j;
  39.  
  40.         if (min != i)
  41.         {      
  42.             T xchg = list[i];
  43.             list[i] = list[min];
  44.             list[min] = xchg;
  45.         }
  46.     }
  47. }
  48.  
  49. int main()
  50. {
  51.     int n;
  52.     cout << "Введите количество векторов: ";
  53.     cin >> n;
  54.  
  55.     if (n < 1)
  56.     {
  57.         cerr << "Введён неверный параметр!" << endl;
  58.         return 1;
  59.     }
  60.  
  61.     Vector *va = new Vector[n];
  62.     cout << "Введите поочерёдно каждый вектор:" << endl;
  63.     for (int i = 0; i < n; i++)
  64.         cin >> va[i];
  65.  
  66.         cout << endl << "Введённые вектора:" << endl;
  67.     for (int i = 0; i < n; i++)
  68.         cout << va[i] << endl;
  69.    
  70.         selection_sort(va, n);
  71.        
  72.         cout << endl << "Отсортированные вектора:" << endl;
  73.     for (int i = 0; i < n; i++)
  74.         cout << va[i] << endl;
  75.            
  76.     return 0;
  77. }
Add Comment
Please, Sign In to add comment