当前位置: 代码迷 >> C语言 >> [求助]字符串排序为什么会出错
  详细解决方案

[求助]字符串排序为什么会出错

热度:232   发布时间:2006-05-18 21:19:00.0

/* 字符串排序 */
#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;
}


----------------解决方案--------------------------------------------------------
  相关解决方案