文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>C程序习题-计算学生成绩[11.5]

C程序习题-计算学生成绩[11.5]

时间:2010-08-26  来源:chengxiaopeng

    有10个学生,每个学生的数据包括学号,姓名,三门课程的成绩,从键盘输入10个学生的数据,要求打印出来三门功课总平均成绩,以及最高分的学生的数据(包括学号,姓名,3门课程成绩,平均分数)。  

#include <stdio.h>
#define N 10
struct student
{
       int num;
       char name[10];
       float score[3];
};
void input(struct student *,int);
void print1(struct student);
void print(struct student *,int,float *);
int maxindex(float *,int);
int main(int argc, char *argv[])
{
    struct student stu[N],*p;
    int i;
    float average[N],*f;
    p = &stu;
    f = average;
    
    input(p,N);
    printf("the source result:\n");
    print(p,N,f);
    
    p = &stu;
    f = average;
    i = maxindex(f,N);
    printf("the max index %d,the details info is :\n",i);
    print1(stu[i]);
    system("pause");
    return 0;
}

void input(struct student *p,int n)
{
     int i;
     for (i = 0; i < n; i++,p++)
     {
        printf("please input %d student info.\n",i + 1);
        scanf("%d%s%f%f%f",&(p->num),&(p->name),&(p->score[0]),&(p->score[1]),&(p->score[2]));
     }
}

void print(struct student *p,int n,float *f)
{
     struct student *stu;
     float average = 0;
     int i;
     for (stu = p; stu < p + n; stu++)
     {
         average = (stu->score[0] + stu->score[1] + stu->score[2]) / 3.0;
         *f++ = average;
         printf("num = %d ,name = %s , score[1] = %.2f ,score[2] = %.2f ,score[3] = %.2f ,average = %.2f\n",
                     stu->num,stu->name,stu->score[0],stu->score[1],stu->score[2],average);
         
     }
}
void print1(struct student stu)
{
      float average = 0;
      average = (stu.score[0] + stu.score[1] + stu.score[2]) / 3.0;
      printf("num = %d ,name = %s , score[1] = %.2f ,score[2] = %.2f ,score[3] = %.2f ,average = %.2f\n",
                     stu.num,stu.name,stu.score[0],stu.score[1],stu.score[2],average);
}

int maxindex(float *p,int n)
{
    int i,result = 0;
    float sum = 0;
    for (i = 0; i < n; i++)
    {
        if (*(p + i) > sum)
        {
           sum = *(p + i);
           result = i;
        }
    }
    return result;
}


相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载