• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

C++控制一组字符串按照字母顺序排序

OC/C/C++ 水墨上仙 1224次浏览

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...
*/


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明C++控制一组字符串按照字母顺序排序
喜欢 (0)
加载中……