#include <stdio.h>
#include<stdlib.h>
#include<string.h>
struct student
{
char num[12];
char name[20];
char sex[4];
int score;
struct student *next;
};
int menu_s()
{
int choice ;
printf(“\n 学生信息管理系统\n”);
printf(“========================================
=\n”);
printf(“ 1.学生信息库的建立\n”);
printf(“ 2.插 入 学 生 信 息\n”);
printf(“ 3.查 询 学 生 信 息\n”);
printf(“ 4.删 除 学 生 信 息\n”);
printf(“ 5.输 出 所有学生信息\n”);
printf(“========================================
==\n”);
printf(“请选择1-5:\n”);
for(;;)
{
scanf(“%d”,&choice);
if (choice<0 || choice>5)
printf(“\n\t输入错误,重选1-5\n”);
else
break; /*跳出循环体*/
}
return choice;
}
void addlist(struct student**HL)
{
struct student* newp;
newp=(struct student*)malloc(sizeof(struct student));
if(newp==NULL)
{
printf(“内存空间用完,退出\n”);
exit(1);
}
printf(“请输入学生的学号,姓名,性别,分数,以空格隔开
\n”);
scanf(“%s%s%s%d”,newp->num,newp->name,newp-
>sex,&newp->score);
newp->next=NULL;
if(*HL==NULL)
*HL=newp;
else
{
struct student* p=*HL;
while(p->next!=NULL)
p=p->next;
p->next=newp;
}
printf(“插入完毕\n”);
}
void dellist(struct student** HL)
{
char num1[12];
char name1[20];
int i=0,choice;
struct student* cp=*HL;
struct student* ap=NULL;
printf(“===========================\n”);
printf(“1、按学号删除\n”);
printf(“2、按姓名删除\n”);
printf(“===========================\n”);
printf(“ 请选择: “);
fflush(stdin);
scanf(“%d”,&choice);
if (choice==1)
{
printf(“请输入要删除学生的学号:”);
scanf(“%s”,num1);
if(cp==NULL)
{
printf(“信息库为空,不能删除\n”);
return;
//exit(1);
}
while(cp!=NULL)
{
i++;
if (strcmp(num1,cp->num)==0)
break;
ap=cp;cp=cp->next;
}
if(cp==NULL)
{
printf(“无该生信息”);
return;
//exit(1);
}
if(i==1)
*HL=cp->next;
else
ap->next=cp->next;
free(cp);
printf(“已经删除成功^_^!!!\n”);
}
else if (choice==2)
{
printf(“请输入要查找学生的姓名:\n”);
scanf(“%s”,name1);
if(cp==NULL)
{
printf(“信息库为空,不能删除\n”);
return;
//exit(1);
}
while(cp!=NULL)
{
i++;
if (strcmp(name1,cp->name)==0)
break;
ap=cp;cp=cp->next;
}
if(cp==NULL)
{
printf(“无该生信息”);
return;
//exit(1);
}
if(i==1)
*HL=cp->next;
else
ap->next=cp->next;
free(cp);
printf(“已经删除成功^_^!!!!!!\n”);
}
}
void creat(struct student **HL )
{
int n=0;
int i=0;
*HL = NULL;
struct student* newp;
printf(“学生人数,请输入:\n”);
scanf(“%d”,&n);
for (i=1;i<=n;i++)
{
newp=(struct student
*)malloc(sizeof(struct student));
if(newp==NULL)
{printf(“内存空间用完,退出\n”);
exit(1);
}
printf(“请输入学生%d的学号,姓名,性别,分
数,以逗号隔开\n”,i);
scanf(“%s %s %s %d”,newp->num,newp-
>name,newp->sex,&newp->score);
newp->next=NULL;
if(*HL==NULL)
*HL=newp;
else
{
struct student* p=*HL;
while(p->next!=NULL)
p=p->next;
p->next=newp;
}
}
printf(“储存完毕\n”);
}
void findlist(struct student *HL)
{
char num1[12];
char name1[20];
int i=0,choice;
printf(“===========================\n”);
printf(“1、按学号查询\n”);
printf(“2、按姓名查询\n”);
printf(“===========================\n”);
printf(“ 请选择: “);
scanf(“%d”,&choice);
if (choice==1)
{
printf(“请输入要查找学生的学号:”);
scanf(“%s”,num1);
while(HL!=NULL)
{
if(strcmp(num1,HL->num)==0)
{
printf(“%s ,%s ,%s ,%d
\n”,HL->num,HL->name,HL->sex,HL->score);
break;
}
else HL=HL->next;
}
if(HL==NULL)
printf(“无该生信息\n”);
}
else if (choice==2)
{
printf(“请输入要查找学生的姓名:”);
scanf(“%s”,name1);
while(HL!=NULL)
{
if(strcmp(name1,HL->name)==0)
{
printf(“%s ,%s ,%s ,%d
\n”,HL->num,HL->name,HL->sex,HL->score);
break;
}
else HL=HL->next;
}
if(HL==NULL)
printf(“无该生信息”);
}
}
void printlist(struct student *HL)
{
if(HL==NULL)
printf(“无信息,请先存储\n”);
while(HL!=NULL)
{
printf(“%s ,%s ,%s ,%d “,HL->num,HL-
>name,HL->sex,HL->score);
HL=HL->next;
printf(“\n”);
}
}
void main()
{
int c;
struct student *SHL;
// HL=0x00431b90;
SHL=NULL;
loop:
switch(menu_s())
{
case 1:
printf(“**************************************\n”);
printf(“ 学生信息库的建
立 \n”);
printf(“***************************************\n”);
creat(&SHL) ;
break;
case 2:
printf(“**************************************\n”);
printf(“插入学生信息\n”);
addlist(&SHL);
break;
case 3:
printf(“**************************************\n”);
printf(“查询学生信息\n”);
printf(“**************************************\n”);
findlist(SHL);
break;
case 4:
printf(“**************************************\n”);
printf(“删除学生信息\n”);
printf(“**************************************\n”);
dellist(&SHL);
break;
case 5:
printf(“**************************************\n”);
printf(“输出所有学生信息\n”);
printf(“**************************************\n”);
printlist(SHL);
break;
}
printf(“按1继续,按2退出”);
scanf(“%d”,&c);
if(c==1)
goto loop;
else
printf(“再见!\n”);
exit(1);
}
>> 本文固定链接: http://www.vcgood.com/archives/2943