6.3w+
社区成员
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
struct POINT
{
public:
float x,y;
bool operator < (const POINT& right)
{
if (this->x < right.x) //按x坐标排序。如果要按y坐标,则把pt_cmp函数中的x换成y
{
return true;
}
else if (this->x == right.x)
{
if (this->y <= right.y)
{
return true;
}
else
return false;
}
return false;
}
};
void Output(vector<POINT>& pArr)
{
for(int i=0; i<pArr.size(); i++)
{
cout<<"("<<pArr[i].x<<", "<<pArr[i].y<<")\n";
}
}
int main()
{
//(9,20.95)(1.25,6)(10,36)(27,8)(27,0.05)(27,1)
//(27,30)(90,110)(27.05,17.9)(90,3)(……)
vector<POINT> vec;
do
{
POINT pt;
cout << "请输入您的二维坐标点(格式:x y,输入-1 -1表示结束):";
cin >> pt.x >> pt.y;
if (pt.x == -1 && pt.y == -1)
{
char ch = 'Y';
cout<<"您共输入"<<vec.size()<<"个二维坐标点, 对吧(输入Y 或 N):";
cin>>ch;
if (ch == 'N' || ch == 'n')
{
cout << "请重新输入" << endl;
vec.clear();
continue;
}
break;
}
vec.push_back(pt);
}while(1);
system("cls"); //clear screen
sort(vec.begin(), vec.end());
Output(vec);
return 0;
}