20251期《C语言程序设计B》专项训练04-结构体01部分-程序P801.C

16 阅读4分钟

题面

请根据程序中的要求完善程序——注意,**除指定位置外,不能对程序中已有部分作任何修改或重新编写一个程序,否则作0分处理**。  
  程序的功能是:有五个学生,每个学生的数据包括学号、姓名(最长19字节)、三门课的成绩,从键盘输入五个学生的数据,并计算每个学生的平均成绩,最后显示最高平均分的学生的信息(包括学号,姓名,三门课的成绩,平均分数)。  
  程序的运行效果应类似地如图1所示,图1中的:  
    2001 wanghai    78   83.5 90  
    2003 zhangzhong 82   80   98.5  
    2005 liumei     95.2 81   90  
    2006 sunqian    70   77.2 90  
    2009 jiansansa  63   87   90.3  
  是从键盘输入的内容。


Please input students  info:Num Name score1 score2 score3
1:2001 wanghai    78   83.5 90
2:2003 zhangzhong 82   80   98.5
3:2005 liumei     95.2 81   90
4:2006 sunqian    70   77.2 90
5:2009 jiansansa  63   87   90.3

The Highest is liumei(2005)
score1=95.20  score2=81.00  score3=90.00  aver=88.73

原码

#include <stdio.h>  
  
struct student  
{  
    int  num;  
    char name[20];  
    float score1, score2, score3;  
    float aver;  
};  
  
void Input(struct student *pStu, int n);  
int Highest(struct student *pStu, int n);  
  
int main(void)  
{  
    int high;  /* high记录平均分最高的学生的序号,具体使用参考后面的代码 */  
    struct student myClass[5];  
  
    /* 本部分代码功能建议:调用相应的函数完成数据输入和统计 */  
    /* User Code Begin(Limit: lines<=2, lineLen<=50, 考生可在本行后添加代码、最多2行、行长<=50字符) */  
  
  
    /* User Code End(考生添加代码结束。注意:空行和单独为一行的{与}均不计行数、行长不计行首tab缩进) */  
  
    printf("\nThe Highest is %s(%d)\nscore1=%.2f  score2=%.2f  score3=%.2f  aver=%.2f\n",  
        myClass[high].name, myClass[high].num,  
        myClass[high].score1, myClass[high].score2, myClass[high].score3, myClass[high].aver);  
  
    return 0;  
}  
  
/* 输入N个学生的信息并计算平均分 */  
void Input(struct student *pStu, int n)  
{  
    int i;  
    struct student tmpStu;  
  
    printf("Please input students  info:Num Name score1 score2 score3\n");  
    for (i=0; i<n; i++)  
    {  
        printf("%d:", i+1);  
        scanf("%d%s%f%f%f", &tmpStu.num, tmpStu.name,  
            &tmpStu.score1, &tmpStu.score2, &tmpStu.score3);  
        pStu[i] = tmpStu;  
        pStu[i].aver = (pStu[i].score1 + pStu[i].score2 + pStu[i].score3) / 3.0f;  
    }  
}  
  
/* 找出并通过函数值返回最高平均分学生的序号 */  
int Highest(struct student *pStu, int n)  
{  
    int i, pos = 0;  
  
    /* User Code Begin(考生可在本行后添加代码,行数不限) */  
      
  
      
    /* User Code End(考生添加代码结束) */  
  
    return pos;  
}

改后

#include <stdio.h>  
  
struct student  
{  
    int  num;  
    char name[20];  
    float score1, score2, score3;  
    float aver;  
};  
  
void Input(struct student *pStu, int n);  
int Highest(struct student *pStu, int n);  
  
int main(void)  
{  
    int high;  /* high记录平均分最高的学生的序号,具体使用参考后面的代码 */  
    struct student myClass[5];  
  
    /* 本部分代码功能建议:调用相应的函数完成数据输入和统计 */  
    /* User Code Begin(Limit: lines<=2, lineLen<=50, 考生可在本行后添加代码、最多2行、行长<=50字符) */  
    Input(myClass, 5);  
    high = Highest(myClass, 5);  
    /* User Code End(考生添加代码结束。注意:空行和单独为一行的{与}均不计行数、行长不计行首tab缩进) */  
  
    printf("\nThe Highest is %s(%d)\nscore1=%.2f  score2=%.2f  score3=%.2f  aver=%.2f\n",  
        myClass[high].name, myClass[high].num,  
        myClass[high].score1, myClass[high].score2, myClass[high].score3, myClass[high].aver);  
  
    return 0;  
}  
  
/* 输入N个学生的信息并计算平均分 */  
void Input(struct student *pStu, int n)  
{  
    int i;  
    struct student tmpStu;  
  
    printf("Please input students  info:Num Name score1 score2 score3\n");  
    for (i=0; i<n; i++)  
    {  
        printf("%d:", i+1);  
        scanf("%d%s%f%f%f", &tmpStu.num, tmpStu.name,  
            &tmpStu.score1, &tmpStu.score2, &tmpStu.score3);  
        pStu[i] = tmpStu;  
        pStu[i].aver = (pStu[i].score1 + pStu[i].score2 + pStu[i].score3) / 3.0f;  
    }  
}  
  
/* 找出并通过函数值返回最高平均分学生的序号 */  
int Highest(struct student *pStu, int n)  
{  
    int i, pos = 0;  
  
    /* User Code Begin(考生可在本行后添加代码,行数不限) */  
    struct student highAver=pStu[0];  
    pos = 0;  
    for(i=1;i<n;i++)  
    {  
        if(highAver.aver<pStu[i].aver)  
        {  
            highAver = pStu[i];  
            pos = i;  
        }  
    }      
    /* User Code End(考生添加代码结束) */  
  
    return pos;  
}

知识点

  • 结构体数组作为函数参数
  • 输入数据时注意格式
  • 打擂算法,记录位置
    • 比较的是均分,记录的是下标