匿名用户
1级
-11-13 回答
#include
#include
#include
structstud
{
longnum;
charname[20];
doublescore;
};
typedefstructstucode
{
structstudstudent;
structstucode*next;
}L;
voidmenu();
voidcreatelist(structstucode**r);
voidout(structstucode*r);
voidsearch1(structstucode*r);
voidsearch2(structstucode*r);
voiddel(structstucode**r);
voidinsert(structstucode**r);
voidsort(structstucode**r);
voidmain()
{
charchoose;
intflag=1;
structstucode*r=NULL;
while(flag)
{
system("cls");
menu();
choose=getchar();
switch(choose)
{
case'1':
createlist(&r);
out(r);
printf("Testingfunction1\nPressanykeytocontinue\n");
getchar();
getchar();
break;
case'2':
search1(r);
printf("Testingfunction1\nPressanykeytocontinue\n");
getchar();
getchar();
break;
case'3':
search2(r);
printf("Testingfunction1\nPressanykeytocontinue\n");
getchar();
getchar();
break;
case'4':
del(&r);
out(r);
printf("Testingfunction1\nPressanykeytocontinue\n");
getchar();
getchar();
break;
case'5':
insert(&r);
out(r);
printf("Testingfunction1\nPressanykeytocontinue\n");
getchar();
getchar();
break;
case'6':
sort(&r);
out(r);
printf("Testingfunction1\nPressanykeytocontinue\n");
getchar();
getchar();
break;
case'7':
out(r);
printf("Testingfunction7\nPressanykeytocontinue\n");
getchar();
getchar();
break;
case'0':
flag=0;
printf("Theend.\n");
break;
default:printf("\nWrongSelection!(选择错误,请重选!)\n");getchar();getchar();
}
}
}
voidcreatelist(structstucode**r)
{
structstucode*p,*t;
longn;
chara[20];
doubles;
if(*r)*r=NULL;
printf("\n请输入:\n学号(请按学号升序排列)姓名分数(若要结束请输入三个为零)\n");
scanf("%ld%s%lf",&n,a,&s);
if(n==0)return;
p=(L*)malloc(sizeof(L));
p->student.num=n;
strcpy(p->student.name,a);
p->student.score=s;
p->next=NULL;
*r=p;
scanf("%ld%s%lf",&n,a,&s);
while(n)
{
t=p;
p=(L*)malloc(sizeof(L));
p->student.num=n;
strcpy(p->student.name,a);
p->student.score=s;
p->next=NULL;
t->next=p;
scanf("%ld%s%lf",&n,a,&s);
}
}
voidsearch1(structstucode*r)
{
longx;
if(!r)
{
printf("没有学生信息可查询!\n");
return;
}
printf("请输入要查询的学生信息的学生学号:\n");
scanf("%ld",&x);
while(r&&r->student.num!=x)
r=r->next;
if(r==NULL)
printf("Error!Nosuchstudent!\n");
else
printf("%ld%s%.2lf\n",r->student.num,r->student.name,r->student.score);
}
voidsearch2(structstucode*r)
{
charm[20];
if(!r)
{
printf("没有学生信息可查询!\n");
return;
}
printf("请输入要查询的学生信息的学生姓名:\n");
scanf("%s",m);
while(r&&strcmp(r->student.name,m))
r=r->next;
if(r==NULL)
printf("Error!Nosuchstudent!\n");
else
printf("%ld%s%.2lf\n",r->student.num,r->student.name,r->student.score);
}
voiddel(structstucode**r)
{
longk;
structstucode*p=*r,*t;
if(!(*r))
{
printf("没有学生信息可删除!\n");
return;
}
printf("请输入要删除的学生信息的学生学号:\n");
scanf("%ld",&k);
if(p->student.num==k)
*r=(*r)->next,free(p);
else
{
while(p->next&&p->next->student.num!=k)
p=p->next;
if(p->next==NULL)
printf("Error!Nosuchstudent!\n");
else
{
t=p->next;
p->next=p->next->next;
free(t);
}
}
}
voidinsert(structstucode**r)
{
longn;
chara[20];
doubles;
L*p,*t,*k;
printf("请输入要插入的学生信息的学生学号姓名分数:\n");
scanf("%ld%s%lf",&n,a,&s);
p=(L*)malloc(sizeof(L));
p->student.num=n;
p->student.score=s;
strcpy(p->student.name,a);
if(!(*r))
{
*r=p;
(*r)->next=NULL;
return;
}
if(p->student.numstudent.num)
p->next=(*r),(*r)=p;
else
{
t=*r;
k=t;
while(t->next&&t->next->student.num<=p->student.num)
t=t->next;
p->next=t->next;
t->next=p;
*r=k;
}
}
voidsort(structstucode**r)
{
structstucode*t,*p,*q,*z;
if(!r)
{
printf("没有学生信息可排序!\n");
return;
}
if(!(*r)||!(*r)->next)
return;
t=*r;
p=t->next;
t->next=NULL;
while(p)
{
q=p->next;
if(p->student.score>t->student.score)
{
p->next=t;
t=p;
}
else
{
z=t;
while(z->next&&z->next->student.score>=p->student.score)
z=z->next;
p->next=z->next;
z->next=p;
}
p=q;
}
*r=t;
}
voidout(structstucode*r)
{
printf("\n\n");
if(!r)
{
printf("没有学生信息可输出!\n");
return;
}
while(r)
{
printf("%ld%s%.2lf\n",r->student.num,r->student.name,r->student.score);
r=r->next;
}
printf("\n\n");
}
voidmenu()
{
printf("\n学生信息管理系统\n");
printf("\n菜单\n\n");
printf("\n1建立链表并显示\n");
printf("\n2查找某学号的学生信息\n");
printf("\n3查找某姓名的学生信息\n");
printf("\n4删除某学号的学生信息\n");
printf("\n5插入新的学生信息\n");
printf("\n6按分数降序排列输出\n");
printf("\n7输出\n");
printf("\n0退出\n");
printf("\n请选择您要执行的选项:\n");
}