#include <iostream>
#include <iterator>
#include <vector>
#include <map>
#include <list>
#include <string>
#include <algorithm>
using namespace std;
class Point
{
private:
int x,y;
public:
Point(int x,int y):x(x),y(y){}
double slopeTo(const Point& that) const
{
if (x == that.x && y == that.y) return - numeric_limits<double>::infinity();
if (x == that.x) return numeric_limits<double>::infinity();
if (y == that.y) return 0;
return (that.y - y) / (double)(that.x - x);
}
bool operator< (const Point& that)const
{
if (y < that.y) return true;
if (y == that.y && x < that.x) return true;
return false;
}
friend ostream& operator<< (ostream&, Point& p);
};
class cmpBySlope
{
private:
Point origin;
public:
cmpBySlope(Point& a) : origin(a){}
bool operator() (const Point& left,const Point& right)const
{
return origin.slopeTo(left) < origin.slopeTo(right);
}
};
ostream& operator<< (ostream& out, Point& p)
{
cout << "(" << p.x << "," << p.y << ")" ;
return out;
}
int N;
vector<Point> v;
void create()
{
cin >> N;
for (int i = 0 ; i < N; i++)
{
int x,y;
cin >> x >> y;
Point* p = new Point(x,y);
v.push_back(*p);
}
cout << "Input reading has complete!" << endl;
}
int main(void)
{
create();
cout << "Sort by natural order : ";
sort(v.begin(),v.end());
for (Point& p : v)
cout << p << " ";
cout << endl;
cout << "Sort by slope : ";
sort(v.begin(),v.end(),cmpBySlope(v[2]));
for (Point& p : v)
{
cout << p << " ";
}
cout << endl;
}
cpp study
最新推荐文章于 2024-03-21 21:45:52 发布