A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© lyricdon 中级黑马   /  2015-6-2 20:50  /  911 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

这是我今天的编程题作业第一题,大家批评指点一下


/*
2.某班有5个学生,三门课。分别编写3个函数实现以下要求:
(1) 求各门课的平均分;
(2) 找出有两门以上不及格的学生,并输出其学号和不及格课程的成绩;
(3) 找出三门课平均成绩在85-90分的学生,并输出其学号和姓名
*/

#include <stdio.h>

typedef  struct Student{
    char name[20];
    int scoreOne;
    int scoreTwo;
    int scoreThr;
    int num;
}Stu;

/**
*  @author pC, 15-06-02 19:06:32
*
*  求各门课的平均分
*
*  @param st  学生结构体数组
*  @param len 数组成员数
*  @param avg 平均值指针
*/
void text(Stu *st,int len,float avg[])
{
    int sum[3] = {0};
    for (int i = 0; i < len; i++) {
        sum[0] += (st+i) -> scoreOne;
        sum[1] += (st+i) -> scoreTwo;
        sum[2] += (st+i) -> scoreThr;
    }
    avg[0] = sum[0]/len;
    avg[1] = sum[1]/len;
    avg[2] = sum[2]/len;
}

/**
*  @author pC, 15-06-02 18:06:48
*
*  找出有两门以上不及格的学生,并输出其学号和不及格课程的成绩;
*
*  @param st  结构体数组
*  @param len 数组成员数
*/
void text2(Stu *st,int len)
{
    printf("--------------------\n");
    printf("两门以上不及格的学生有:\n");
   
    for (int i = 0; i<len; i++) {
        // 定义判断标记
        int flag = -2,score1 ,score2,score3;
        score1 = score2 = score3 = 0;
        
        if ( (st+i) -> scoreOne < 60) {
            flag ++;
            score1 = 1;
        }
        if ( (st+i) -> scoreTwo < 60) {
            flag ++;
            score2 = 1;
        }
        if ( (st+i) -> scoreThr < 60)  {
            flag ++;
            score3 = 1;
        }
        
        while ( flag >= 0) {
            printf("学号:%d\n",(st+i) -> num);
            while ( score1 ) {
                printf("scoreOne:%d\n",(st+i) -> scoreOne);
                break;
            }
            while ( score2 ) {
                printf("scoreTwo:%d\n",(st+i) -> scoreTwo);
                break;
            }
            while ( score3 ) {
                printf("scoreThr:%d\n",(st+i) -> scoreThr);
                break;
            }
            break;
        }
    }
}


/**
*  @author pC, 15-06-02 18:06:48
*
*  找出三门课平均成绩在85-90分的学生,并输出其学号和姓名
*
*  @param st  结构体数组
*  @param len 数组成员数
*/
void text3(Stu *st,int len)
{
    printf("--------------------\n");
    printf("三门课平均成绩在85-90分的学生有:\n");
    int avg = 0;
    for (int i = 0; i<len; i++) {
        avg = ( (st+i) -> scoreOne + (st+i) -> scoreThr + (st+i) -> scoreTwo  )/3;
        if (avg >= 85 && avg <= 90) {
            printf("%d--%s\n",(st+i) ->num,(st+i) ->name);
        }
    }
   
}


int main()
{
    int len;
   
    Stu stu[] ={
        { "Jim",85,97,83,1},
        { "Jack",85,87,87,2},
        { "Tom",59,79,66,3},
        { "Lily",55,97,57,4},
        { "Amy",43,69,58,5}
    };
   
    len = sizeof(stu)/sizeof(Stu);
   
    float avg[3] = {0};
   
    text(stu, len, avg);
    printf("scoreOne:%.2f \nscoreTwo:%.2f \nscoreThr:%.2f\n",avg[0],avg[1],avg[2]);
   
    text2(stu, len);
    text3(stu, len);
   
   
   
    return 0;
}

评分

参与人数 1黑马币 +4 收起 理由
李闯 + 4 pc你能不打击我么

查看全部评分

4 个回复

倒序浏览
66666666666666666还么有写呢
回复 使用道具 举报
pc 略叼了
回复 使用道具 举报
受教了啊
回复 使用道具 举报
.........大神呀 ,为啥不直接去就业办,非得来基础班
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马