学生结构体数组,按成绩排序、按年 龄排序,按名字排序...如何实现?
2014-03-18 14:43
645 查看
#include <stdio.h>
//学生结构体数组,按成绩排序、按年龄排序,按名字排序...如何实现?
//1. 动态排序
typedef struct student {
char name[20]; //注意类型不可为
char * name 否则为常量 即不可改变
int age;
float score;
}Student;
typedefBOOL(*pFun)(Student *,Student
*);
BOOL sortWithScore(Student *stu1,Student *stu2) {
return stu1->score < stu2->score;
}
BOOL sortWithName(Student *stu1,Student *stu2) {
returnstrcmp(stu1->name, stu2->name)
>0;
}
BOOL sortWithAge(Student *stu1,Student *stu2) {
return stu1->age < stu2->age;
}
void bubbleSort(Student * str,int count,pFun
p) {
for (int i =0; i < count -1; i++)
{
for (int j =0; j < count - i -1;
j++) {
if (p(&str[j],&str[j +1])) {
Student temp = str[j]; //结构体名代表的是结构体整体,可直接赋值
str[j] = str[j +1]; //而数组名代表的是首地址,所以不能
str[j +1] = temp;
}
}
}
}
void printStudent(Student * str,int count) {
for (int i =0; i < count; i++) {
printf("%-10s,%-3d,%-5.2f",str[i].name,str[i].age,str[i].score);
printf("\n");
}
}
int main(int argc,constchar * argv[])
{
Student stu[5] = {{"wangxiao",23,79},{"yanyuan",21,89},{"liujian",19,95},{"daomeicui",24,59.9},{"haha",20,69}};
// bubbleSort(stu, 5,sortWithName); //按姓名排序
// bubbleSort(stu, 5, sortWithAge); //按年龄
bubbleSort(stu,5,sortWithScore); //按成绩
printStudent(stu,5);
return0;
}
//2. 函数指针数组(回调函数)
#include <stdio.h>
typedef struct student {
char name[20]; //注意类型不可为
char * name 否则为常量
即不可改变
int age;
float score;
}Student;
//重定义函数指针,新类型名为pCompair
typedefBOOL(*pCompair)(Student *,Student
*);
typedefstruct functionPointer {
char sortWayName[50];
pCompair pointer; //按什么方式排序的指针
}fPointer;
BOOL sortWithScore(Student *stu1,Student *stu2) {
return stu1->score < stu2->score;
}
BOOL sortWithName(Student *stu1,Student *stu2) {
returnstrcmp(stu1->name, stu2->name)
>0;
}
BOOL sortWithAge(Student *stu1,Student *stu2) {
return stu1->age < stu2->age;
}
//获得排序方式的函数指针
pCompair getFunction(fPointer * str,int count,char
* arr) {
//遍历数组,判断用哪个函数作为返回值
for (int i =0; i < count; i++) {
if (strcmp(str[i].sortWayName, arr) ==0)
{
return str[i].pointer;
}
}
return
NULL;
}
void bubbleSort(Student * str,fPointer *fpointer,int
count,char * arr) {
pCompair p =getFunction(fpointer,3, arr);
for (int i =0; i < count -1; i++)
{
for (int j =0; j < count - i -1;
j++) {
if (p(&str[j],&str[j +1])) {
Student temp = str[j]; //结构体名代表的是结构体整体,可直接赋值
str[j] = str[j +1]; //而数组名代表的是首地址,所以不能
str[j +1] = temp;
}
}
}
}
//输出结构体
void printStudent(Student * str,int count) {
for (int i =0; i < count; i++) {
printf("%-10s,%-3d,%-5.2f",str[i].name,str[i].age,str[i].score);
printf("\n");
}
}
int main(int argc,constchar * argv[])
{
fPointer pointer[3] = {{"姓名排序",sortWithName},{"年龄排序",sortWithAge},{"成绩排序",sortWithScore}};
Student stu[5] = {{"wangxiao",23,79},{"yanyuan",21,89},{"liujian",19,95},{"daomeicui",24,59.9},{"haha",20,69}};
bubbleSort(stu,pointer,5,"成绩排序");
printStudent(stu,5);
return0;
}
//学生结构体数组,按成绩排序、按年龄排序,按名字排序...如何实现?
//1. 动态排序
typedef struct student {
char name[20]; //注意类型不可为
char * name 否则为常量 即不可改变
int age;
float score;
}Student;
typedefBOOL(*pFun)(Student *,Student
*);
BOOL sortWithScore(Student *stu1,Student *stu2) {
return stu1->score < stu2->score;
}
BOOL sortWithName(Student *stu1,Student *stu2) {
returnstrcmp(stu1->name, stu2->name)
>0;
}
BOOL sortWithAge(Student *stu1,Student *stu2) {
return stu1->age < stu2->age;
}
void bubbleSort(Student * str,int count,pFun
p) {
for (int i =0; i < count -1; i++)
{
for (int j =0; j < count - i -1;
j++) {
if (p(&str[j],&str[j +1])) {
Student temp = str[j]; //结构体名代表的是结构体整体,可直接赋值
str[j] = str[j +1]; //而数组名代表的是首地址,所以不能
str[j +1] = temp;
}
}
}
}
void printStudent(Student * str,int count) {
for (int i =0; i < count; i++) {
printf("%-10s,%-3d,%-5.2f",str[i].name,str[i].age,str[i].score);
printf("\n");
}
}
int main(int argc,constchar * argv[])
{
Student stu[5] = {{"wangxiao",23,79},{"yanyuan",21,89},{"liujian",19,95},{"daomeicui",24,59.9},{"haha",20,69}};
// bubbleSort(stu, 5,sortWithName); //按姓名排序
// bubbleSort(stu, 5, sortWithAge); //按年龄
bubbleSort(stu,5,sortWithScore); //按成绩
printStudent(stu,5);
return0;
}
//2. 函数指针数组(回调函数)
#include <stdio.h>
typedef struct student {
char name[20]; //注意类型不可为
char * name 否则为常量
即不可改变
int age;
float score;
}Student;
//重定义函数指针,新类型名为pCompair
typedefBOOL(*pCompair)(Student *,Student
*);
typedefstruct functionPointer {
char sortWayName[50];
pCompair pointer; //按什么方式排序的指针
}fPointer;
BOOL sortWithScore(Student *stu1,Student *stu2) {
return stu1->score < stu2->score;
}
BOOL sortWithName(Student *stu1,Student *stu2) {
returnstrcmp(stu1->name, stu2->name)
>0;
}
BOOL sortWithAge(Student *stu1,Student *stu2) {
return stu1->age < stu2->age;
}
//获得排序方式的函数指针
pCompair getFunction(fPointer * str,int count,char
* arr) {
//遍历数组,判断用哪个函数作为返回值
for (int i =0; i < count; i++) {
if (strcmp(str[i].sortWayName, arr) ==0)
{
return str[i].pointer;
}
}
return
NULL;
}
void bubbleSort(Student * str,fPointer *fpointer,int
count,char * arr) {
pCompair p =getFunction(fpointer,3, arr);
for (int i =0; i < count -1; i++)
{
for (int j =0; j < count - i -1;
j++) {
if (p(&str[j],&str[j +1])) {
Student temp = str[j]; //结构体名代表的是结构体整体,可直接赋值
str[j] = str[j +1]; //而数组名代表的是首地址,所以不能
str[j +1] = temp;
}
}
}
}
//输出结构体
void printStudent(Student * str,int count) {
for (int i =0; i < count; i++) {
printf("%-10s,%-3d,%-5.2f",str[i].name,str[i].age,str[i].score);
printf("\n");
}
}
int main(int argc,constchar * argv[])
{
fPointer pointer[3] = {{"姓名排序",sortWithName},{"年龄排序",sortWithAge},{"成绩排序",sortWithScore}};
Student stu[5] = {{"wangxiao",23,79},{"yanyuan",21,89},{"liujian",19,95},{"daomeicui",24,59.9},{"haha",20,69}};
bubbleSort(stu,pointer,5,"成绩排序");
printStudent(stu,5);
return0;
}
相关文章推荐
- C语言结构体+数组实现学生学号成绩的统计、排序
- C/C++动态自定义结构体数组实例锻炼-学生成绩排序
- 结构体指针数组完成输入学生姓名学号数学语文成绩修改排序显示等功能
- 结构体数组排序 (学生信息 按成绩排序 )
- 有5名学生保存在结构体数组中,编程按学生的成绩升序排序,按学生的姓名降序排序,按年龄从低到高排序
- 在一个长度为10的整型数组里面,保存了班级10个学生的考试成绩。要求编写5个函数,分别实现计算考试的总分,最高分,最低分,平均分和考试成绩降序排序
- 有 4000 5名学生保存在结构体数组中,编程按学生的成绩升序排序,按学生的姓名降序排序,按年龄从低到高排序
- C语言利用结构体数组实现学生成绩管理系统
- //有5名学生保存在结构体数组中,编程查找成绩最高者,输出该学生全部信息。 // //对上述5名学生数组,按成绩从高到低排序,并输出。
- 数组实现学生成绩录入、求最高分、最低分、平均分和排序
- 有5名学生保存在结构体数组中,编程按学生的成绩升序排序,按学生的姓名降序排序,按年龄从低到高排序, 成绩, 年龄
- C语言,有5名学生保存在结构体数组中,编程按学生的成绩升序排序,按学生的姓名降序排序,按年龄从低到高排序
- 有5名学生保存在结构体数组中,编程按学生的成绩升序排序,按学生的姓名降序排序,按年龄从低到高排序
- 结构体数组经典例题--学生成绩的输入,总分及平均分----编程随笔(3)
- 汇编语言:在以GRADE为首地址的数组中存放着某班某门课30个学生的成绩。试编写一完整汇编语言程序实现:统计小于60分,60~89分和大于等于90分的学生人数,统计结果分别存放在LOWNUM、MIDD
- Linux-C基础知识学习:C语言作业-将5个学生成绩保存在一个数组中,单独实现一个计算平均成绩的average函数, 在main函数中获取该函数返回的平均值,并打印。
- java新手如何简单的理解java数组中冒泡排序法是怎么实现排序的
- 用C语言关于学生管理系统的几种实现方法(一位数组,二维数组,指针,结构体)
- 结构体案例之学生成绩输入及排序
- 参考C语言的qsort函数实现一个一个能对任意数据类型(包括结构体)的数组进行排序的函数(里面用的不是快速排序)