求助,未完善的程序,谁知道为什么调用fun 1,fun2,fun3时会停止运行?望指点
#include <stdio.h>#include <string.h>
#include <malloc.h>
#define LEN sizeof(struct student)
int n=0;
struct student
{ long int num;
char name[20];
float chinese,math,english,average;
int sum;
struct student *next;
};
main()
{
struct student *head,*stud;
struct student *fun1(void);
struct student *fun2(struct student *head,long num);
struct student *fun3(struct student *head,long num);
struct student *fun4(struct student *head,struct student *stu);
int fun5(void);
char a='1' ;
printf("\n ********************** 学生成绩管理系统 ********************** \n\n\n");
printf("\t\t请选择功能: 1.初始化\t 2.学生删除\n");
printf("\t\t 3.学生查找\t 4.学生添加\n");
printf("\t\t 5.学生总数\t 0.退出系统\n\n\n");
printf("********************************************************************************\n\n");
while(a!='0')
{
a=getchar();
switch(a)
{ case '1':fun1();break;
case '2':{long m;printf("请输入要删除的学号:");scanf("%ld",&m);head=fun2(head,m);};break;
case '3':{long h;printf("请输入要查找学生的学号:");scanf("%ld",&h);fun3(head,h);};break;
case '4':{printf("请依次输入要插入学生的学号,姓名,语文成绩,数学成绩,英语成绩:\n");scanf("%ld",&stud->num);scanf("%s",&stud->name);scanf("%d%d%d",&stud->chinese,&stud->math,&stud->english);head=fun4(head,stud);};break;
case '5':fun5();break;
}
}
return 0;
}
/*调用的函数1*/
struct student *fun1(void)
{ struct student *head;
struct student *p1,*p2;
p1=p2=(struct student*)malloc(LEN);
printf("请依次输入学生学号,姓名,语文成绩,数学成绩,英语成绩\n");
scanf("%ld",&p1->num);
scanf("%s",p1->name);
scanf("%d%d%d",&p1->chinese,&p1->math,&p1->english);
head=NULL;
p1->sum=p1->chinese+p1->math+p1->english;
p1->average=p1->sum/3;
while(1)
{n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct student*)malloc(LEN);
printf("请依次输入学生学号,姓名,语文成绩,数学成绩,英语成绩\n");
scanf("%d",&p1->num);
if(p1->num==0)
{printf(" 此操作已完成,请选择其他操作:\n\n\n");
printf("\t\t 1.初始化\t 2.学生删除\n");
printf("\t\t 3.学生查找\t 4.学生添加\n");
printf("\t\t 5.学生总数\t 0.退出系统\n\n\n");
printf("********************************************************************************\n\n");
break;}
scanf("%s",p1->name);
scanf("%d%d%d",&p1->chinese,&p1->math,&p1->english);
}
p2->next=NULL;
return(head);
}
/*调用的函数2*/
struct student *fun2(struct student *head,long num)
{struct student *p1,*p2;
if(head==NULL)
{printf("\nlist null!\n");
goto end;
}
p1=head;
while(num!=p1->num&&p1->next!=NULL)
{p2=p1;
p1=p1->next;
}
if(num==p1->num)
{if(p1==head)
{
head=p1->next;
printf("%ld号已从系统删除\n",num);}
else
p2->next=p1->next;
printf("%ld号已从系统删除\n",num);
n=n-1;
}
else
printf("%ld 号不在系统当中\n",num);
end:
return (head);
}
/*调用的函数5*/
int fun5()
{
printf("系统中共有%d个学生",n);
printf(" 此操作已完成,请选择其他操作:\n\n\n");
printf("\t\t 1.初始化\t 2.学生删除\n");
printf("\t\t 3.学生查找\t 4.学生添加\n");
printf("\t\t 5.学生总数\t 0.退出系统\n\n\n");
printf("********************************************************************************\n\n");
return 0;
}
/*调用的函数4*/
struct student *fun4(struct student *head,struct student *stu)
{
struct student *p0,*p1,*p2;
p1=head;
p0=stu;
if(head==NULL)
{head=p0;p0->next=NULL;}
else
{
while((p0->num>p1->num)&&(p1->num!=NULL))
{p2=p1;
p1=p1->next;}
if(p0->num<=p1->num)
{if(head==p1)head=p0;
else p2->next=p0;
p0->next=p1;}
else
{p1->next=p0;p0->next=NULL;}
}
n=n+1;
printf(" 此操作已完成,请选择其他操作:\n\n\n");
printf("\t\t 1.初始化\t 2.学生删除\n");
printf("\t\t 3.学生查找\t 4.学生添加\n");
printf("\t\t 5.学生总数\t 0.退出系统\n\n\n");
printf("********************************************************************************\n\n");
return(head);
}
/*调用的函数3*/
struct student *fun3(struct student *head,long num)
{
struct student *p1,*p2;
p1=head;
if(p1==NULL)
printf("系统中没有要查找的学生\n");
else
while(num!=p1->num&&p1->next==NULL)
{p2=p1;p1=p1->next;}
if(p1->next==NULL)
printf("系统中没有要查找的学生\n");
else
printf("要查找的学生学号, 姓名,语文成绩,数学成绩,英语成绩依次是:%ld %s %d %d %d",p1->num,p1->name,p1->chinese,p1->math,p1->english);
return 0;
}