| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1270 人关注过本帖
标题:怎样跳出creat中while这个循环阿?
只看楼主 加入收藏
Garand
Rank: 1
等 级:新手上路
帖 子:56
专家分:0
注 册:2006-5-27
收藏
 问题点数:0 回复次数:6 
怎样跳出creat中while这个循环阿?

大家好,这是小弟写的一个学生成绩管理程序,在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();
}


搜索更多相关主题的帖子: creat 
2006-06-21 19:04
–★–
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:1512
专家分:0
注 册:2006-5-1
收藏
得分:0 
至少
while(p1->name!="exit")
应改为
while(stricmp(p1->name,"exit"))

注意:stricmp( )是string.h中有定义的库函数
比较形参1、2所指向的串(忽略大小写)是否相同
若相同返回0否则返回1。
显然用了它,你输入
exit
Exit
EXIT
都将导致退出。

落霞与孤鹜齐飞,秋水共长天一色! 心有多大,路有多宽。三教九流,鸡鸣狗盗。兼收并蓄,海纳百川。
2006-06-21 20:13
Garand
Rank: 1
等 级:新手上路
帖 子:56
专家分:0
注 册:2006-5-27
收藏
得分:0 
按楼上的做了,还是不行啊
2006-06-21 21:18
–★–
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:1512
专家分:0
注 册:2006-5-1
收藏
得分:0 
//粗制滥造,也好意思求助!
//只能是代你改多少算多少
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include<string.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=NULL,*p1,*p2;
p1=p2=(struct student *)malloc(STU_LEN);
printf("请输入学生姓名:\n");
scanf("%s",&p1->name);
if(stricmp(p1->name,"exit")==0)return head;
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);//语文2次?
scanf("%d",&p1->physics);

getchar();
head=NULL;
while(1)
{
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);//语文2次
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();
}


落霞与孤鹜齐飞,秋水共长天一色! 心有多大,路有多宽。三教九流,鸡鸣狗盗。兼收并蓄,海纳百川。
2006-06-21 21:40
Garand
Rank: 1
等 级:新手上路
帖 子:56
专家分:0
注 册:2006-5-27
收藏
得分:0 

您开头那句话真让我对斑竹这个头衔跌破眼镜

2006-06-21 22:15
Garand
Rank: 1
等 级:新手上路
帖 子:56
专家分:0
注 册:2006-5-27
收藏
得分:0 
while(1) ??
那就无法跳出这个循环了啊
2006-06-21 22:46
穆扬
Rank: 1
等 级:禁止发言
帖 子:1910
专家分:0
注 册:2006-6-1
收藏
得分:0 
提示: 作者被禁止或删除 内容自动屏蔽

2006-06-21 22:52
快速回复:怎样跳出creat中while这个循环阿?
数据加载中...
 
   



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

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