[ ] |
const int n = 15;
string cities[n] = {
"", "", "", "", "",
"", "", "", "", "",
"", "", "", "", ""
};
typedef bool(*CompareFunction) (int i, int j);
bool compareUp(int i, int j) {
return cities[i] < cities[j];
}
bool compareDown(int i, int j) {
return cities[i] > cities[j];
}
bool compareLengthUp(int i, int j) {
return cities[i].length() < cities[j].length();
}
bool compareLengthDown(int i, int j) {
return cities[i].length() > cities[j].length();
}
CompareFunction compares[] = {compareUp, compareDown, compareLengthUp, compareLengthDown};
void sortCities(CompareFunction compare) {
for (int i = 0; i < n - 1; i++) {
int j_min = i;
for (int j = i+1; j < n; j++) {
if (compare(j, j_min)) {
j_min = j;
}
}
string temp = cities[i];
cities[i] = cities[j_min];
cities[j_min] = temp;
}
}
int _tmain(int argc, _TCHAR* argv[]) {
setlocale(LC_ALL, "Russian");
for (;;) {
int choice;
cout << " : ";
cin >> choice;
sortCities(compares[choice]);
printCities();
}
system("pause");
return 0;
}