# 让结构体按某一个属性排序，减少排序函数复用，怎么弄？

0

``` printf("按学号排序:\n");
SelectSortbyOrder(student);
for(p=student;(p+1)->score!='\000';p++)
{
printf("学号:%d\n姓名:%s\n分数:%d\n地址：%p\n\n",p->order,p->name,p->score,p);
}
printf("\n");

printf("再按分数排序:\n");
SelectSortbyScore(student);
for(p=student;(p+1)->score!='\000';p++)
{
printf("学号:%d\n姓名:%s\n分数:%d\n地址：%p\n\n",p->order,p->name,p->score,p);
}
printf("\n");
return 0;```

```#include <stdio.h>
#include<stdlib.h>
#define TRUE 1
#define FALSE 0
struct  Students
{
int order;
char *name;
int score;

};

typedef struct Students Stu;
void swap(Stu *a,Stu *b)
{
Stu temp;

temp.order  =   a->order;
temp.name   =   a->name ;
temp.score  =   a->score;

a->order     =   b->order;
a->name      =   b->name ;
a->score     =   b->score;

b->order     =   temp.order;
b->name      =   temp.name ;
b->score     =   temp.score;
}

void SelectSortbyOrder(Stu *p)   //用选择排序来排序
{
Stu *min = p;
{
while((p+1)->score!='\000')
{
if(p->order < min->order)
{
min = p;
}
p++;
}
// printf("最小学号:%d\n",min->order);
}

}

void SelectSortbyScore(Stu *p)   //用选择排序来排序
{
Stu *min = p;
{
while((p+1)->score!='\000')
{
if(p->score < min->score)
{
min = p;
}
p++;
}
// printf("最小学号:%d\n",min->order);
}

}

void SelectSort(Stu *p,int *value)   //用选择排序来排序
{
Stu *min = p;
{
while((p+1)->score!='\000')
{
if(p->score < min->score)
{
min = p;
}
p++;
}
// printf("最小学号:%d\n",min->order);
}

}

int main()
{
Stu student[3];
Stu *p;
int i=0;
student[0].order = 402; //  初始化
student[0].name = "zdz";
student[0].score = 79;

student[1].order = 403;
student[1].name = "zdl";
student[1].score = 62;

student[2].order = 401;
student[2].name = "zxl";
student[2].score = 83;

printf("初始记录:\n");
for(p=student;(p+1)->score!='\000';p++)
{
printf("学号:%d\n姓名:%s\n分数:%d\n结构体地址：%p\n学号地址：%p\n姓名地址:%p\n分数地址:%p\n\n",p->order,p->name,p->score,p,p->order,p->name,p->score);
}

printf("按学号排序:\n");
SelectSort(student,&student->order);
for(p=student;(p+1)->score!='\000';p++)
{
printf("学号:%d\n姓名:%s\n分数:%d\n地址：%p\n\n",p->order,p->name,p->score,p);
}
printf("\n");

printf("再按分数排序:\n");
SelectSort(student,&student->score);
for(p=student;(p+1)->score!='\000';p++)
{
printf("学号:%d\n姓名:%s\n分数:%d\n地址：%p\n\n",p->order,p->name,p->score,p);
}
printf("\n");
return 0;
}```

#include <stdio.h>
#include<stdlib.h>
#define TRUE 1
#define FALSE 0
struct  Students
{
int order;
char *name;
int score;

};

typedef struct Students Stu;
void swap(Stu *a,Stu *b)
{
Stu temp;

temp.order  =   a->order;
temp.name   =   a->name ;
temp.score  =   a->score;

a->order     =   b->order;
a->name      =   b->name ;
a->score     =   b->score;

b->order     =   temp.order;
b->name      =   temp.name ;
b->score     =   temp.score;
}

void SelectSortbyOrder(Stu *p)   //用选择排序来排序
{
Stu *min = p;
{
while((p+1)->score!='\000')
{
if(p->order < min->order)
{
min = p;
}
p++;
}
// printf("最小学号:%d\n",min->order);
}

}

