| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 477 人关注过本帖
标题:[求助]一个小程序,各位看下
只看楼主 加入收藏
lm123
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2006-7-9
收藏
 问题点数:0 回复次数:0 
[求助]一个小程序,各位看下

代码如下,帮忙修改下,谢了

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#define SU 10
FILE *fp1;
struct student
{
int no;
char name[10];
int ji1;
int ji2;
int ji3;
int sum;
}stu[SU];
int han_shu();
int han_cha();
int han_xiu();
int han_shan();
int han_tong();
int n=0;
void main()
{
if ((fp1=fopen("wen_shu","w"))==NULL) /*定义文件*/
printf("文件不能打开" );fclose(fp1);
int c;
while(1)
{
printf("\n");
printf("\n");
printf("\n");
printf(" *****************************************************\n");
printf(" * *\n");
printf(" * 1-输入 2-查询 3-修改 4-删除 *\n");
printf(" * 5-统计 0-退出 *\n");
printf(" * *\n");
printf(" *****************************************************\n");
printf("\n");
printf("\t请选择:");
c=getchar();clrscr();
switch?
{
case'1':han_shu();break;
case'2':han_cha();break;
case'3':han_xiu();break;
case'4':han_shan();break;
case'5':han_tong();break;
case'0':{clrscr();
printf("\n\n\n\n\n\t\t您已经退出系统!");
}exit(0);

}
}
}
/*********************************/
int han_shu() /*输入的函数*/
{
int t;
if ((fp1=fopen("wen_shu","a+"))==NULL)
printf("打开错误");
for(t=n;t<SU;t++)
{ printf("\t 学号 姓名 基础 导论 程序\n");
printf("请输入:");
scanf("%d%s%d%d%d",&stu[t].no,stu[t].name,&stu[t].ji1,
&stu[t].ji2,&stu[t].ji3);
stu[t].sum=stu[t].ji1+stu[t].ji2+stu[t].ji3;
fwrite(&stu[t],sizeof(struct student),1,fp1);
n++;
printf("还要添加吗?y/n");
getchar();
if (getchar()!='y')break;
}
fclose(fp1); /*关闭f1所指文件*/
return 0;
}
/*********************************/
int han_cha() /*查询的函数*/
{
if ((fp1=fopen("wen_shu","r"))==NULL)
printf("打开错误");
int i,c2;
while(!feof(fp1))
{
clrscr();
printf("请输入你要查询的学号:");
scanf("%d",&c2);
rewind(fp1);
for(i=0;i<SU;i++)
{
fread(&stu[i],sizeof(struct student),1,fp1);
if (c2==stu[i].no)
{
printf("\n该生情况如下:\n学号 姓名 基础 导论 程序\n");
printf("%d %4s %4d %4d %4d",stu[i].no,stu[i].name,
stu[i].ji1,stu[i].ji2,stu[i].ji3);
break;
}
}
if(i==SU)
printf("\n\n\n对不起!没有您所需的记录");
printf("\n\n\n还需查询吗?y/n");
getchar();
if (getchar()!='y')break;
}
fclose(fp1); /*关闭f1所指文件*/
return 0;
}
/*********************************/
int han_shan() /*删除的函数*/
{
int r,y,u,j,e,m;
while(1)
{
if ((fp1=fopen("wen_shu","r"))==NULL)
printf("不能打开");
if (!feof(fp1))rewind(fp1);
for (r=0;!feof(fp1)&&fread(&stu[r],sizeof(struct student),
1,fp1);r++);
printf("要删除的学号");
scanf("%d",&y);
for (u=0;u<r&&stu[u].no!=y;u++);
if(u>=r)printf("没有学生信息");
fclose(fp1);
n--;
if(r>1)
{
printf("\n删除后的学生情况如下:\n学号 姓名 基础 导论 程序\n");
for (j=u;j<r-1;j++)stu[u]=stu[u+1];
fp1=fopen("wen_shu","w+");
for (u=0;u<r-1;u++)
{
fwrite(&stu[u],sizeof(struct student),1,fp1);
fread(&stu[u],sizeof(struct student),1,fp1);
printf("\n%d %4s %4d %4d %4d",stu[u].no,stu[u].name,
stu[u].ji1,stu[u].ji2,stu[u].ji3);
}
fclose(fp1);
}
else
{printf ("无任何记录");
fclose(fp1);
}
printf("\n\n还要删除吗?y/n");
getchar();
if (getchar()!='y'){clrscr();break; }
clrscr();
}
return 0;
}

/*********************************/
int han_xiu() /*修改的函数*/
{
int z,a,d,s,g,l;
if ((fp1=fopen("wen_shu","r"))==NULL)printf("不能打开");
if(!feof(fp1))
{
rewind(fp1);
printf("输入要修改的学生学号");
scanf("%d",&s);
for (z=0;fread(&stu[z],sizeof(struct student),
1,fp1);z++)
if(s==stu[z].no){a=z;break;}
if (z==SU)
printf("\n没有学生信息\n");
for (1;z<SU;z++)
fread(&stu[z],sizeof(struct student),1,fp1);
fclose(fp1);
printf("\t 学号 姓名 基础 导论 程序\n");
printf("请输入:");
scanf("%d%s%d%d%d",&stu[a].no,stu[a].name,&stu[a].ji1,
&stu[a].ji2,&stu[a].ji3);
fp1=fopen("wen_shu","w+");
printf("\n\n修改后的学生情况如下:\n学号 姓名 基础 导论 程序\n");
for (g=0;g<n;g++)
fwrite(&stu[g],sizeof(struct student),1,fp1);
rewind(fp1);
for(l=0;l<n;l++)
{ fread(&stu[g],sizeof(struct student),1,fp1);
printf("\n%d %4s %4d %4d %4d",stu[g].no,stu[g].name,
stu[g].ji1,stu[g].ji2,stu[g].ji3);
}
}
else
{ printf ("无任何记录");
fclose(fp1);
}
printf("\n\n退回菜单请输入回车键");
getchar();
if (getchar()!=EOF); clrscr();
return 0;
}
/*********************************/
int han_tong() /*统计的函数*/
{
int k;
printf("\n学生情况如下:\n学号 姓名 基础 导论 程序 总成绩\n");
if ((fp1=fopen("wen_shu","r"))==NULL) /*打开f1所指文件*/
printf("打开错误");
for(k=0;k<n;++k)
{ fread(&stu[k],sizeof(struct student),1,fp1);
printf("%d %4s %4d %4d %4d %4d\n",stu[k].no,stu[k].name,
stu[k].ji1,stu[k].ji2,stu[k].ji3, stu[k].sum);
}
fclose(fp1); /*关闭f1所指文件*/
printf("\n\n退回菜单请输入回车键");
getchar();
if (getchar()!=EOF); clrscr();
return 0;
}

[此贴子已经被作者于2006-7-9 15:43:21编辑过]

2006-07-09 15:42
快速回复:[求助]一个小程序,各位看下
数据加载中...
 
   



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

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