某班有5个学生,三门课。分别编写3个函数实现以下要求:
(1) 求各门课的平均分;
(2) 找出有两门以上不及格的学生,并输出其学号和不及格课程的成绩;
(3) 找出三门课平均成绩在85-90分的学生,并输出其学号和姓名
.h
struct course {
char name[60]; //课程名称
float score; //分数
};
//类型定义
typedef struct course Course;
struct Student {
char name[30]; //姓名
int number; //学号
Course courses[3];//课程
};
//类型定义
typedef struct Student Student;
//打印每门课的平均分
void averageScoreOfStudents(Student stus[],int count);
//void averageScoreOfStudents(Student *stus,int count);
//打印两门以上不及格学生信息
void failStudentsInfo(Student *stus,int count);
//打印平均分在85~90之间学生的学号和姓名
void averageBetween85And90StudentsInfo(Student *stus,int count);
.m
#import "Student.h"
void averageScoreOfStudents(Student stus[],int count)
{
for (int i = 0; i < 3; i++) {//控制课程
float sum = 0;
for (int j = 0; j < count; j++) {
sum += stus[j].courses[i].score;
}
printf("%s平均分:%.2f\n",stus[0].courses[i].name,sum/count);
}
}
void failStudentsInfo(Student *stus,int count)
{
for (int i = 0; i < count; i++) {
int count = 0;
for (int j = 0; j < 3; j++) {
if ((stus+i)->courses[j].score < 90){
count++;
}
}
if (count >= 2) {
printf("%s %d ",stus[i].name,stus[i].number);
for (int k = 0; k < 3; k++) {
if (stus[i].courses[k].score < 90) {
printf("%s:%.2f ",stus[i].courses[k].name,stus[i].courses[k].score);
}
}
printf("\n");
}
}
}
void averageBetween85And90StudentsInfo(Student *stus,int count)
{
for (int i= 0; i < 5; i++) {
float sum = 0;
for (int j = 0; j < 3; j++) {
sum += stus[i].courses[j].score;
}
if (sum/3 >= 85 && sum/3 <= 90){
printf("%d %s\n",stus[i].number,stus[i].name);
}
}
}
main
int main(int argc, const char * argv[])
{
Student stus[5] = {
{"zhangsan",5,{{"shuxue",78},{"yuwen",79},{"yingyu",110}}},
{"lisi",3,{{"shuxue",96},{"yuwen",23},{"yingyu",44}}},
{"wangwu",2,{{"shuxue",150},{"yuwen",90},{"yingyu",0}}},
{"zhaoda",4,{{"shuxue",90},{"yuwen",90},{"yingyu",90}}},
{"qianer",1,{{"shuxue",102},{"yuwen",125},{"yingyu",130}}}
};
//使用函数求平均分
averageScoreOfStudents(stus, 5);
//使用函数打印两门以上不及格学生信息
failStudentsInfo(stus, 5);
//打印成绩在85-90之间的学生
averageBetween85And90StudentsInfo(stus, 5);
return 0;
}