void SelectSortbyScore(Stu *p)   //用选择排序来排序
{
Stu *min = p;
{
while((p+1)->score!='\000')
{
if(p->score < min->score)
{
min = p;
}
p++;
}
// printf("最小学号:%d\n",min->order);
}

}

void SelectSort(Stu *p,int *value)   //用选择排序来排序
{
Stu *min = p;
{
while((p+1)->score!='\000')
{
if(p->score < min->score)
{
min = p;
}
p++;
}
// printf("最小学号:%d\n",min->order);
}

}

int main()
{
Stu student[3];
Stu *p;
int i=0;
student[0].order = 402//  初始化
student[0].name = "zdz";
student[0].score = 79;

student[1].order = 403
student[1].name = "zdl";
student[1].score = 62;

student[2].order = 401
student[2].name = "zxl";
student[2].score = 83;

printf("初始记录:\n");
for(p=student;(p+1)->score!='\000';p++)
{
printf("学号:%d\n姓名:%s\n分数:%d\n结构体地址：%p\n学号地址：%p\n姓名地址:%p\n分数地址:%p\n\n",p->order,p->name,p->score,p,p->order,p->name,p->score);
}

printf("按学号排序:\n");
SelectSort(student,&student->order);
for(p=student;(p+1)->score!='\000';p++)
{
printf("学号:%d\n姓名:%s\n分数:%d\n地址：%p\n\n",p->order,p->name,p->score,p);
}
printf("\n");

printf("再按分数排序:\n");
SelectSort(student,&student->score);
for(p=student;(p+1)->score!='\000';p++)
{
printf("学号:%d\n姓名:%s\n分数:%d\n地址：%p\n\n",p->order,p->name,p->score,p);
}
printf("\n");
return 0;
}

1
```#include <stdio.h>
#include<stdlib.h>
#define TRUE 1
#define FALSE 0
struct  Students
{
int order;
char *name;
int score;

};

typedef struct Students Stu;
void swap(Stu *a,Stu *b)
{
Stu temp;

temp.order  =   a->order;
temp.name   =   a->name ;
temp.score  =   a->score;

a->order     =   b->order;
a->name      =   b->name ;
a->score     =   b->score;

b->order     =   temp.order;
b->name      =   temp.name ;
b->score     =   temp.score;
}

void SelectSort(Stu *p,int choose)   //用选择排序来排序
{
Stu *min = p;
{
while((p+1)->score!='\000')
{
if(choose == 0)
{
if(p->order < min->order)
{
min = p;
}
}
else if (choose == 1)
{
if(p->score < min->score)
{
min = p;
}
}

p++;
}
// printf("最小学号:%d\n",min->order);
}

}

int main()
{
Stu student[3];
Stu *p;
int i=0;
student[0].order = 402; //  初始化
student[0].name = "zdz";
student[0].score = 79;

student[1].order = 403;
student[1].name = "zdl";
student[1].score = 62;

student[2].order = 401;
student[2].name = "zxl";
student[2].score = 83;

printf("初始记录:\n");
for(p=student;(p+1)->score!='\000';p++)
{
printf("学号:%d\n姓名:%s\n分数:%d\n地址：%p\n\n",p->order,p->name,p->score,p);
}

printf("按学号排序(输入 0):\n");
SelectSort(student,0);
for(p=student;(p+1)->score!='\000';p++)
{
printf("学号:%d\n姓名:%s\n分数:%d\n地址：%p\n\n",p->order,p->name,p->score,p);
}
printf("\n");

printf("再按分数排序(输入 1):\n");
SelectSort(student,1);
for(p=student;(p+1)->score!='\000';p++)
{
printf("学号:%d\n姓名:%s\n分数:%d\n地址：%p\n\n",p->order,p->name,p->score,p);
}
printf("\n");
return 0;
}```

@Conan-jine 已自己解决，其实你想表达的思想是用布尔值做判断条件来使用哪个语句，但是本来就可以用if封装语句，没必要再设两个函数那么麻烦。

1

return p->score < q->score;

return p->order < q->order;

Conan-jine | 园豆：1272 (小虾三级) | 2020-12-04 19:57

您需要登录以后才能回答，未注册用户请先注册