本程序意在输入城市信息进行保管(名字,坐标),其中print函数用来打印指定城市的指定范围内的城市群名字,两个随机函数用来生成坐标和名字。
#include<iostream> #include<string> #include<cmath> #include<ctime> #include<cassert> using namespace std; class Coordinate { double x; double y; public : Coordinate(double i=0, double j=0) : x(i), y(j) {} double return_x() { return x; } double return_y() { return y; } bool operator ==(Coordinate temp) { return (fabs(temp.x-x)<1e-6) && (fabs(temp.y - y)<1e-6); } }; class City { Coordinate coordinate; public : string name; City(string s, double i, double j) :name(s), coordinate(i, j) {} City() {} string getname() { return name;} Coordinate getCoordinate() { return coordinate; } void setname(string s) { name = s; } void operator=(City temp) { name = temp.name; coordinate = temp.coordinate; } }; double Distance(Coordinate * c1, Coordinate * c2) { return sqrt(pow((c1->return_x() - c2->return_x()), 2) + pow((c1->return_y() - c2->return_y()), 2)); } class Acitydict { private : City * citylist; int maxsize; int size; int curr; public : Acitydict(int i) { citylist = new City[i]; maxsize = i; size = 0; } ~Acitydict() { delete[]citylist; maxsize = size = 0; } Coordinate find(string s) { for (curr = 0; curr < size; curr++) { if (s == citylist[curr].getname()) return citylist[curr].getCoordinate(); } return NULL; } string find(int x,int y) { Coordinate temp(x, y); for (curr = 0; curr < size; curr++) { if (temp == citylist[curr].getCoordinate()) return citylist[curr].getname(); } return NULL; } void insert(string s, double x, double y) { if (size == maxsize) resize(); City temp(s, x, y); citylist[size] = temp; size++; } void resize() { City *temp = new City[maxsize]; for (int i = 0; i < maxsize; i++) temp[i] = citylist[i]; maxsize *= 2; citylist = new City[maxsize]; for (int i = 0; i < size; i++) citylist[i] = temp[i]; } Coordinate remove(string s) { Coordinate temp = find(s); if (curr == size) return Coordinate(0,0); for (int i = curr; i <size-1; i++) citylist[i] = citylist[i + 1]; size--; } void Print(double distance,string s) { City *temp = new City[size]; for (int i = 0; i < size; i++) temp[i].setname("END") ; Coordinate city = find(s); int j = 0; for (int i = 0; i < size; i++) { if ( Distance(&citylist[i].getCoordinate(), &city) < distance) temp[j++] = citylist[i]; } for (int i = 0; temp[i].getname() != "END"; i++) cout << temp[i].getname() << " "; } int length() { return size; } void Display() { for (int i = 0; i < size; i++) cout << citylist[i].name << "(" << citylist[i].getCoordinate().return_x() << "," << citylist[i].getCoordinate().return_y() << ") "; cout << endl; } City& getCity(int i) { return citylist[i]; } }; string randstr(int num) { string s; char c = (char)(rand() % 26 + 65); s += c; for (int i = 1; i < num; i++) s += (char)(rand() % 26 + 97); return s; } int randint() { return (rand() % 20 + 5); } int main() { srand((unsigned)time(0)); Acitydict list(5); for (int i = 0; i < 10; i++) { list.insert(randstr(randint()%5+3), randint(), randint()); } list.Display(); list.Print(10, list.getCity(1).getname()); }