C++控制一组字符串按照字母顺序排序
/****************************************************** ** Name: ** Filename: alphabetize.cpp ** Project #: Deitel & Deitel 5.38 ** Project Description: Use the sting comparision functions discussed in Section 5.12.2 and the techniques for sorting arrays developed in Chapter 4 to write a program that alphabetizes a list of strings. Use the names of 10 or 15 towns in your area as data for your program. ** Output: Listing of the cities in the original order Listing of the cities in alphabetical order ** Input: None ** Algorithm: Describe purpose of program to user Print out the listing of cities stored in the string array *cities[]. Next perform function BubbleSort Do number of runs as contained in array Check each string with string compare If Result value > 0 then perform a swap until all strings are sorted in alphabetical listing. Return results of BubbleSort Print out the listing of cities in alphabetical order. End program. ******************************************************/ // Include files #include <iostream> // used for cin, cout #include <conio.h> #include <cstring> using namespace std; // Global Type Declarations // Function Prototypes void instruct (void); void pause (); void BubbleSort( const char *array[] , int size ); //Global Variables - should not be used without good reason. int main () { // Declaration section int i = 0; const int arraySize = 15; const char *cities[ arraySize ] = { "Honolulu", "Aiea", "Pearl City", "Wahiawa", "Kalihi", "Waipahu", "Pearl Harbor", "Hawaii Kai", "Mililani", "Waikiki", "Kaneohe", "Kapolei", "Salt Lake", "Ewa Beach", "Manoa" }; // Executable section instruct (); //Original print out of listing of cities cout << "The original listing of cities:\n\n"; for ( i = 0; i < arraySize; ++i ) cout << cities[ i ] << "\n" ; pause(); // Sort the array BubbleSort( cities , arraySize ); //Print out of listing of cities in alphabetical order cout << "The alphabetical listing of cities:\n\n"; for ( i = 0; i < arraySize; ++i ) cout << cities[ i ] << "\n" ; pause (); return 0; } void BubbleSort( const char *array[] , int size ) { int result; //Performs a run through number of strings for ( int pass = 0; pass < size - 1 ; ++pass ){ //Runs through each string for compare for ( int j = 0; j < size - 1 - pass; ++j ){ //Perform string compare and return value store as result result = strcmp (array[j], array[j+1]); //If value is less than 0 then perform swap function to rearrange if (result > 0) swap ( array[j] , array[j+1] ); } } } void instruct (void) { // Declaration section cout << "This program will take a lising of 15 cities located in a string " << "array and will\nprint out the original listing of cities. It " << "will then take the same string\narray, do a sting compare, sort " << "if necessary and rearrange the string array to\nplace it in " << "alphabetical order. Finally it will print out the alphabetical.\n" << "listing of the cities in the string array.\n" << "_______________________________________________________________" << "_____________" << "\n" << endl; pause(); // Executable section } void pause () { // Declaration section // Executable section cout << "\nPress any key to continue..."; getch(); cout << "\r"; cout << " "; cout << "\r"; } /* Program Output This program will take a lising of 15 cities located in a string array and will print out the original listing of cities. It will then take the same string array, do a sting compare, sort if necessary and rearrange the string array to place it in alphabetical order. Finally it will print out the alphabetical. listing of the cities in the string array. ____________________________________________________________________________ The original listing of cities: Honolulu Aiea Pearl City Wahiawa Kalihi Waipahu Pearl Harbor Hawaii Kai Mililani Waikiki Kaneohe Kapolei Salt Lake Ewa Beach Manoa The alphabetical listing of cities: Aiea Ewa Beach Hawaii Kai Honolulu Kalihi Kaneohe Kapolei Manoa Mililani Pearl City Pearl Harbor Salt Lake Wahiawa Waikiki Waipahu Press any key to continue... */