| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 519 人关注过本帖
标题:代码不多,大家帮忙看看fun()函数那里里应该怎么改,就是输不出正确的答案
只看楼主 加入收藏
saysaysay
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2008-6-18
收藏
 问题点数:0 回复次数:2 
代码不多,大家帮忙看看fun()函数那里里应该怎么改,就是输不出正确的答案
#include <stdio.h>
#define SIZE 10
struct student
{
int name[20];
char num;
float score1;                               /*高数成绩*/
float creh1;                                /*高数学分*/
float score2;                               /*英语成绩*/
float creh2;                                /*英语学分*/
float score3;                               /*物理成绩*/
float creh3;                                /*物理学分*/
}stu[SIZE];

/****************************************************************************/

void save()
{FILE *fp;
 int i;
 if ((fp=fopen(“C:\\c\\student.txt”,”rb”))==NULL)
 {printf(“can not open file\n”);
  exit(0);
 }
 for(i=0;i<SIZE;i++)
 if(fwrite(stu[i],sizeof(struct student),1,fp))!=1)
{printf(“file write error\n”);
  fclose(fp);
 }
}

/****************************************************************************/
void menu()                           /*主菜单界面*/
{
printf(“\t\t********************************************\n\n”);
printf(“\t\t********************************************\n\n”);
printf(“\t\t ***choose function****************************\n\n”);
printf(“\t\t ***1 search**********************************\n\n”);
ptintf(“\t\t ***2 backupdata******************************\n\n”);
ptintf(“\t\t ***3 exit************************************\n\n”);
printf(“\t\t********************************************\n\n”);
printf(“\t\t********************************************\n\n”);
}

/*******************************************************************/
fun()
{int i;
 FILE *fp,*fp1;
fp1= fopen(“C:\\c\\student.txt”,”rb”);                     /*打开student.txt文件*/
 if((fp1==NULL)
   {printf(“can not open file\n”);
    return;
   }
  for(i=0;i<SIZE;i++)
{if(fread(&stu[i],sizeof(struct student),1,fp))!=1)         /*从student.txt文件中读取数据*/
 {if (feof(fp1)){fclose(fp1);return;}
  printf(“file reas error\n”);
 }
}
for(i=0;i<SIZE;i++)
{if(stu[i].creh1==0 || stu[i].creh2==0||stu[i].creh3==0)         /*三科是否有一科不及格*/
 {printf(“%s was fail!\n”,stu[i].name);                     /*输出谁不及格*/
 }
else
{printf(“%s has pass!\n”stu[i].name);                     /*输出及格的人*/
if((fp=fopen(“C:\\c\\score.txt”,”wb”))==NULL)             /*打开score.txt文件*/
{printf(“can not open file\n”);
 return;
}
 if(fwrite(&stu[i],sizeof(struct student),1,fp)!=1)        /*向score.txt文件中输入数据*/
 printf(“file write error\n”);
}
}
fclose(fp)
}

/**********************************************************************/
backupdata()
{int i;
 FILE *in,*out;
 if ((out=fopen(“C:\\c\\student.txt”,”rb”))==NULL)          /*打开student.txt文件*/
  {printf(“can not open outfile\n”);
   exit(0);
  }
  for(i=0;i<SIZE;i++)
  {if(fread(&stu[i],sizeof(struct student),1,out)!=1)             /*从student.txt读取数据*/
   printf(“file read error\n”);
  }
 fclose(out);
 if((in=fopen(“C:\\c\\backup.txt”,”wb”))==NULL)                /*打开backup.txt*/
 {printf(“can not open infile\n”);
  exit(0);
 }
for(i=0;i<10;i++)
{ if(fwrite(&stu[i],sizeof(struct student),1,in)!=1)                 /*将student.txt的数据写
 printf(“file write error\n”);                                       入backup.txt中*/
}
fclose(in);
}

/*****************************************************************************/
void main()
{FILE *fp;
 char build,other=’y’;
 int i,n,wl;
 fp=fopen(“C:\\c\\student.txt”,”rb”);
 if(fp==NULL)
 {system(“cls”);
printf(“\n====>the file is inexistence,build?(y/n)\n”);          /*没有student.txt文件
  scanf(“%c”,&build);                                           是否创建*/  
  if(build==’y’||build==’Y’)
  {fp=fopen(“C:\\c\\student.txt”,”wb”);
   printf(“please enter name,number,GS,GSXF,YY,YYXF,WL,WLXF\n”);
   for(i=0;i<SIZE;i++)
scanf(“%s,%d,%f,%f,%f,%f,%f,%f”,stu[i].name,stu[i].num,stu[i].score1,stu[i].creh1, stu[i].score2,stu[i].creh2, stu[i].score3,stu[i].creh3);     /*输入每个学生的数据*/
save();                                      /*保存到student.txt中*/
  }
else
exit(0);
 }
while(other==’y’||other==’Y’)
{
system(“cls”);
menu();
scanf(“%d”,&n);
if(n>=1&&n>=3)
 {switch(n)
  {case 1:fun();break;
   case 2:backupdata();break;
   case 3:exit(0);
  }
 }
else exit(0);
system(“pause”);
printf(“is there anything else?(y/n)”);             /*是否有其它事?*/
scanf(“%c”,&other);
}

[[it] 本帖最后由 saysaysay 于 2008-6-26 23:42 编辑 [/it]]

[[it] 本帖最后由 saysaysay 于 2008-6-26 23:43 编辑 [/it]]
搜索更多相关主题的帖子: fun 函数 
2008-06-26 20:46
chinaxy11
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2007-11-14
收藏
得分:0 
是你写的有很多错误。。你整理好了,在分析
2008-06-26 22:42
saysaysay
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2008-6-18
收藏
得分:0 
你说的很多错误是指整个很多错误还是fun函数那里很多错误?这个程序有两个功能,一个是备份,一个是找出及格与不及格的人,并将他们的信息存储到score.txt文件中,我测试了,备份功能可以实现。另一个不行,得出的结果是全部人都不及格了,而且不是输出人名,而是全部信息
2008-06-26 23:25
快速回复:代码不多,大家帮忙看看fun()函数那里里应该怎么改,就是输不出正确的 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018718 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved