跪求C语言大作业学生管理系统的程序
发布网友
发布时间:2022-05-26 02:55
我来回答
共1个回答
热心网友
时间:2024-05-12 19:50
#include<stdio.h>
#include<malloc.h>
#include<string.h>
#include<stdlib.h>
#define NULL 0
#define LEN sizeof(struct student)
struct student
{
int num;
char name[10];
int score;
struct student *next;
};
int n;
struct student *creat()
{
struct student *head;
struct student *p1,*p2;
p1=p2=(struct student *)malloc(LEN);
printf("请输入数据:\n");
printf("-学号---姓名---成绩-\n");
scanf("%d%s%d",&p1->num,p1->name,&p1->score);
head=NULL;
n=0;
while(p1->num!=NULL)
{
n=n+1;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
scanf("%d%s%d",&p1->num,p1->name,&p1->score);
}
p2->next=NULL;
return(head);
}
void print(struct student *head)
{
struct student *p;
p=head;
if(head!=NULL)
{
printf("-学号---姓名---成绩-\n");
while(p!=NULL)
{
printf("%d %s %d\n",p->num,p->name,p->score);
p=p->next;
}
}
else printf("没有数据 !\n");
}
struct student *del(int num,struct student *head)
{
int a=0;
struct student *p1,*p2;
do
{
p1=head;
if(head==NULL)
{
printf("没有数据 !\n");
goto end;
}
while(p1->num!=num&&p1->next!=NULL)
{
p2=p1;p1=p1->next;
}
if(p1->num==num)
{
if(p1==head) head=p1->next;
else p2->next=p1->next;
printf("删除记录学号为N0.%d\n",num);
n=n-1;
a++;
}
}while(p1->next!=NULL);
if(a==0)
printf("学号未找到 !\n");
end:
return(head);
}
struct student *add(int num,char name[],int score,struct student *head)
{
struct student *p0,*p1,*p2;
p0=(struct student *)malloc(LEN);
p0->num=num,strcpy(p0->name,name),p0->score=score;
p1=head;
if(num==0)
return(head);
if(head==NULL)
{
head=p0;
p0->next=NULL;
}
while(p0->num>p1->num&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(p0->num<=p1->num)
{
if(p1==head) head=p0;
else p2->next=p0;
p0->next=p1;
}
else
{
p1->next=p0;
p0->next=NULL;
}
printf("增加记录学号为:%d\n",num);
n=n+1;
return(head);
}
void num(struct student *head)
{
int num,a=0;
struct student *p;
printf("------请输入 \"0\" 结束输入--------\n");
do
{
p=head;
printf("请输入要查找的学号:");
scanf("%d",&num);
if(num!=0)
printf("-学号---姓名---成绩-\n");
while(p!=NULL)
{
if(p->num==num)
{
printf("%d %s %d\n",p->num,p->name,p->score);
a++;
}
p=p->next;
}
if(a==0&num!=0)
printf("学号没找到!\n");
a=0;
}while(num!=0);
system("cls");
}
void name(struct student *head)
{
char name[10],a=0;
struct student *p;
printf("------请输入 \"0\" 结束输入------\n");
do
{
p=head;
printf("请输入要查找的名字:");
scanf("%s",name);
if(name[0]!='0')
printf("-学号---姓名---成绩-\n");
while(p!=NULL)
{
if(strcmp(p->name,name)==0)
{
printf("%d %s %d\n",p->num,p->name,p->score);
a++;
}
p=p->next;
}
if(a==0&&name[0]!='0')
printf("名字未找到 !\n");
a=0;
}while(name[0]!='0');
system("cls");
}
void search(struct student *head)
{
int a;
struct student *p;
p=head;
do
{
printf("1.按学号查找\n2.按名字查找\n3.退出\n");
scanf("%d",&a);
system("cls");
switch(a)
{
case 1:num(p);break;
case 2:name(p);break;
}
}while(a!=3);
}
struct student *sort(struct student *head)
{
struct student *p1,*p2;
int i,j;
int num0,score0;
char string[10];
for(i=0;i<n-1;i++)
{
p1=head;
for(j=n-1;j>0;j--)
while(p1->next!=NULL)
{
p2=p1;p1=p1->next;
if(p2->score>p1->score)
{
num0=p1->num;
p1->num=p2->num;
p2->num=num0;
strcpy(string,p1->name);
strcpy(p1->name,p2->name);
strcpy(p2->name,string);
score0=p1->score;
p1->score=p2->score;
p2->score=score0;
}
}
}
return(head);
}
void main()
{
struct student *head=NULL;
int num,score,a;
char name[10];
do
{
printf("1.创建记录\n2.删除记录\n3.增加记录\n4.查找记录\n5按成绩排序\n6.显示当前记录\n7.退出\n");
scanf("%d",&a);
system("cls");
switch(a)
{
case 1:printf("------请输入 \"0 0 0\" 结束输入------\n");head=creat();print(head);getch();system("cls");break;
case 2:printf("------请输入 \"0\" 结束输入--------\n");do{printf("请输入要删除记录的学号:");scanf("%d",&num);head=del(num,head);print(head);}while(num!=0);system("cls");break;
case 3:printf("------请输入 \"0 0 0\" 结束输入------\n");do{printf("请输入要增加的记录数据:\n");printf("-学号---姓名---成绩-\n");scanf("%d%s%d",&num,name,&score);head=add(num,name,score,head);print(head);}while(num!=0);system("cls");break;
case 4:search(head);break;
case 5:head=sort(head);print(head);getch();system("cls");break;
case 6:print(head);getch();system("cls");break;
}
}while(a!=7);