免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1559 | 回复: 3
打印 上一主题 下一主题

统计函数哪里错了? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-05-21 16:27 |只看该作者 |倒序浏览
#include<stdio.h>
#include <string.h>
struct student        /*定义一个学生信息结构体*/
  {
  int num;
  char name[10];
  char sex;
  int age;
  float score[5];
  };
struct student std1[10];  /*定义两个包含10个学生的以上结构的结构体数组std1[10]和std2[10]*/
struct student std2[10];

void input_data()    /*实现将10个学生的数据信息读入到数组std1[10]中。(input_data( )函数)*/
{
        int i;
        printf("please enter the message of ten students\n";
        printf("num  name  sex  age  computer  programmer  maths  English  PE\n";  
        for(i=0;i<10;i++)
     scanf("%d %s %c %d %f %f %f %f %f",&std1.num,std1.name,&std1.sex,&std1.age,
     &std1.score[0],&std1.score[1],&std1.score[2],&std1.score[3],&std1.score[4]);
}
void output_data()  /*实现将10个学生的数据信息输出到屏幕中。(output_data( )函数)*/
{
  int i;
  FILE *fp;
  printf(" num  name       sex  age computer programmer maths   English      PE\n";
  fp=fopen("std.dat","rb";
  for(i=0;i<10;i++)
  {  fread(&std1,sizeof(struct student),1,fp);
     printf("%5d %-10s %-3c %4d %4f %4f %4f %4f %4f\n",std1.num,std1.name,std1.sex,
     std1.age,std1.score[0],std1.score[1],std1.score[2],std1.score[3],std1.score[4]);
  }
    fclose(fp);
}
void data_write()  /*将数组std1[10]中的数据写入一个名为std.dat的二进制文件中。(data_write( )函数)*/
{
        FILE *fp;
        int i;
        if((fp=fopen("std.dat","wb")==NULL)
      {printf("cannot open file\n";
      return;
      }
        for(i=0;i<10;i++)
     if(fwrite(&std1,sizeof(struct student),1,fp)!=1)
        printf("file write error\n";
    fclose(fp);
}
void data_read()     /*从文件std.dat中将数据读入到数组std2[10]中。(data_read( )函数)*/
{
        FILE *fp;
        int i;
        if((fp=fopen("std.dat","rb")==NULL)
        {printf("cannot open infile\n";
         return;}
        for(i=0;i<10;i++)
        fread(&std2,sizeof(struct student),1,fp);
        fclose(fp);
}

void average1() /*统计1*/
{
        float *p,aver1,sum1=0;
        int m;
        printf("enter the number of score:";
        scanf("%d",&m); /*输入课程号*/
         p=&std2[0].score[m];
          for(;p<=&std2[9].score[m];p=p+5)
           sum1=sum1+(*p);  /*统计某门课程的总分*/
           aver1=sum1/10;   /*统计某门课程的平均分*/
           printf("the sum and average of Score %d are:\n",m);
           printf("sum1=%f\n",sum1);
           printf("aver1=%f\n",aver1);
}
          
void average2()  /*统计2*/
{
        float *p,aver[10],sum2=0;
        int i;   
        for(i=0;i<10;i++)  /*统计每个学生的总分和平均分*/
         {
          for(p=&std2.score[0];p<=&std2.score[4];p++)
           sum2=sum2+(*p);  
           aver=sum2/5;
           printf("the sum and average of Student %d are:\n",i);
           printf("sum2=%f\n",sum2);
           printf("aver=%f\n",aver);
         }
}
          
          
main()       /*定义一个主函数,在主函数中调用以上四个函数,实现对学生数据信息的文件读写
              和数组数据的输入输出功能,以菜单方式实现功能选择。(main( )函数)*/
{
       
        char ch;
        printf("****menu****\n");
    printf("1.input_data();data_write()                                 (please enter '1')\n");
    printf("2.output_data()                                              (please enter '2')\n");
    printf("3.data_read()from file;void average1();void average2() stat. (please enter '4')\n");
    printf("5.exit(0)                                                    (please enter '5')\n");
    ch=getchar();
        switch(ch)
        {case'1':input_data();data_write();break;
         case'2'utput_data();break;
     case'3':data_read();average1();average2();break;
     case'5':exit(0);
        }
}

论坛徽章:
0
2 [报告]
发表于 2008-05-21 23:21 |只看该作者
说实话,你这样发贴估计没人能够帮你。

论坛徽章:
0
3 [报告]
发表于 2008-05-22 04:50 |只看该作者
CU论坛为什么不实现一个自动识别源代码的功能,应该不难吧。

论坛徽章:
0
4 [报告]
发表于 2008-05-22 10:14 |只看该作者

复制代码
包一下
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP