c语言怎么用姓名(汉字)进行排序

汉字的第一个字母
2025-05-15 20:39:44
推荐回答(4个)
回答1:

写出姓氏与第一个字母的映射表,比如
赵->Z;
钱->Q;
孙->S;
李->L;
周->Z;
吴->W;
郑->Z;
王->W

然后需要用到排序的时候,再调用它对应的字母来进行排序,也就是比较ASCII码的大小了。编码就自己去实现啦!

回答2:

排序后输出的:(是以字母表从小到大排)
#include
#include
using namespace std;
int main()
{
string s[4]={"大军","阿姨","弟弟","妹妹"},t;
for(int i=0;i<4;i++)
{
for(int j=i;j<4;j++)
{
if(s[i]>s[j])
{
t=s[i];
s[i]=s[j];
s[j]=t;
}
}
}
for(i=0;i<4;i++)
cout< return 0;
}

回答3:

#include
#include
#include
#define N 3
struct people
{
char name[20];
char sex;//M or F
int year;
}peo[N];

void input()
{
for(int i=0;i {
scanf("%s %c%d",peo[i].name,&peo[i].sex,&peo[i].year);
}
}

void sort(struct people *p,int n)
{
struct people tmp;
for(int i=0;i {
for(int j=i+1;j {
if(strcmp(p[i].name,p[j].name)>0)
{
tmp=p[i];
p[i]=p[j];
p[j]=tmp;
}
}
}
}

void output()
{
for(int i=0;i {
printf("%s %c %d",peo[i].name,peo[i].sex,peo[i].year);
printf("\n");
}
}
int main()
{
printf("please input the data:\n");
input();
sort(peo,N);
printf("after sort:\n");
output();
return 0;
}

回答4:

可以写汉字库,然后按笔画排序