建议在循环后观察i的值
程序代码:
#include<stdio.h> #include<malloc.h> #define null 0 #define xian "---------------------------------------------------------------------------\n" struct student { int num; char name[20]; char sex[10]; int addr; float yuweng; float shuxue; float yingyu; struct student *next; }; #define changdu sizeof(struct student) int n; struct student* scan() { struct student *p; p=(struct student *)malloc(changdu); printf("请输入学生信息:\n"); printf("学号:\n"); scanf("%d",&p->num); printf("姓名:\n"); scanf("%s",p->name); printf("性别:\n"); scanf("%s",p->sex); printf("年龄:\n"); scanf("%s",&p->addr); printf("语文成绩:\n"); scanf("%f",&p->yuweng); printf("数学成绩:\n"); scanf("%f",&p->shuxue); printf("英语成绩:\n"); scanf("%f",&p->yingyu); return p; } //输入学生信息 struct student* chuangjian() { struct student *head,*p1,*p2; p1=p2=(struct student *)malloc(changdu); p1=scan(); head=null; while(p1->num!=0) { n++; if(n==1) head=p1; else p2->next=p1; p2=p1; p1=(struct student *)malloc(changdu); p1=scan(); } p2->next=null; return head; } //创建链表 void print(struct student *head) { struct student *p; printf(xian); printf("|\t\t\t学\t生\t信\t息\t\t\t |\n"); printf(xian); printf("| 学 号 | 姓 名\t| 性 别 | 年 龄 | 语 文 | 数 学 | 英 语 |\n"); p=head; while(head!=null) { printf(xian); printf("| %d | %s\t| %s | %d | %5.2f | %5.2f | %5.2f |\n",p->num,p->name,p->sex,p->addr,p->yuweng,p->shuxue,p->yingyu); p=p->next; } printf(xian); } //输出学生信息 struct student* del(struct student *head,int num) { struct student *p1,*p2; if(head==null) printf("暂时没有学生信息.\n"); 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; printf("删除学号为%d的学生信息\n",num); } else printf("没有这个学生的信息\n"); return head; } //删除学生信息 struct student* insert(struct student *head) { char x; struct student *p0,*p1,*p2; p1=head; printf("请问是否要插入学生信息?y或N\n"); scanf("%c",&x); if(x=='n') return head; else if(x=='y') { printf("请输入要插入的学生信息:\n"); p0=(struct student *)malloc(changdu); printf("学号:\n"); scanf("%d",&p0->num); printf("姓名:\n"); scanf("%s",p0->name); printf("性别:\n"); scanf("%s",p0->sex); printf("年龄:\n"); scanf("%s",&p0->addr); printf("语文成绩:\n"); scanf("%f",&p0->yuweng); printf("数学成绩:\n"); scanf("%f",&p0->shuxue); printf("英语成绩:\n"); scanf("%f",&p0->yingyu); } else { printf("输入错误,系统会当作您输入的是No\n"); } if(p0->num>p1->num&&p1->next!=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; } return head; } //插入学生信息 int to() { int m; printf("\t1-输入学生信息:\n"); printf("\t2-插入学生信息:\n"); printf("\t3-删除学生信息:\n"); printf("\t4-输出学生信息:\n"); printf("注意:在选2,3,4选项时,先选1进行创建,否则系统错误!!!!\n\n\n\n"); printf("请选择\n"); scanf("%d",&m); return m; } //菜单 void main() { struct student *head; char b; int num,m,i=0; int c; printf("\t\t************************************************\n"); printf("\t\t* *\n"); printf("\t\t* *\n"); printf("\t\t* 学 生 管 理 系 统 *\n"); printf("\t\t* *\n"); printf("\t\t* *\n"); printf("\t\t* 本系统乃小五制作,版权所有,不可侵犯!!!!!!! *\n"); printf("\t\t************************************************\n\n\n\n\n\n"); for(i=0;i<10;i++) { printf("是否进入主菜单?Y或N.\n"); printf("请选择:"); if(i!=0) //add a if while((c=getchar())!='\n') //add a while ; scanf("%c",&b); //%s change to %c while((c=getchar())!='\n') //add a while ; //printf("%d\n",b); if(b=='Y'||b=='y') { m=to(); switch(m) { case 1: head=chuangjian(); break; case 2: head=insert(head); break; case 3: printf("请输入要删除的学生学号:\n"); scanf("%d",&num); head=del(head,num); break; case 4: print(head); break; default: printf("输入错误\n"); break; } } else { printf("\n\n\n\n\t\t****** * * ****** *******\n"); printf("\t\t* * * * * * *\n"); printf("\t\t* * * * * * *\n"); printf("\t\t* * * * * * *\n"); printf("\t\t***** * ***** *******\n"); printf("\t\t* * * * * *\n"); printf("\t\t* * * * * *\n"); printf("\t\t* * * * * *\n"); printf("\t\t****** * ****** *******\n\n\n\n\n\n\n\n\n\n"); printf("谢谢使用!!!\n\n\n\n"); i=10; } } }没有多做测试,不过能解决楼主只能选择一次的问题