- /*
- 2.某班有5个学生,三门课。分别编写3个函数实现以下要求:
- (1) 求各门课的平均分;
- (2) 找出有两门以上不及格的学生,并输出其学号和不及格课程的成绩;
- (3) 找出三门课平均成绩在85-90分的学生,并输出其学号和姓名
- */
- #include <stdio.h>
- typedef struct {
- char name[20];//课程名称
- float score;//课程分数
- }Score;
- typedef struct {
- int number;//学号
- char name[20];//姓名
- Score courses[3];//三门课程
- }student;
- //求三门课的平均成绩
- void averageScore(student arr[] ,int arr_len ){
-
- for(int j = 0; j<arr_len;j++){
- float average = 0.0f;
- for (int i = 0; i < 3; i++) {
- average += arr[j].courses[i].score;
- }
- average /= 3;
- if (average >= 85 && average <= 90) {
- printf("学号:%d 姓名:%s 的平均分在 85-90之间\n",arr[j].number,arr[j].name);
- }
- }
-
- }
- //求每一门课总的平均成绩
- void averageEvery( student arr[],int arr_len ){
-
- for (int i = 0; i < 3; i++) {
- float averageEveryScore;
- float sum = 0.0f;
- for (int j = 0; j < arr_len; j++) {
- sum += arr[j].courses[i].score;
- }
- averageEveryScore = sum / arr_len;
- printf("%s 的平均分数是 %.2f\n",arr[i].courses[i].name,averageEveryScore);
- }
- }
- //至少两门低于60
- void printPoorStudent(student arr[],int arr_len){
-
- for (int i = 0; i < arr_len; i++) {
- int count = 0;
- for (int j = 0; j < 3; j++) {
- if (arr[i].courses[j].score < 60) {
- count++;
- }
- }
- if (count > 1) {
- printf("%s 的学号是: %d\n",arr[i].name,arr[i].number);
- for (int k = 0; k < 3; k++) {
- if (arr[i].courses[k].score < 60) {
- printf("%s : %.2f\n",arr[i].courses[k].name,arr[i].courses[k].score);
- }
- }
- }
- }
-
- }
- int main(int argc, const char * argv[]) {
-
- student person[5] = {
- {1,"liujiaping","yuwen",99,"shuxue",98,"yingyu",59},
- {2,"liuqiangsheng","yuwen",58,"shuxue",76,"yingyu",45},
- {3,"malibo","yuwen",67,"shuxue",67,"yingyu",90},
- {4,"duantaiyang","yuwen",65,"shuxue",56,"yingyu",45},
- {5,"zhouyu","yuwen",45,"shuxue",36,"yingyu",45},
- };
- averageEvery( person,5 );
-
- printf("------------------------\n");
- printPoorStudent(person,5);
- printf("------------------------\n");
- averageScore(person ,5 );
-
- return 0;
- }
复制代码
|
|