首页 新闻 赞助 找找看

调用函数2和3输出的文本出了一点小问题(截图),求指导

0
悬赏园豆:50 [已关闭问题] 关闭于 2014-03-18 11:30

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct student
{
 long num;
 char name[10];
 int eng;
 int math;
 int comp;
 char addr[20];
};
struct student stu[10],*p=stu;
int main()
{
//  void jf(struct student [],int n);
  void studentaver(struct student stu[],int n);
  void courseaver_max(struct student stu[],int n);

// jf(p,10);
  studentaver(p,10);
  courseaver_max(p,10);
  return 0;
}
/*#include<stdlib.h>
  void jf(struct student [],int n)
{
 int s[10],i;char c[50];
FILE *fp,*fpp;
 fp=fopen("D:\\ccc\\file1.txt","r");
 fpp=fopen("D:\\ccc\\file2.txt","w");
  if(fp==NULL||fpp==NULL)
  {
   printf("cannot open this file\n");
   exit(0);
  }
 printf("家访同学信息如下:\n");
 fgets(c,50,fp);
 for(i=0;i<10;i++)
 {
  fscanf(fp,"%ld%s%d%d%d%s",&stu[i].num,stu[i].name,&stu[i].eng,&stu[i].math,&stu[i].comp,stu[i].addr);
        s[i]=stu[i].eng+stu[i].math+stu[i].comp;
  if(s[i]<180)
  {
   printf("学号 姓名 英语 数学 计算机 住址\n");
   printf("%ld\t%s\t%d\t%d\t%d\t%s\n",stu[i].num,stu[i].name,stu[i].eng,stu[i].math,stu[i].comp,stu[i].addr);
 }
}
 fclose(fp);fclose(fpp);
}*/
#include<stdlib.h>
void studentaver(struct student stu[],int n)
{
 int stuaver;
 int i;
 char c[50];
 FILE *fp,*fpp;
 fp=fopen("D:\\ccc\\file1.txt","r");
 fpp=fopen("D:\\ccc\\file2.txt","w");
  if(fp==NULL||fpp==NULL)
 {
   printf("cannot open this file\n");
   exit(0);
  }
  fgets(c,50,fp);
 fprintf(fpp,"学号\t姓名\t\t平均分\n");
    for(i=0;i<10;i++)
 {fscanf(fp,"%ld%s%d%d%d%s",&stu[i].num,stu[i].name,&stu[i].eng,&stu[i].math,&stu[i].comp,stu[i].addr);
  stuaver=(int)((stu[i].eng+stu[i].math+stu[i].comp)/3);
  fprintf(fpp,"%ld\t%s\t\t%d\n",stu[i].num,stu[i].name,stuaver);
 }
fclose(fp);fclose(fpp);
}
#include<stdlib.h>
void courseaver_max(struct student stu[],int n)
{
 int max[3]={0},i;char c[50];
 float courseaver[3]={0.0};
 FILE *fp,*fpp;
 fp=fopen("D:\\ccc\\file1.txt","r");
 fpp=fopen("D:\\ccc\\file2.txt","a+");
  if(fp==NULL||fpp==NULL)
 {
   printf("cannot open this file\n");
   exit(0);
  }
  fgets(c,50,fp);
  fprintf(fpp,"课程\t平均分\t最高分\n");
    for(i=0;i<10;i++)
 {
 fscanf(fp,"%ld%s%d%d%d%s",&stu[i].num,stu[i].name,&stu[i].eng,&stu[i].math,&stu[i].comp,stu[i].addr);
  max[1]=max[1]>stu[i].eng?max[1]:stu[i].eng;
  max[2]=max[2]>stu[i].math?max[2]:stu[i].math;
  max[3]=max[3]>stu[i].comp?max[3]:stu[i].comp;
 }
 for(i=0;i<10;i++)
 {
  courseaver[1]+=stu[i].eng;
  courseaver[2]+=stu[i].math;
  courseaver[3]+=stu[i].comp;
 }
  courseaver[1]/=10;
  courseaver[2]/=10;
  courseaver[3]/=10;
 fprintf(fpp,"英语\t%.2f\t%d\n",courseaver[1],max[1]);
 fprintf(fpp,"数学\t%.2f\t%d\n",courseaver[2],max[2]);
 fprintf(fpp,"计算机\t%.2f\t%d\n",courseaver[3],max[3]);
 fclose(fp);fclose(fpp);
}

 

file1

mum     name       eng   math   comp     addr
95001   奥巴马     88    97     98      room201
95002   卡扎菲     67    55     62      room201
95003   sunh       87    87     86      room203
95004   zhaoht     55    66     45      room203
95005   zhangy     91    78     98      room203
95006   凤姐       77    90     91      room212
95007   芙蓉姐姐   94    92     90      room212
95008   liuq       89    92     93      room316
95009   songh      35    66     57      room317
95010   李刚       88    99     92      room319

 

 

zhengzhiqiang的主页 zhengzhiqiang | 初学一级 | 园豆:12
提问于:2013-01-03 15:24
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册