![]() |
#2
rjsp2022-01-04 12:57
![]() #include <stdio.h> #include <stdlib.h> #include <string.h> struct student { char name[20]; float score; int age; int num; }; void student_print( const struct student stu[], size_t n ) { for( size_t i=0; i!=n; ++i ) printf( "%8s %6.2f %d %d\n", stu[i].name, stu[i].score, stu[i].age, stu[i].num ); } int student_sort_cmp1( const void* a, const void* b ) // 成绩由大到小 { const struct student* p = (const struct student*)a; const struct student* q = (const struct student*)b; if( p->score > q->score ) return -1; if( p->score < q->score ) return +1; return 0; } int student_sort_cmp2( const void* a, const void* b ) // 姓名首字母由A到Z,听不懂是什么意思 { const struct student* p = (const struct student*)a; const struct student* q = (const struct student*)b; return strcmp(p->name,q->name); } int student_sort_cmp3( const void* a, const void* b ) // 年龄由小到大 { const struct student* p = (const struct student*)a; const struct student* q = (const struct student*)b; if( p->age < q->age ) return -1; if( p->age > q->age ) return +1; return 0; } int student_sort_cmp4( const void* a, const void* b ) // 学号由小到大 { const struct student* p = (const struct student*)a; const struct student* q = (const struct student*)b; if( p->num < q->num ) return -1; if( p->num > q->num ) return +1; return 0; } void student_sort( struct student stu[], size_t n, int (*comp)(const void*,const void*) ) { qsort( stu, n, sizeof(*stu), comp ); } int main( void ) { struct student stu[] = { { "zhangsan", 65.0f, 18, 211301 } , { "qiqi", 89.5f, 17, 211302 } , { "wanger", 75.3f, 15, 211303 } , { "sazi", 93.7f, 19, 211304 } }; const size_t n = sizeof(stu)/sizeof(*stu); puts( "原始数据:" ); student_print( stu, n ); puts( "\n成绩由大到小:" ); qsort( stu, n, sizeof(*stu), student_sort_cmp1 ); student_print( stu, n ); puts( "\n姓名首字母由A到Z:" ); qsort( stu, n, sizeof(*stu), student_sort_cmp2 ); student_print( stu, n ); puts( "\n年龄由小到大:" ); qsort( stu, n, sizeof(*stu), student_sort_cmp3 ); student_print( stu, n ); puts( "\n学号由小到大:" ); qsort( stu, n, sizeof(*stu), student_sort_cmp4 ); student_print( stu, n ); } 输出 原始数据: zhangsan 65.00 18 211301 qiqi 89.50 17 211302 wanger 75.30 15 211303 sazi 93.70 19 211304 成绩由大到小: sazi 93.70 19 211304 qiqi 89.50 17 211302 wanger 75.30 15 211303 zhangsan 65.00 18 211301 姓名首字母由A到Z: qiqi 89.50 17 211302 sazi 93.70 19 211304 wanger 75.30 15 211303 zhangsan 65.00 18 211301 年龄由小到大: wanger 75.30 15 211303 qiqi 89.50 17 211302 zhangsan 65.00 18 211301 sazi 93.70 19 211304 学号由小到大: zhangsan 65.00 18 211301 qiqi 89.50 17 211302 wanger 75.30 15 211303 sazi 93.70 19 211304 |
这是题目:
用C语言代码实现对一份成绩单的排序(成绩单包过姓名,成绩,年龄,学号4个内容)要求能根据用户的需要(选择),提供4种排序模式:
1,成绩由大到小。
2,姓名首字母由A到Z
3,年龄由小到大
4,学号由小到大
备注:
1,在能力范围内使代码更加简洁,代码执行更快。(程序优化)
2,合理备注(可读性)
3,可能用到的知识:动态数组定义(课本8.8)网络资料https://blog.:
这是我写的代码:
#include <stdio.h>
#include <stdlib.h>
struct student
{
char name[20];
float score;
int age;
int num;
};
void mode1( struct student stu[],int n)//参考书上307页(比较模式一)
{ n=4;
struct student temp;//定义变量temp,排序时交换以便使用
int i,j,k;
for(i=0;i<n-1;i++)
{
k=i;
for(j=i+1;j<n;j++ )
if(stu[j].score>stu[k].score)//两两比较成绩,由高到低排列
k=j;
temp=stu[k];stu[k]=stu[i];stu[i]=temp;//进行交换比较
}
}
void mode2(struct student stu[],int n)//(比较模式二)
{ n=4;
struct student temp;//定义变量temp,排序时交换以便使用
int i,j,k;
for(i=0;i<n-1;i++)
{
k=i;
for(j=i+1;j<n;j++ )
if(stu[j].name<stu[k].name)//两两比较姓名首字母,由A到Z排列
k=j;
temp=stu[k];stu[k]=stu[i];stu[i]=temp;//进行交换比较
}
}
void mode3(struct student stu[],int n)//(比较模式三)
{ n=4;
struct student temp;//定义变量temp,排序时交换以便使用
int i,j,k;
for(i=0;i<n-1;i++)
{
k=i;
for(j=i+1;j<n;j++ )
if(stu[j].age<stu[k].age)//两两比较年龄,由小到大排列
k=j;
temp=stu[k];stu[k]=stu[i];stu[i]=temp;//进行交换比较
}
}
void mode4(struct student stu[],int n)//(比较模式四), int n=4;//定义变量n为学生人数4
{ n=4;
struct student temp;//定义变量temp,排序时交换以便使用
int i,j,k;
for(i=0;i<n-1;i++)
{
k=i;
for(j=i+1;j<n;j++ )
if(stu[j].num<stu[k].num)//两两比较学号数字大小,由小到大排列
k=j;
temp=stu[k];stu[k]=stu[i];stu[i]=temp;//进行交换比较
}
}
int main()
{
struct student stu[4]={{"zhangsan",65,18,211301},
{"qiqi",89.5,17,211302},
{"wanger",75.3,15,211303},
{"sazi",93.7,19,211304}};//定义结构体数组,我理解为填写学生信息
int m,i,n;
n=4;
scanf("%d",&m);
for(i=0;i<n;i++)
{printf("%8s %6.2f %d %dn",stu[i].name,stu[i].score,stu[i].age,stu[i].num);}
printf("n");
if(m==1)
mode1;
else if(m==2)
mode2;
else if(m==3)
mode3;
else if(m==4)
mode4;
for(i=0;i<n;i++)
{printf("%8s %6.2f %d %dn",stu[i].name,stu[i].score,stu[i].age,stu[i].num);}
return 0;
}