姓氏排序
作者:互联网
#include <stdio.h>
#include <string.h>
#define N 5
struct student //数据类型
{
int num; //学号
char sname[25]; //姓名
char sex[4]; //性别
int age; //年龄
};
struct student stu[N]={
{1008,"钟丽琪","女",18},
{1001,"赵文彬","男",19},
{1005,"陈敏芳","女",17},
{1010,"张力维","男",20},
{1009,"吴泽林","男",21}
};
//按姓名排序
void sort(struct student stud[],int n)
{
int i,j;
struct student temp; //临时结构体变量
for(i=0;i<n;i++) //行
{
for(j=0;j<n-i-1;j++) //列
{
if(strcmp(stud[j].sname,stud[j+1].sname)>0)
{
temp=stud[j];
stud[j]=stud[j+1];
stud[j+1]=temp;
}
}
}
}
int main()
{
int i;
printf(" 学号 姓名 年龄\n");
printf("---------------------------\n");
sort(stu,N);
for(i=0;i<N;i++)
printf("%5d%10s%8d\n",stu[i].num,stu[i].sname,stu[i].age);
//printf("%5d%10s%8d\n",stu2.num,stu2.sname,stu2.age);
}
效果图:
标签:sname,struct,int,stu,姓氏,stud,printf,排序 来源: https://www.cnblogs.com/-Qiqiqiiklki/p/16143988.html