源程序:
#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;
}
}
}
}
void 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);
}
运行结果 :