首页上一页 1 下一页尾页 1 条记录 1/1页
发现BUG啦,第17章的学生成绩管理系统
发表在C语言图书答疑
2016-08-06
是否精华
是
否
版块置顶:
是
否
第5个选项插入学生资料,如果取插在中间学生的资料前面,程序只检验前面元素有没有重复学号,没有检验后面元素
void insert()
{
FILE *fp;
int i,j,k,m=0,snum;
if((fp=fopen("data","ab+"))==NULL)
{
printf("can not open\n");
return;
}
while(!feof(fp))
if(fread(&stu[m],LEN,1,fp)==1)
m++;
if(m==0)
{
printf("no record/没有学生资料!\n");
fclose(fp);
return;
}
printf("please input position where do you want to insert!(input the number)\n");
scanf("%d",&snum);
for(i=0;i<m;i++)
if(snum==stu[i].num)
break;
for(j=m-1;j>i;j--)
stu[j+1]=stu[j];
printf("now please input the new information.\n");
printf("number/学号:");
scanf("%d",&stu[i+1].num);
for(k=0;k<m;k++)
/*插入新生资料前,学生总数是多了1个位置,用k-m就是忽视了m与多的位置*/
if(stu[k].num==stu[i+1].num&&k!=i+1)
/*我把上面改成for(k=0;k<(m+2);k++),也不行,不知道咋改*/
{
printf("the number is existing,press any to continue!");
getch();
fclose(fp);
return;
}
printf("please input per centum/输入百分比值:");
printf("\nelective/选修课百分比值:");
scanf("%f",&Felec);
printf("\nexperiment/实验课百分比值:");
scanf("%f",&Fexpe);
printf("\nrequired course/必修课百分比值:");
scanf("%f",&Frequ);
printf("name/新生姓名:\n");
scanf("%s",stu[i+1].name);
printf("\nelective/选修课成绩:");
scanf("%lf",&stu[i+1].elec);
printf("\nexperiment/实验课成绩:");
scanf("%lf",&stu[i+1].expe);
printf("\nrequired course/必修课成绩:");
scanf("%lf",&stu[i+1].requ);
stu[i+1].sum=stu[i+1].elec*Felec+stu[i+1].expe*Fexpe+stu[i+1].requ*Frequ;
if((fp=fopen("data","wb"))==NULL)
{
printf("can not open\n");
return;
}
for(k=0;k<=m;k++)
if(fwrite(&stu[k] ,LEN,1,fp)!=1)
{
printf("can not save!");
getch();
}
fclose(fp);
}
void insert()
{
FILE *fp;
int i,j,k,m=0,snum;
if((fp=fopen("data","ab+"))==NULL)
{
printf("can not open\n");
return;
}
while(!feof(fp))
if(fread(&stu[m],LEN,1,fp)==1)
m++;
if(m==0)
{
printf("no record/没有学生资料!\n");
fclose(fp);
return;
}
printf("please input position where do you want to insert!(input the number)\n");
scanf("%d",&snum);
for(i=0;i<m;i++)
if(snum==stu[i].num)
break;
for(j=m-1;j>i;j--)
stu[j+1]=stu[j];
printf("now please input the new information.\n");
printf("number/学号:");
scanf("%d",&stu[i+1].num);
for(k=0;k<m;k++)
/*插入新生资料前,学生总数是多了1个位置,用k-m就是忽视了m与多的位置*/
if(stu[k].num==stu[i+1].num&&k!=i+1)
/*我把上面改成for(k=0;k<(m+2);k++),也不行,不知道咋改*/
{
printf("the number is existing,press any to continue!");
getch();
fclose(fp);
return;
}
printf("please input per centum/输入百分比值:");
printf("\nelective/选修课百分比值:");
scanf("%f",&Felec);
printf("\nexperiment/实验课百分比值:");
scanf("%f",&Fexpe);
printf("\nrequired course/必修课百分比值:");
scanf("%f",&Frequ);
printf("name/新生姓名:\n");
scanf("%s",stu[i+1].name);
printf("\nelective/选修课成绩:");
scanf("%lf",&stu[i+1].elec);
printf("\nexperiment/实验课成绩:");
scanf("%lf",&stu[i+1].expe);
printf("\nrequired course/必修课成绩:");
scanf("%lf",&stu[i+1].requ);
stu[i+1].sum=stu[i+1].elec*Felec+stu[i+1].expe*Fexpe+stu[i+1].requ*Frequ;
if((fp=fopen("data","wb"))==NULL)
{
printf("can not open\n");
return;
}
for(k=0;k<=m;k++)
if(fwrite(&stu[k] ,LEN,1,fp)!=1)
{
printf("can not save!");
getch();
}
fclose(fp);
}