美文网首页
C++控制一组字符串按照字母顺序排序的代码

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

作者: 简简单单咦 | 来源:发表于2019-02-25 17:02 被阅读0次

在工作期间,将内容过程重要的内容做个备份,下面内容内容是关于C++控制一组字符串按照字母顺序排序的内容,希望对小伙伴们有些用处。

  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.

          Listing of the cities in alphabetical order

              Print out the listing of cities stored

      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 <conio.h>

#include <cstring>

using namespace std;

void instruct (void);

void pause ();

int main ()

{

int i = 0;

const int arraySize = 15;

                          "Kalihi", "Waipahu", "Pearl Harbor", "Hawaii Kai",

      "Mililani", "Waikiki", "Kaneohe", "Kapolei",

      "Salt Lake", "Ewa Beach", "Manoa" };

instruct ();

cout << "The original listing of cities:nn";

for ( i = 0; i < arraySize; ++i )

        cout << cities[ i ] << "n" ;

pause();

BubbleSort( cities , arraySize );

        cout << "The alphabetical listing of cities:nn";

        for ( i = 0; i < arraySize; ++i )

        cout << cities[ i ] << "n" ;

        pause ();

return 0;

}

{

    int result;

for ( int pass = 0; pass < size - 1 ; ++pass ){

for ( int j = 0; j < size - 1 - pass; ++j ){

                result = strcmp (array[j], array[j+1]);

                if (result > 0)

  swap ( array[j] , array[j+1] );

}

}

}

void instruct (void)

{

              cout << "This program will take a lising of 15 cities located in a string "

  << "array and willnprint out the original listing of cities. It "

  << "will then take the same stringnarray, do a sting compare, sort "

  << "if necessary and rearrange the string array tonplace it in "

  << "alphabetical order. Finally it will print out the alphabetical.n"

  << "listing of the cities in the string array.n"

  << "_______________________________________________________________"

  << "_____________"

  << "n" << endl;

            pause();

}

void pause ()

{

    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++控制一组字符串按照字母顺序排序的代码

    在工作期间,将内容过程重要的内容做个备份,下面内容内容是关于C++控制一组字符串按照字母顺序排序的内容,希望对小伙...

  • 力扣 953 验证外星语词典

    题意:给定一个字母排序以及一个字符串array,查看字符串array是否按照字母顺序排序 思路:见代码注释 思想:...

  • 三、Java面向对象-集合排序

    List字符串排序顺序如下: 默认按照首字母顺序排序 首字母相同的情况下,依次比较第二个、直到第n个字母 排序顺序...

  • JS简单编程

    冒泡排序 选择排序 把下面的字符串去重,并去除掉特殊字符按照数字在前字母在后的顺序排序字符串 如下:“12...

  • 1.0.0传统的冒泡排序

    0.算法解决的问题 排序就是将 一组对象 按照 某种规则 排列 的过程。某种规则 可以指按时间排序、首字母顺序排序...

  • js 数组排序

    js 数组默认是按照数字的字母顺序排序

  • 字符大小写排序

    LintCode题目地址 给定一个只包含字母的字符串,按照先小写字母后大写字母的顺序进行排序。 注意事项小写字母或...

  • 49. 字符大小写排序

    描述 给定一个只包含字母的字符串,按照先小写字母后大写字母的顺序进行排序。 注意事项 小写字母或者大写字母他们之间...

  • 对象转字符串(按照属性排序)

    如题,该工具通过反射实现对象转json字符串,可以按照对象属性首字母(首字母相同则依次向后比对)进行顺序或逆序排序...

  • LintCode 49 [Sort Letters by Cas

    原题 给定一个只包含字母的字符串,按照先小写字母后大写字母的顺序进行排序。 给出"abAcD",一个可能的答案为"...

网友评论

      本文标题:C++控制一组字符串按照字母顺序排序的代码

      本文链接:https://www.haomeiwen.com/subject/jdbgyqtx.html