#import
//1.有5名学⽣生保存在结构体数组中,编程查找
//成绩最⾼高者,输出该学⽣生全部信息
//对上述5名学⽣生数组,按成绩从⾼高到低排序, 并输出
//1.定义结构体类型,并起一个别名
typedef struct{
float score; //成绩
char name[20];//姓名
}Student;
//4.1数组排序函数
void bubblesort(Student stu[], int count);
void bubblesort(Student stu[], int count)
{
// sizeof(stu) /sizeof(Stndent);//数组作为参数的时候,传递色是首地址(指针),所以,不能用这种方式获取数组长度
for (int i = 0; i < count - 1; i++) {
for(int j = 0; j < count - 1 -i; j++) {
if (stu[j].score < stu[j+1].score) {
Student temp = stu[j];
stu[j] = stu[j+1];
stu[j+1] = temp;
}
}
}
}
//5.1创建打印结果函数
void printStus(Student stu[],int count);
void printStus(Student stu[],int count)
{
for (int i = 0; i < count; i++) {
printf("score= %.2f name = %sn", stu[i].score, stu[i].name);
}
}
int main(int argc, const char * argv[])
{
//2.创建出来5个学生变量
Student stu1 = {89.23, "laoda"};
Student stu2 = {32.21, "laoer"};
Student stu3 = {83.23, "laosan"};
Student stu4 = {59.43, "laosi"};
Student stu5 = {88.88, "laowu"};
//3.把5名学生长进数组中
Student stus[] = {stu1, stu2, stu3, stu4,stu5};
//4.调用函数进行排序
bubblesort(stus, (sizeof(stus) / sizeof(Student)));
//5.打印结果
printStus(stus, (sizeof(stus) / sizeof(Student)));
//6.找出成绩最高的人
printf("maxScore = %.2f name = %sn", stus[0].score,stus[0].name);
return 0;
}