结构体编程

【问题描述】C语言进行了5次阶段性测试,老师需要统计3位同学的总分,请你用结构体数组或结构体指针的知识帮忙补全代码,求出每一位同学的总分,然后输出。

输入:

1001 5 5 4 5 5

1002 4 4 4 4 4

1003 5 3 3 5 3

输出:

1001 24

1002 20

1003 19

#include  <stdio.h>
struct    STUDENT
{  
        int  id;                    //学号  
        int  score[5];          //5次成绩  
        int  total;                //总分  
};
struct    STUDENT    student[3],*pstu;
int  main()
{
        int  i;
        for(pstu=student;  pstu<student+3;  pstu++)
        {
                scanf("%d",&pstu->id);
                pstu->total=0;
                  //接下来输入每位同学的5次测验成绩,并求每一位同学5次测验的总和
        
        i = 0;
        while (i < 5)
        {
            scanf("%d", &pstu->score[i]);
            i++;
        }

        }
        for(pstu=student;  pstu<student+3;  pstu++)
        {
                printf("%d\t",pstu->id);                  
        //输出每一位同学的总分
        
        printf("%d", (pstu->score[0] + pstu->score[1] + pstu->score[2] + pstu->score[3] + pstu->score[4]));


        printf("\n");
        }
        return  0;
}

版权声明:本文为liourenyu原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。