C语言编程题——结构体
设计程序,程序通过定义学生结构体数组,存储了若干名学生的学号、姓名和3门课的成绩。函数fun的功能是将存放学生数据的结构体数组,按照姓名的字典序(从小到大)排序。
#include <stdio.h>
#include <string.h>
struct student
{ long sno;
char name[10];
float score[3];
};
/****************fun函数******************/
void fun(struct student a[], int n)
{
int i,j,k;
struct student t;
for (i=0; i<n; i++)
{
k=i;
for (j=i+1; j<n; j++)
if (a[j].name[0] < a[k].name[0])
k=j;
t=a[k];a[k]=a[i];a[i]=t;
}
}
/****************fun函数******************/
void main()
{ struct student s[4]={{10001,"ZhangSan", 95, 80, 88},{10002,"LiSi", 85, 70, 78}, {10003,"CaoKai", 75, 60, 88}, {10004,"FangFang", 90, 82, 87}};
int i, j;
printf("\n\nThe original data :\n\n");
for (j=0; j<4; j++)
{ printf("\nNo: %ld Name: %-8s Scores: ",s[j].sno, s[j].name);
for (i=0; i<3; i++)
printf("%6.2f ", s[j].score[i]);
printf("\n");
}
fun(s, 4);
printf("\n\nThe data after sorting :\n\n");
for (j=0; j<4; j++)
{ printf("\nNo: %ld Name: %-8s Scores: ",s[j].sno, s[j].name);
for (i=0; i<3; i++)
printf("%6.2f ", s[j].score[i]);
printf("\n");
}
}
运行示例
关注我,C语言不迷路。扫码关注,任何问题直接聊天框提问即可
想了解我的C语言学习笔记等更多知识请关注微信公众号: