/*
某班有5个学生,三门课。分别编写3个函数实现以下要求:
(1) 求各门课的平均分;
(2) 找出有两门以上不及格的学生,并输出其学号和不及格课程的成绩;
(3) 找出三门课平均成绩在85-90分的学生,并输出其学号和姓名
*/
#include <stdio.h>
#define M 5 // 五个学生
#define N 3 // 三门功课
typedef struct course {
char courseName[20];
float score;
} Course;
typedef struct student {
int stuNo;
char *name;
Course course[N];
} Student;
void getAvg();
void getNoPass();
void getBests();
int main(int argc, const char * argv[]) {
// insert code here...
Student stu[M] = {
{201501,"张三",{{"Chinese",80},{"Math",85},{"English",90}}},
{201502,"李四",{{"Chinese",90},{"Math",80},{"English",70}}},
{201503,"王五",{{"Chinese",80},{"Math",59},{"English",58}}},
{201504,"赵六",{{"Chinese",99},{"Math",99},{"English",99}}},
{201505,"loser",{{"Chinese",59.9},{"Math",59.9},{"English",59.9}}},
};
getAvg(stu);
getNoPass(stu);
getBests(&stu);
printf("The end!\n");
return 0;
}
void getAvg(Student getStu[M]){
float sumCh= 0,sumMa= 0,sumEn= 0;
float arr[3] = {sumCh,sumMa,sumEn};
for (int i= 0; i< M; i++) {
for (int k= 0; k< N; k++) {
arr[k] += getStu[i].course[k].score;
}
}
printf("语文平均分%.1lf,数学平均分%.1lf,英语平均分%.1lf\n",arr[0]/5,arr[1]/5,arr[2]/5);
}
void getNoPass(Student getStu[M]){
printf("两门成绩以上不及的同学:");
for (int i= 0; i< M; i++) {
if ((60 > getStu[i].course[0].score && getStu[i].course[1].score<60)||(60 > getStu[i].course[0].score && getStu[i].course[2].score<60)||(60 > getStu[i].course[1].score && getStu[i].course[2].score<60) ) {
printf("学号:%d,",getStu[i].stuNo);
for (int k= 0; k< N; k++) {
if (60 > getStu[i].course[k].score) {
printf("%s,%.1lf;",getStu[i].course[k].courseName,getStu[i].course[k].score);
}
}
}
}
printf("\n");
}
void getBests(Student getStu[]){
float arr[M] = {0,0,0,0,0};
for (int i= 0; i< M; i++) {
for (int k= 0; k< N; k++) {
arr[i] += getStu[i].course[k].score;
}
}
printf("三门课平均成绩在85-90分的学生:");
for (int i= 0; i< M; i++) {
if (80 < arr[i]/3 && arr[i]/3 < 90) {
printf("学号:%d,姓名:%s;\n",getStu[i].stuNo,getStu[i].name);
}
}
}
|
|