初始化10名学生的基本信息,包括学号、姓名、三科(高数、英语、程序设计)成绩,分别计算每名学生的总分,并按个人总分对学生降序排序,输出排序后的学生信息
编写程序,初始化10名学生的基本信息,包括学号、姓名、三科(高数、英语、程序设计)成绩,分别计算每名学生的总分,并按个人总分对学生降序排序,输出排序后的学生信息。下面是我写的代码:
程序代码:
#include <stdio.h> struct courses { char cName[20]; double score; }; struct student /* 学生信息结构定义 */ { int num; /* 学号 */ char name[10]; /* 姓名 */ double sum; /* 平均分 */ struct courses sc[10]; }; int main() { int i=0, index, n, j; struct student students[10]={ {101,"xiao",{{1,"computer",89},{2,"math",78},{3,"english",76}}}, {102,"zhao",{{1,"computer",89},{2,"math",78},{3,"english",76}}}, {103,"qian",{{1,"computer",89},{2,"math",78},{3,"english",76}}}, {104,"sun",{{1,"computer",89},{2,"math",78},{3,"english",76}}}, {105,"li",{{1,"computer",89},{2,"math",78},{3,"english",76}}}, {106,"zhou",{{1,"computer",89},{2,"math",78},{3,"english",76}}}, {107,"wu",{{1,"computer",89},{2,"math",78},{3,"english",76}}}, {108,"zheng",{{1,"computer",89},{2,"math",78},{3,"english",76}}}, {109,"wang",{{1,"computer",89},{2,"math",78},{3,"english",76}}}, {110,"jiang",{{1,"computer",89},{2,"math",78},{3,"english",76}}} }, temp; /* 定义结构数据 */ for (; i < 10; i++) { printf("num:%d\n",students[i].num); printf("name:%s\n",students[i].name); students[i].sum +=students[i].sc[10].score; printf("sum:%lf\n",students[i].sum); } /* 结构数组排序,选择排序法 */ for (i = 0; i < n - 1; i++) { index = i; for (j = i + 1; j < n; j++) { if (students[j].sum > students[index].sum) /* 比较平均成绩 */ { index = j; } } temp = students[index]; /* 交换数组元素 */ students[index] = students[i]; students[i] = temp; } /* 输出排序后的信息 */ printf("num\tname\tsum\n"); for (i = 0; i < n; i++) { printf("%d\t%s\t%d\n", students[i].num, students[i].name, students[i].sum); } return 0; }
接下来就写不下去了,求和那里程序不能求和,然后下面的排序也对应 的出了问题。
求大佬帮我改正。