大家好,这是小弟写的一个学生成绩管理程序,在creat这个新建学生这个函数中,我想要当输入姓名为exit时退出creat这个函数,但运行的时候总是退出不了这个循环,请问该怎么改阿?谢谢大家
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define STU_LEN sizeof(struct student)
#define NULL 0
struct student
{
long id;
char name[20];
int math,chinese,english,physics;
struct student *next;
};
struct student *creat(void)
{
struct student *head,*p1,*p2;
p1=p2=(struct student *)malloc(STU_LEN);
printf("请输入学生姓名:\n");
scanf("%s",&p1->name);
printf("请输入%s的学号:\n");
scanf("%ld",&p1->id);
printf("请输入%s的数学成绩:\n",p1->name);
scanf("%d",&p1->math);
printf("请输入%s的语文成绩:\n",p1->name);
scanf("%d",&p1->chinese);
printf("请输入%s的英语成绩:\n",p1->name);
scanf("%d",&p1->english);
printf("请输入%s的语文成绩:\n",p1->name);
scanf("%d",&p1->physics);
getchar();
head=NULL;
while(p1->name!="exit")
{
if(head==NULL)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct student *)malloc(STU_LEN);
printf("请输入学生姓名:\n");
scanf("%s",&p1->name);
printf("请输入%s的学号:\n",p1->name);
scanf("%ld",&p1->id);
printf("请输入%s的数学成绩:\n",p1->name);
scanf("%d",&p1->math);
printf("请输入%s的语文成绩:\n",p1->name);
scanf("%d",&p1->chinese);
printf("请输入%s的英语成绩:\n",p1->name);
scanf("%d",&p1->english);
printf("请输入%s的语文成绩:\n",p1->name);
scanf("%d",&p1->physics);
getchar();
}
p2->next=NULL;
return(head);
}
void print(struct student *head)
{
struct student *p;
p=head;
while(p!=NULL)
{
printf("姓名:%s 学号:%ld 数学分数:%d 语文分数:%d 英语分数:%d 物理分数:%d\n",p->name,p->id,p->math,p->chinese,p->english,p->physics);
p=p->next;
}
}
struct student *intsert(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->id>p1->id&&(p1->next!=NULL))
{
p2=p1;
p1=p1->next;
}
if(p0->id<=p1->id)
{
if(head==p1)
head=p0;
else
p2->next=p0;
p0->next=p1;
}
else
{
p1->next=p0;
p0->next=NULL;
}
}
return(head);
}
struct student *del(struct student *head,long id)
{
struct student *p1,*p2;
if(head==NULL)
printf("\nlist null!\n");
p1=head;
while(id!=p1->id&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(id==p1->id)
{
if(p1==head)
head=p1->next;
else
p2->next=p1->next;
printf("delete:%ld\n",id);
}
else
printf("%ld not been found!\n",id);
return(head);
}
void search(struct student *head,long id)
{
struct student *p;
p=head;
do
{
if(id==p->id)
{
printf("学号为%ld的学生的数学成绩为%d, 语文成绩为:%d, 英语成绩为:%d,物理成绩为:%d\n",p->id,p->math,p->chinese,p->english,p->physics);
p=p->next;
}
else
p=p->next;
}while(p->next!=NULL);
}
void Max_score(struct student *head)
{
struct student *p1,*p11,*p2,*p22,*p3,*p33,*p4,*p44;
int Max_math=0,Max_chinese=0,Max_english=0,Max_physics=0;
p1=head;
p2=head;
p3=head;
p4=head;
do
{
if(p1->math>=Max_math)
{
Max_math=p1->math;
p11=p1;
p1=p1->next;
}
else
p1=p1->next;
}while(p1->next!=NULL);
if(p1->math>Max_math)
{
Max_math=p1->math;
p11=p1;
}
printf("数学最高分是:%d 是由学号为%d的学生取得的\n",Max_math,p11->id);
do
{
if(p2->chinese>=Max_chinese)
{
Max_chinese=p2->chinese;
p22=p2;
p2=p2->next;
}
else
p2=p2->next;
}while(p2->next!=NULL);
if(p2->chinese>Max_chinese)
{
Max_chinese=p2->chinese;
p22=p2;
}
printf("语文最高分:%d 是由学号为%d的学生取得的\n",Max_chinese,p22->id);
do
{
if(p3->english>=Max_english)
{
Max_english=p3->english;
p33=p3;
p3=p3->next;
}
else
p3=p3->next;
}while(p3->next!=NULL);
if(p3->english>Max_english)
{
Max_english=p3->english;
p33=p3;
}
printf("英语最高分是:%d 是由学号为%d的学生取得的\n",Max_english,p33->id);
do
{
if(p4->physics>=Max_physics)
{
Max_physics=p4->physics;
p44=p4;
p4=p4->next;
}
else
p4=p4->next;
}while(p4->next!=NULL);
if(p4->physics>Max_physics)
{
Max_physics=p4->physics;
p44=p4;
}
printf("物理最高分是:%d 是由学号为%d的学生取得的\n",Max_physics,p44->id);
}
char a;
main()
{
FILE *fp;
struct student *head,*p,stu;
long id;
char a;
do
{
printf("\t\t----淮海工学院计算机系学生管理系统----\t\n");
printf("\t\t\t\t\tver 1.00\n");
printf("\t\t----press l to creat a record\n");
printf("\n");
printf("\t\t----press c to creat a record\n");
printf("\n");
printf("\t\t----press i to insert a record\n");
printf("\n");
printf("\t\t----press d to delete a record\n");
printf("\n");
printf("\t\t----press s to search a record\n");
printf("\n");
printf("\t\t----press m to find the Maxscore\n");
printf("\n");
printf("\t\t----press other to exit\n");
scanf("%c",&a);
switch(a)
{
case 'c':head=creat();
print(head);
getchar(); break;
case 'i':
p=head;
printf("请输入学生姓名:\n");
scanf("%s",&stu.name);
printf("请输入%s的学号:\n",p->name);
scanf("%ld",&stu.id);
printf("请输入%s的数学成绩:\n",p->name);
scanf("%d",&stu.math);
printf("请输入%s的语文成绩:\n",p->name);
scanf("%d",&stu.chinese);
printf("请输入%s的英语成绩:\n",p->name);
scanf("%d",&stu.english);
printf("请输入%s的语文成绩:\n",p->name);
scanf("%d",&stu.physics);
head=intsert(head,&stu);
print(head);
getchar();break;
case 'd':printf("请输入要删除学生的学号:\n");
scanf("%ld",&id);
head=del(head,id);
print(head);
getchar();break;
case 's':printf("请输入要查找学生的学号:\n");
scanf("%ld",&id);
search(head,id);
getchar(); break;
case 'm':Max_score(head); break;
default :a=0;
}
}while(a!=0);
getch();
}