Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct Vector
- {
- double x;
- double y;
- bool operator<(const Vector&);
- friend istream& operator>>(istream&, Vector&);
- friend ostream& operator<<(ostream&, const Vector&);
- };
- bool Vector::operator<(const Vector& v)
- {
- return (x*x+y*y) < (v.x*v.x+v.y*v.y);
- }
- istream& operator>>(istream& input, Vector& v)
- {
- return input >> v.x >> v.y;
- }
- ostream& operator<<(ostream& output, const Vector& v)
- {
- return output << '(' << v.x << ", " << v.y << ')';
- }
- template <class T>
- void selection_sort(T list[], int len)
- {
- for (int i = 0; i < len - 1; i++)
- {
- int min = i;
- for (int j = i + 1; j < len; j++)
- if (list[j] < list[min])
- min = j;
- if (min != i)
- {
- T xchg = list[i];
- list[i] = list[min];
- list[min] = xchg;
- }
- }
- }
- int main()
- {
- int n;
- cout << "Введите количество векторов: ";
- cin >> n;
- if (n < 1)
- {
- cerr << "Введён неверный параметр!" << endl;
- return 1;
- }
- Vector *va = new Vector[n];
- cout << "Введите поочерёдно каждый вектор:" << endl;
- for (int i = 0; i < n; i++)
- cin >> va[i];
- cout << endl << "Введённые вектора:" << endl;
- for (int i = 0; i < n; i++)
- cout << va[i] << endl;
- selection_sort(va, n);
- cout << endl << "Отсортированные вектора:" << endl;
- for (int i = 0; i < n; i++)
- cout << va[i] << endl;
- return 0;
- }
Add Comment
Please, Sign In to add comment