习题 8.15 有一个班4个学生,5门课程。1. 求第1门课程的平均分;2.找出有两门以上课程不及格的学生,输出他们的学号和全部课程成绩及平均成绩;3.找出平均成绩在90分以上或全部课程成绩在85分以

C程序设计(第四版) 谭浩强 习题8.15 个人设计

习题 8.15 有一个班4个学生,5门课程。1. 求第1门课程的平均分;2.找出有两门以上课程不及格的学生,输出他们的学号和全部课程成绩及平均成绩;3.找出平均成绩在90分以上或全部课程成绩在85分以上的学生。分别编3个函数实现以上3个要求。

代码块:

#include <stdio.h>
#include <stdlib.h>
void aver_fcourse(int *s[4], int n);           //定义函数1
void two_fail(int *s[4], int m, int n);        //定义函数2
void high_score(int *s[4], int m, int n);      //定义函数3
int main()
{
    int *stu_score[4], i, j;
    for (i=0; i<4; i++){
        stu_score[i]=(int *)malloc(3*sizeof(int));                   //给学生成绩分配动态空间
        printf("Please enter No.%d student score: ", i+1);           //输入学生成绩
        for (j=0; j<5; scanf("%d", *(stu_score+i)+j), j++);
    }
    aver_fcourse(stu_score, 4);                                      //调用函数1
    two_fail(stu_score, 4, 5);                                       //调用函数2
    high_score(stu_score, 4, 5);                                     //调用函数3
    return 0;
}
//函数1
void aver_fcourse(int *s[4], int n)
{
    int i;
    float sum;
    for (i=0, sum=0; i<n; sum+=*s[i++]);
    printf("The first course average score: %.2f\n", sum/n);
}
//函数2
void two_fail(int *s[4], int m, int n)
{
    int i, j, k, cc;
    float sum;
    for (i=0; i<m; i++){
        for (j=0, cc=0; j<n; *(*(s+i)+j)<60 ? cc++, j++ : j++);
        if (cc>=2){
            printf("No.%d student is fail.  Score: ", i+1);
            for (k=0, sum=0; k<n; printf("%d ", *(*(s+i)+k)), sum+=*(*(s+i)+k), k++);
            printf("\nAverage=%.2f\n", sum/n);
        }
    }
}
//函数3
void high_score(int *s[4], int m, int n)
{
    int i, j, cc;
    float sum, aver;
    for (i=0; i<m; i++){
        for (j=0, cc=0, sum=0; j<n; sum+=*(*(s+i)+j), *(*(s+i)+j)>85 ? cc++, j++ : j++);
        aver=sum/n;
        if (aver>90||cc==5)
            printf("No.%d student is high score.\n", i+1);
    }
}

猜你喜欢

转载自blog.csdn.net/navicheung/article/details/79421953