关于C语言课题设计(学生信息管理系统)
功能要求及说明1数据录入功能
2查询功能
3删除功能
4排序功能
答案:#include"stdio.h"
#include"stdlib.h"
#include"string.h"
#include"conio.h"
typedef struct Student
{char name[20];
char num[20];
int score;
Student *next;
}student;
student *creat()
{student *head;student *p,*s;int flag=1;
head=(student*)malloc(sizeof(student));p=(student*)malloc(sizeof(student));
head->next=p;
if(!p){printf("失败");return 0;}
printf("输入姓名:");gets(p->name);
printf("输入学号:");scanf("%s",p->num);
printf("输入成绩:");scanf("%d",&p->score);
while(flag)
{s=(student*)malloc(sizeof(student));
getchar();
printf("输入姓名:(输入0退出):");gets(s->name);
if(!strcmp(s->name,"0")){flag=0;break;}
printf("输入学号:");scanf("%s",s->num);
printf("输入成绩:");scanf("%d",&s->score);
p->next=s;
p=s;
}
p->next=NULL;
return head;
}
void listprint(student *head)
{student *p;
p=head->next;
while(p)
{printf("姓名:%s\
学号:%s\
成绩:%d\
",p->name,p->num,p->score);
p=p->next;
}
}
void listfind(student *head,char str[])
{student *p;p=head->next;
if(p)
{
while(p&&(strcmp(p->name,str)&&strcmp(p->num ,str)))
p=p->next;
if(p){ printf("姓名:%s\
学号:%s\
成绩:%d\
",p->name,p->num,p->score);}
else printf("没有该学生信息!\
");
}
}
void listadd(student *head)
{student *p,*s;int flag=1;
p=head->next;
while(p->next!=NULL)p=p->next;
while(flag)
{s=(student*)malloc(sizeof(student));
printf("输入姓名:(输入0退出):");gets(s->name);
if(!strcmp(s->name,"0")){ flag=0;break;}
printf("输入学号:");scanf("%s",s->num);
printf("输入成绩:");scanf("%d",&s->score);
p->next=s;
p=s;
getchar();
}
p->next=NULL;
}
void listdel(student *head,char str[])
{student *p1,*p2;p2=head;
p1=head->next;
while(p1&&(strcmp(p1->name,str)&&strcmp(p1->num ,str)))
{p2=p1;p1=p1->next;}
if(!p1)printf("\
\
\
\
\\t
没有该学生的信息\
");
else {p2->next=p1->next;printf("\
\
\
\
\
\\t\\t\\t\\t
删除成功!\
\
");free(p1);}
}
void listsort(student *head)
{student *p1,*p2,*p3,*p4,*temp;temp=(student *)malloc(sizeof(student));
p1=p2=head->next;
for(;p1!=NULL;p1=p1->next)
{p2=p3=p1;p4=p3;
for(;p3!=NULL;p3=p3->next)
{
if(p3->score>p4->score){p4=p3;}
}strcpy(temp->name,p2->name);strcpy(temp->num,p2->num);temp->score=p2->score;
strcpy(p2->name,p4->name);strcpy(p2->num,p4->num);p2->score=p4->score;
strcpy(p4->name,temp->name);strcpy(p4->num,temp->num);p4->score=temp->score;}}
void menu()
{printf("\
\
\
\
\\t\\t\\t菜单\
\\t\\t\\t\
\\t\\t\\t请选择\
\\t\\t\\t********************\
\\t\\t\\t*1创建资料\
\\t\\t\\t*2查找\
\\t\\t\\t*3显示全部\
\\t\\t\\t*4添加\
\\t\\t\\t*5删除\
\\t\\t\\t*6排名\
\\t\\t\\t*7退出\
\\t\\t\\t********************\
");
}
int main()
{student *head;char str[20],ch; int count=0;
while(1)
{menu();ch=getch();
system("cls");
if(ch!='1'&&ch!='7'&&count==0){printf("\
\
\
\
\
\
\\t\\t\\t
你还没有创建资料\
");printf("\
\\t\\t
按任意键回主菜单\
");getch();system("cls");continue;}
else count=1;
switch(ch)
{case '1':head=creat();break;
case '2':printf("输入要查找的姓名或学号:");gets(str);listfind(head,str);break;
case '3':listprint(head);break;
case '4':listadd(head);break;
case '5':printf("输入要删除的姓名或学号:");gets(str);listdel(head,str);break;
case '6':listsort(head);listprint(head);break;
case '7':printf("\
\
\
\
\
\
\\t\\t\\t
谢谢使用\
\
\
\
\
");getchar();return 0;
}printf("\
\\t\\t
按任意键回主菜单\
");getch();system("cls");
}
}