/* 字符串排序 */
#include "stdio.h"
#include "conio.h"
#define MAN_COUNT 21 /*21条人名*/
#define NAME_LEN_MAX 30
int main(void)
{
char Name[MAN_COUNT][NAME_LEN_MAX] = /* 串表 */
{
"MaoWenChao",
"ChenShaoJie",
"CaiZhiHong",
"ANiu",
"ZhangChaoBin",
"LiYa",
"HuangShuPeng",
"HuangZhanPeng",
"WangWeiTang",
"ChenDaJian",
"TaoWenChao",
"KhenShaoJie",
"UaiZhiHong",
"ZNiu",
"ChangChaoBin",
"PiYa",
"SuangShuPeng",
"LuangZhanPeng",
"EangWeiTang",
"QhenDaJian",
"AXiong",
};
char* NameMap[MAN_COUNT]; /* 字符串地址映射表,不对原二维数组作任何改动 */
char* TPointer = NULL;
unsigned long int T = 0, Tm, Tn; /*循环辅助变量*/
puts("Initializing map table ...");
while(T < MAN_COUNT) /* 映射表初始化*/
{
NameMap[T] = Name[T];
T++;
}
puts(" > Mapped done.");
puts("Sorting...");
for(Tm = (MAN_COUNT - 1); Tm > 0; Tm--) /* 开始排序 */
{
for(Tn = 0; Tn < Tm; Tn++)
{
if(*NameMap[Tn] > *NameMap[Tn + 1]) /*取每串第一个字符比较,修改地址映射表*/
{
TPointer = NameMap[Tn];
NameMap[Tn] = NameMap[Tn + 1];
NameMap[Tn + 1] = TPointer;
}
}
}
puts(" > Sorted done.");
puts("\nPress any key to Output data.");
getch();
T = 0;
while(T < MAN_COUNT) /* 输出排序后的字符串表 */
{
printf(" %s\n", NameMap[T]);
T++;
}
puts("\n > Out done.");
getch();
return 0;
}
欢迎加入 MVC 技术讨论群(新群:90093426)