标题:[求助]大家看看这个学生成绩管理系统程序的错误
只看楼主
lookdd1
Rank: 1
等 级:新手上路
帖 子:27
专家分:0
注 册:2005-12-13
 问题点数:0 回复次数:1 
[求助]大家看看这个学生成绩管理系统程序的错误

我运行了,编译可以通过。文件读入和输出、删除、查找都可以!但是,插入(insert函数)无法实现!
我用的文件和链表!请大家帮忙!
#include<stdio.h>
#include<math.h>
#include<malloc.h>
#include<stddef.h>
#include<string.h>
#define LEN sizeof(struct stud)/*宏定义*/
struct stud
{char name[10];
int num;
int scores;
struct stud *next;
};
struct stud*p1,*p2,*head;
FILE *fp;
int n;
void creat()
{if((fp=fopen("s_scores.txt","w"))==NULL)
{printf("Can't open file!\n");
exit(0);
}
for(p1=head;p1!=NULL;p1=p1->next)
if(fwrite(p1,LEN,1,fp)==NULL)
printf("File write error!\n");
fclose(fp);
}
struct stud *input()/*链式输入,并保存到磁盘*/
{char ch;
int i=1;
p1=(struct stud *)malloc(LEN);
head=p1;
while(i)
{printf("input the name:\n");
scanf("%s",p1->name);
printf("input the number:\n");
scanf("%d",&p1->num);
printf("input the scores:\n");
scanf("%d",&p1->scores);
getchar();
printf("another?y/n");
ch=getchar();
if(ch=='n'||ch=='N')
{ i=0;
p1->next=NULL;
}
else
{p2=p1;
p1=(struct stud *)malloc(LEN);
p2->next=p1;
}
}
creat();/*存盘*/
return(head);}
struct stud *output(struct stud *head)/*输出函数*/
{ if((fp=fopen("s_scores.txt","r"))==NULL)/*打开文件*/
{printf("Can't read the file!\n");
exit(0);}
for(p1=head;p1!=NULL;p1=p1->next)/*读文件*/
{fread(p1,LEN,1,fp);
printf("%-10s%-6d%-4d\n",p1->name,p1->num,p1->scores);
}getchar();
printf("any key continue...\n");
getchar();/*便于在屏幕上显示,可以看到输出结果*/
return(p1);}
void search(struct stud *head)/*查找函数*/
{char name[10];
p1=(struct stud *)malloc(LEN);/*分配一个内存单元*/
printf("Please put the name which you want to find:\n");
scanf("%s",name);/*从键盘输入名字*/
if((fp=fopen("s_scores.txt","r"))==NULL)/*以读的形式打开*/
printf("Unluckily!File open failed!");
for(p1=head;p1!=NULL;p1=p1->next)
{fread(p1,LEN,1,fp);
if(strcmp(name,p1->name)==0)/*和名字比较,相同就输出*/
{printf("%s\n",p1->name);
printf("%d\n",p1->num);
printf("%d\n",p1->scores);break;}}
printf("any key continue!\n");
getchar();
getchar();
fclose(fp);}
struct stud *del(struct stud *head)/*删除一组数据*/
{int num;
p1=(struct stud *)malloc(LEN);
printf("please put the num which you want to del!\n");
scanf("%d",&num);
if((fp=fopen("s_scores","w"))==NULL)/*以写的形式打开*/
printf("Unlucily!file write error!");
{{ p1=head;
while(num!=p1->num&&p1->next!=NULL)
{ p2=p1;
p1=p1->next;
}
if(num==p1->num)
{if(p1==head)
head=p1->next;
else p2->next=p1->next;
for(p1=head;p1!=NULL;p1=p1->next)
fwrite(p1,LEN,1,fp);/*保存到磁盘*/
fclose(fp); }}
getchar();
printf("any key continue.......\n");
getchar(); }
return(head);
}
void insert(struct stud *p2)/*插入一组数据,插入到最后*/
{struct stud *p3;
p3=(struct stud *)malloc(LEN);
printf("put the name:\n");
scanf("%s",p3->name);
printf("put the num:\n");
scanf("%d",&p3->num);
printf("put the scores:\n");
scanf("%d",&p3->scores);
p2=p3;
p2->next=NULL;/*把尾地址赋给该节点*/
if((fp=fopen("s_scores","w"))==NULL)
printf("file open error!\n");
{ for(p1=head;p1!=NULL;p1=p1->next)
fwrite(p1,LEN,1,fp);
fclose(fp);}
getchar();
printf("any key continue....\n");
getchar();}
main()
{char i;
while(1)/*循环执行,当i='6'的时候跳出*/
{clrscr();
printf("~~~~~~~~~~~~~~wellcome~~~~~~~~~~~~~~\n");
printf("1)input 2)output 3)search\n");
printf("4)del 5)insert 6)exit\n");
printf("~~~~~~~~~~~~~~~system~~~~~~~~~~~~~~~\n");
printf("***********HAPPY NEW YEAR**************\n");
printf("please put your choice:\n");
i=getchar();
switch(i)
{case '1':head=input();break;
case '2':p2=output(head);break;
case '3':search(head);break;
case '4':head=del(head);break;
case '5':insert(p2);break;
case '6':exit(0);break;
}
}
}


[此贴子已经被作者于2006-1-7 14:31:27编辑过]

搜索更多相关主题的帖子: stud 系统程序 include struct 学生 
2006-01-07 13:51
Knocker
Rank: 8Rank: 8
等 级:贵宾
威 望:47
帖 子:10454
专家分:603
注 册:2004-6-1
得分:0 
建议你整理好格式及加上注解

九洲方除百尺冰,映秀又遭蛮牛耕。汽笛嘶鸣国旗半,哀伤尽处是重生。     -老K
治国就是治吏。礼义廉耻,国之四维。四维不张,国之不国。   -毛泽东
2006-01-07 14:07



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-41948-1-1.html




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

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