文件操作 课程成绩平均分统计并排名

//运行环境 visual studio 2008 

//c语言

代码如下

#include <stdio.h> 
#include <stdlib.h> 
 
typedef struct student 

   char    number[20]; 
    char    name[20]; 
      int score_1;
   int score_2;
      int score_3;
      int score_4;
      int score_5;
     int score_6;
     int score_7;
     int score_8;
  float score_9;
   struct  student *next; 
}student; 
 
 
void sort(student *s[],int sn); 
void average(student *s[],int sn);
 
int main() 

    FILE* fd; 
    int flag = 0; 
    int sn = 10;//读取前面十个学生的信息 
   student * s[10]; 
   for(;flag<sn;flag++) 
   { 
        s[flag] = (student *) malloc(sizeof(student)); 
   } 
 
    //打开文件 
   if((fd = fopen("d:\g.txt","r"))==NULL) 
  { 
        printf("文件读取失败.\n"); 
        return 0; 
 
    } 
  printf("文件读取成功.\n"); 
 
    //读取文件 
    for( flag = 0; flag < sn; flag++) 
    { 
       fscanf(fd,"%s%s%d%d%d%d%d%d%d%d",s[flag]->number,s[flag]->name,&s[flag]->score_1,
      &s[flag]->score_2,&s[flag]->score_3,&s[flag]->score_4,&s[flag]->score_5,&s[flag]->score_6,
                             &s[flag]->score_7,&s[flag]->score_8);
            s[flag]->score_9=(float(s[flag]->score_1+s[flag]->score_2+s[flag]->score_3+s[flag]->score_4+s[flag]->score_5+s[flag]->score_6+s[flag]->score_7+s[flag]->score_8)/sn);
      printf("%s%8s%6d%6d%6d%6d%6d%6d%6d%6d*%10f*\n",s[flag]->number,s[flag]->name,s[flag]->score_1, 
                                   s[flag]->score_2,s[flag]->score_3,s[flag]->score_4,s[flag]->score_5,s[flag]->score_6,
                             s[flag]->score_7,s[flag]->score_8,s[flag]->score_9); 
    } 
 
   //关闭文件 
    fclose(fd); 
 
    sort(s,sn);//按照分数高低输出所有学生信息 
 
 
 
   return 0; 
 

 
 

 
void sort(student *s[],int sn) 

   student * temp; 
   int i=0,j=0; 
  for( ;i<sn-1; i++) 
   { 
      for(j = i; j<sn; j++) 
      { 
          if(s[i]->score_9 < s[j]->score_9) 
            { 
               temp = s[i]; 
               s[i] = s[j]; 
               s[j] = temp; 
           } 
       } 

   } 
 
    printf("\n"); 
 
    //输出所有排序后学生的信息 
    for(i=0;i<sn;i++) 
    { 
       printf("%s%8s%6d%6d%6d%6d%6d%6d%6d%6d*%10f*\n",s[i]->number,s[i]->name,s[i]->score_1, 
                 s[i]->score_2,s[i]->score_3,s[i]->score_4,s[i]->score_5,s[i]->score_6,
                             s[i]->score_7,s[i]->score_8,s[i]->score_9
                                   ); 
    } 
 
  } 

// g.txt 文件举例

10903070101  张三丰    43 23 45 32 45 34 34 44
10903070102  郭靖      42 23 45 32 45 34 34 44
10903070103  黄蓉      41 23 45 32 45 34 34 44
10903070104  杨康      46 23 45 32 45 34 34 44
10903070105  令狐冲    47 23 45 32 45 34 34 44
10903070106  任盈盈    48 23 45 32 45 34 34 44
10903070107  洪七公    49 23 45 32 45 34 34 44
10903070108  欧阳锋    40 23 45 32 45 34 34 44
10903070109  阿紫      44 23 45 32 45 34 34 44
10903070110  段誉      47 23 45 32 45 34 34 44



  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值