Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2539905
  • 博文数量: 308
  • 博客积分: 5547
  • 博客等级: 大校
  • 技术积分: 3782
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-24 09:47
个人简介

hello world.

文章分类

全部博文(308)

分类: C/C++

2010-08-26 17:07:14

    有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;
}


阅读(1000) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~