C语言综合问题:输入5个学生的姓名、学号和5门课成绩计算每个学生的总分和平均分并按从高到低排序

如题所述

定义一个结构体
struct student
{
char *name;//姓名
int num[10];//学号
int scor[5];//5门课成绩
int average;//平均成绩
};
用student定义5个变量,再操作应该就很简单了
温馨提示:内容为网友见解,仅供参考
第1个回答  2013-06-01
#include <stdio.h> typedef struct _student{ int id; char name[8]; float score1; float score2; float score3; float score4; float score5; float score_total; float score_avg;}student; int student_swap(student* s1, student* s2){ //Add your code here return 0;} float score_sum(student* s, int s_size){ float sum = 0.0; sum = sum + s->score1; sum = sum + s->score2; sum = sum + s->score3; sum = sum + s->score4; sum = sum + s->score5; return sum;} float score_avg(student* s, int s_size){ float sum = 0.0; sum = score_sum(s, s_size); return sum/s_size;
} int student_sort(student s[], int s_size){ for(int n1=0;n1<s_size;n1++) { for(int n2=n1+1;n2<s_size;n2++) { if(s[n1].score_total<s[n2].score_total) { student_swap(&s[n1], &s[n2]); } } } return 0;} int main(){ student s[5]; for(int n=0;n<5;n++) { printf("请输入学生学号:"); scanf("%d", &s[n].id); printf("请输入学生姓名:"); scanf("%d", &s[n].name); printf("请输入第一门课成绩:"); scanf("%d", &s[n].score1); printf("请输入第二门课成绩:"); scanf("%d", &s[n].score2); printf("请输入第三门课成绩:"); scanf("%d", &s[n].score3); printf("请输入第四门课成绩:"); scanf("%d", &s[n].score4); printf("请输入第五门课成绩:"); scanf("%d", &s[n].score5); s[n].score_total = score_sum(s, 5); s[n].score_avg = score_avg(s, 5); printf("总成绩=%f,平均成绩=%f\n", s[n].score_total, s[n].score_avg); } return 0;}
相似回答