开课水平小测(数据结构_修改版)

来源:互联网 发布:诲女知之乎诲的意思 编辑:程序博客网 时间:2024/06/16 06:57
/* 输入10个学生5门课的成绩,分别用函数实现下列功能: 1. 计算每个学生的平均分; 2. 计算每门课程的平均分; 3. 找出50个分数中的最高分。*/#include <stdio.h>#define N 10#define M 5#define Q 30//声明结构体类型struct STUDENT{    int studentID;    float score[M];};//定义结构体数组struct STUDENT stu[N];void Student_achievement_output(struct STUDENT *s);//输出成绩表函数原型void each_student_average(struct STUDENT *stu_score);//计算每个学生平均分函数原型void each_course_average(struct STUDENT *sco_score);//计算每门课程平均分函数原型void MAX(struct STUDENT *Max);//计算最高分函数原型int main(void){    printf("struct STUDENT stu[%d] Number of bytes :%d\n", N, Q*sizeof(struct STUDENT));    //计算结构体数组所占字节数    putchar('\n');    //初始化结构体数组    struct STUDENT stu[N] = {{123401, {91.27, 92.39, 93.48, 94.36, 95.97}},                                {123402, {90.97, 91.27, 92.39, 93.48, 94.36}},                                {123403, {99.36, 98.97, 97.27, 96.39, 95.48}},                                {123404, {91.48, 99.36, 92.36, 98.27, 93.39}},                                {123405, {90.39, 91.48, 92.36, 93.97, 94.27}},                                {123406, {99.11, 98.64, 97.93, 96.01, 95.55}},                                {123407, {90.55, 91.11, 92.64, 93.93, 94.01}},                                {123408, {99.01, 98.55, 97.11, 96.64, 95.93}},                                {123409, {90.93, 91.01, 92.55, 93.11, 94.64}},                                {123410, {99.64, 98.93, 97.01, 96.55, 95.11}}};    putchar('\n');    Student_achievement_output(stu);//调用输出成绩表函数    putchar('\n');    each_student_average(stu);//调用计算每个学生的平均分函数    putchar('\n');    each_course_average(stu);//调用计算每门课程的平均值函数    putchar('\n');    MAX(stu);//调用计算最高分函数    return 0;}void Student_achievement_output(struct STUDENT *s){    int i=0;    int j=0;    //输出成绩表    printf("studentID:\tscore_1: score_2: score_3: score_4: score_5:");    putchar('\n');    for(i=0; i<N; i++)    {        putchar('\n');        printf("   %d ", s[i].studentID);        putchar('\t');        for(j=0; j<M; j++)        {   printf("  %.1f  ", s[i].score[j]);  }        putchar('\n');    }}//计算每个学生的平均分函数定义void each_student_average(struct STUDENT *stu_score){    int i, j;    float sum = 0;    putchar('\n');    for(i=0; i<M; i++)    {        for(j=0; j<N; j++)        {   sum += stu_score[i].score[j];   }        printf("学号:%d, Average score: %.1f\n", stu_score[i].studentID, sum/5.0);        putchar('\n');    }}//计算每门课程的平均分函数定义void each_course_average(struct STUDENT *sco_score){    int i, j;    float sum = 0;    putchar('\n');    for(j=0; j<M; j++)    {        for(i=0; i<N; i++)        {   sum += sco_score[i].score[j];   }        printf("sccore_%d, Average value: %.1f\n", j+1, sum/10.0);        putchar('\n');    }}//计算最高分函数定义void MAX(struct STUDENT *Max){    int i, j, m1, m2;    float max = 0;    for(i=0; i<N; i++)    {        for(j=0; j<M; j++)        {            if(Max[i].score[j] > max)            {                max = Max[i].score[j];                m1 = i;                m2 = j+1;            }        }    }    putchar('\n');    printf("max_score: %.3f\n", max);    printf("studentID: %d\n", Max[m1].studentID);    printf("score_%d\n ", m2);    putchar('\n');}
0 0
原创粉丝点击