一个c语言问题:对结构体的排序问题

来源:百度知道 编辑:UC知道 时间:2024/06/15 23:42:20
typedef struct /*定义结构体数组*/
{
char num[10]; /*学号*/
char name[20]; /*姓名*/
int age; /*年龄*/
char sex[20]; /*性别*/
char birthday[20]; /*出生年月*/
char address[50]; /*地址*/
int tel; /*电话*/
char e_mail[50]; /*邮箱*/
}Student;
Student stu[80]; /*结构体数组变量*/
.........................
.......对结构体进行数据记录..................
..........(省略)..............
........................
void Sort_by_num(Student stud[],int n) /*按学号排序*/
{ int i,j;
char t[10];
for(i=0;i<n-1;i++) /*冒泡法排序*/
for(j=0;j<n-1-i;j++)
if(strcmp(stud[j].num,stud[j+1].num)>0)
{strcpy(t,stud[j+1].num);
strcpy(stud[j+1].num,stud[j].num);
strcpy(stud[j].num,t);
strcpy(stud[j+1].name,stud[j].name);

typedef struct /*定义结构体数组*/
{
char num[10]; /*学号*/
char name[20]; /*姓名*/
int age; /*年龄*/
char sex[20]; /*性别*/
char birthday[20]; /*出生年月*/
char address[50]; /*地址*/
int tel; /*电话*/
char e_mail[50]; /*邮箱*/
}Student;
Student stu[80]; /*结构体数组变量*/

void Sort_by_num(Student stud[],int n) /*按学号排序*/
{ int i,j;
Student t;
bool key;
for(i=0;i<n-1;i++) /*冒泡法排序*/
{
key=1;
for(j=0;j<n-1-i;j++)
if(strcmp(stud[j].num,stud[j+1].num)>0)
{
key=0;
strcpy(t.num,stud[j+1].num);
strcpy(t.name,stud[j+1].name);
strcpy(t.sex,stud[j+1].sex);
strcpy(t.birthday,stud[j+1].birthday);
strcpy(t.address,stud[j+1].address);