栀九1234
2021-09-05 09:23
采纳率: 100%
浏览 41

编写3个函数实现要求(C语言)

img

  • 收藏

2条回答 默认 最新

  • joel_1993 2021-09-05 11:34
    已采纳

    你好,大致程序是这样的

    #include <stdio.h>
    struct student{
        char id[100];
        float math;
        float physics;
        float chemisty;
        float chinese;
        float gymnastics;
    };
    // 计算数学分平均 
    float math_average(struct student s[], int n){
        int i;
        float ave;
        ave = 0;
        for(i=0; i<n; i++) ave+=s[i].math;
        ave = ave/n;
        return ave;
    }
    
    void fail(struct student s[], int n){
        int count, i;
        float ave;
        for(i=0; i<n; i++){
            count = 0;
            if(s[i].math<60)count++;
            if(s[i].physics<60)count++;
            if(s[i].chemisty<60)count++;
            if(s[i].chinese<60)count++;
            if(s[i].gymnastics<60)count++;
            if(count>=2){
                ave = (s[i].math + s[i].physics + s[i].chemisty + s[i].chinese + s[i].gymnastics)/5;
                printf("%s\n",s[i].id);
                printf("math\tphysics\tchemisty\tchinese\tgymnastics\taverage\n");
                printf("%.2f\t%.2f\t%.2f\t\t%.2f\t%.2f\t\t%.2f\n", s[i].math, s[i].physics, s[i].chemisty,s[i].chinese, s[i].gymnastics,ave);
            }
        }
    }
    
    void success(struct student s[], int n){
        int count, i;
        float ave;
        for(i=0; i<n; i++){
            count = 0;
            ave = (s[i].math + s[i].physics + s[i].chemisty + s[i].chinese + s[i].gymnastics)/5;
            if(s[i].math>=80)count++;
            if(s[i].physics>=80)count++;
            if(s[i].chemisty>=80)count++;
            if(s[i].chinese>=80)count++;
            if(s[i].gymnastics>=80)count++;
            if(count==5||ave>=85){
                printf("%s\n",s[i].id);
                printf("math\tphysics\tchemisty\tchinese\tgymnastics\taverage\n");
                printf("%.2f\t%.2f\t%.2f\t\t%.2f\t%.2f\t\t%.2f\n", s[i].math, s[i].physics, s[i].chemisty,s[i].chinese, s[i].gymnastics,ave);
            }
        }
    }
    
    
    int main(){
        float a;
        struct student s[10]= {
        {"2021000001",56,78,89,90,45},
        {"2021000002",56,78,89,90,45},
        {"2021000003",56,78,89,90,45},
        {"2021000004",56,78,89,90,45},
        {"2021000005",56,78,89,90,45},
        {"2021000006",56,78,89,90,45},
        {"2021000007",80,81,89,90,85},
        {"2021000008",56,78,89,90,45},
        {"2021000009",59,95,95,90,100},
        {"2021000010",56,78,89,90,45}};
        printf("average math grade:%.2f\n",math_average(s,10));
        printf("\nFail的学生:\n");
        fail(s, 10);
        printf("\nSuccess的学生:\n");
        success(s, 10);
        return 0;
    }
    
     
    

    运行结果截图

    img

    1 打赏 评论
  • 赵4老师 2021-09-05 10:08

    百度搜,应该有现成的例子代码。

    打赏 评论

相关推荐 更多相似问题