有5名学生保存在结构体数组中,编程查找成绩最高者,输出该学生的全部信息.

来源:互联网 发布:mac能玩qq游戏吗 编辑:程序博客网 时间:2024/05/24 15:41
<span style="font-family:Arial Black;font-size:14px;">main.m文件//    Stu  stu[5] = {//        {"summer", 'M', 98.0, 18},//        {"mht", 'M', 89.0, 19},//        {"mjm", 'F', 78.0, 20},//        {"xiaoyu", 'F', 101.0, 21},//        {"xiaoshi", 'M', 102.3, 30}//    };//    //1.有5名学生保存在结构体数组中,编程查找成绩最高者,输出该学生的全部信息.//    Stu maxScoreStudent = {0};//存储成绩最高者//    for (int i = 0; i < 5; i++) {//        if (maxScoreStudent.score < stu[i].score) {//            maxScoreStudent = stu[i];//        }//    }//    printfStudentInfo(maxScoreStudent);//    printf("\n排序之前学生信息为:\n");//    printAllStudentInfo(stu, 5);//    //2.对上述5名学生数组.按成绩从高到低排序,并输出//    for (int i = 0; i < 5 - 1; i++) {//        for (int j = 0; j < 5 - 1 - i; j++) {//            if (stu[j].score < stu[j + 1].score) {//                Stu temp = {0};//                temp = stu[j];//                stu[j] = stu[j + 1];//                stu[j + 1] = temp;//            }//        }//    }//    printf("按成绩从高到低排好序的学生信息:\n");//    printAllStudentInfo(stu, 5);//    printf("\n排序之前的学生信息:\n");//    printAllStudentInfo(stu, 5);//    //(1)按姓名从高到低排序//    for (int i = 0; i < 5 - 1; i++) {//        for (int j = 0; j < 5 - 1 - i; j++) {//            if (strcmp(stu[j].name, stu[j + 1].name) < 0) {//                Stu temp = {0};//                strcpy(temp.name, stu[j].name);//                strcpy(stu[j].name, stu[j + 1].name);//                strcpy(stu[j + 1].name, temp.name);//            }//        }//    }//    printf("按姓名从高到低排好序的学生信息:\n");//    printAllStudentInfo(stu, 5);//    printf("\n排序之前的学生信息为:\n");//    printAllStudentInfo(stu, 5);//    for (int i = 0; i < 5 - 1; i++) {//        for (int j = 0; j < 5 - 1 - i; j++) {//            Stu temp = {0};//            if (stu[j].age > stu[j + 1].age) {//                temp = stu[j];//                stu[j] = stu[j + 1];//                stu[j + 1] = temp;//            }//        }//    }//    printf("按年龄从低到高排好序的学生信息:\n");//    printAllStudentInfo(stu, 5);.h文件//定义学生结构体typedef struct student{    char name[20];    char sex;    float score;    int age;}Stu;//输出结构体的结构体成员void printfStudentInfo(Stu student);//输出全部同学信息void printAllStudentInfo(Stu stu[], int count);.m文件//输出结构体的结构体成员void printfStudentInfo(Stu student){    printf("Name:%s, Sex:%c, Score:%.2f, Age:%d\n", student.name, student.sex, student.score, student.age);}//输出全部同学信息void printAllStudentInfo(Stu stu[], int count){    for (int i = 0; i < count; i++) {        printfStudentInfo(stu[i]);    }}</span>

0 0
原创粉丝点击