程序代码:
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<string.h>
struct sc
{
int chinese;
int maths;
int english;
};
typedef struct node
{
int num;
char name[20];
struct sc score;
struct node *next;
}st;
int menu()//菜单
{
int choice;
do{
system("cls");
printf("\t1.input the messega about a student\n");
printf("\t2.insect a messega of a new student\n");
printf("\t3.look for the messega\n");
printf("\t4.dellect the messega\n");
printf("\t5.arranging base on the number of learning\n");
printf("\t6.output all the messega\n");
printf("\t7.arranging base on all scores\n");
printf("\t8.exit the system\n");
printf("\tplease input your choice:");
scanf("%d",&choice);
}while(choice>7&&choice<1);
return choice;
}
st *create()//创建链表
{
st *head,*p,*tail;
char c;
head=tail=NULL;
while(c!='n'&&c!='N')
{
p=(st *)malloc(sizeof(st));
p->next=NULL;
printf("\t\tplease input the number of learning:");
scanf("%d",&p->num);
printf("\t\tplease input the name:");
scanf("%s",p->name);
printf("\t\tplease input the score of chinese:");
scanf("%d",&p->score.chinese);
printf("\t\tplease input the score of maths:");
scanf("%d",&p->score.maths);
printf("\t\tplease input the score of english:");
scanf("%d",&p->score.english);
if(head==NULL)
head=tail=p;
else
{
tail->next=p;
tail=p;
}
printf("\t\tcontinue or stop(Y/N):");
scanf("%s",&c);
}
return head;
}
st *arrange(st *head)// 以学号排名
{
st *p,*q;
int t,i=1,j,chinese,maths,english;
char name[20];
p=head;
if(head==NULL)
printf("\t\tNoting to arrange\n");
else
{
do
{
j=1;
while(p->next!=NULL)
{
q=p->next;
if(p->num>=q->num)
{
t=p->num;
p->num=q->num;
q->num=t;
strcpy(name,p->name);
strcpy(p->name,q->name);
strcpy(q->name,name);
chinese=p->score.chinese;
p->score.chinese=q->score.chinese;
q->score.chinese=chinese;
maths=p->score.maths;
p->score.maths=q->score.maths;
q->score.maths=maths;
english=p->score.english;
p->score.english=q->score.english;
q->score.english=english;
}
p=q;
j++;
}
p=head;
i++;
}while(i!=j);
}
return head;
}
st *arrangeall(st *head)//以总分排名
{
st *p,*q;
int t,i=1,j,chinese,maths,english;
char name[20];
p=head;
if(head==NULL)
printf("\t\tNoting to arrange\n");
else
{
do
{
j=1;
while(p->next!=NULL)
{
q=p->next;
if(p->score.chinese+p->score.maths+p->score.english<q->score.chinese+q->score.maths+q->score.english)
{
t=p->num;
p->num=q->num;
q->num=t;
strcpy(name,p->name);
strcpy(p->name,q->name);
strcpy(q->name,name);
chinese=p->score.chinese;
p->score.chinese=q->score.chinese;
q->score.chinese=chinese;
maths=p->score.maths;
p->score.maths=q->score.maths;
q->score.maths=maths;
english=p->score.english;
p->score.english=q->score.english;
q->score.english=english;
}
p=q;
j++;
}
p=head;
i++;
}while(i!=j);
}
return head;
}
st *insect(st *head,st *t)//插入学生的信息
{
st *p,*q;
p=head;
if(head==NULL)
printf("\tThe list is empty\n");
else
{
if(t->num==p->num)
{
head=t;
t->next=p;
}
else
{
while(p->next!=NULL)
{
q=p->next;
if(p->num<=t->num&&q->num>=t->num)
{
t->next=q;
p->next=t;
}
p=q;
}
p->next=t;
}
}
return head;
}
void output(st *head)//输出所以学生的信息
{
st *p;
int i=0;
float sum1=0,sum2=0,sum3=0;
p=head;
if(head==NULL)
{
printf("\tThere is nothing \n");
return;
}
else
{
printf("\tnumber name chinese maths english allscores\n");
while(p)
{
printf("\t %d ",p->num);
printf(" %s ",p->name);
printf(" %d ",p->score.chinese);
printf(" %d ",p->score.maths);
printf(" %d ",p->score.english);
printf("%6d",p->score.chinese+p->score.maths+p->score.english);
printf("\n");
p=p->next;
}
p=head;
while(p)// avrege scores
{
sum1+= p->score.chinese;
sum2+=p->score.maths;
sum3+=p->score.english;
p=p->next;
i++;
}
printf("\tarvege %.2f %.2f %.2f %.2f \n",sum1/i,sum2/i,sum3/i,(sum1+sum2+sum3)/i);
}
system("pause");
}
st *dellect(st *head,st *d)//删除学生的信息
{
st *p,*q;
char c;
p=head;
if(!(strcmp(p->name,d->name)))
{
head=p->next;
free(p);
}
else
{
while((strcmp(p->name,d->name))&&p->next!=NULL)
{
q=p;
p=q->next;
}
if(!(strcmp(p->name,d->name)))
{
printf("do you want to delete %S ?(Y/N):",p->name);
scanf("%s",&c);
if(c=='y'||c=='Y')
{
q->next=p->next;
free(p);
}
}
else
printf("\tThere isn't this name\n");
}
return head;
}
void find(st *head,st *s)//查找学生的信息
{
st *p,*q;
p=head;
if(head==NULL)
printf("\tThe list is empty\n");
else
{
while((strcmp(p->name,s->name))!=0&&p->next!=NULL)
{
q=p;
p=q->next;
}
if((strcmp(p->name,s->name))==0)
{
printf("\t %d ",p->num);
printf(" %s ",p->name);
printf(" chinese=%d ",p->score.chinese);
printf(" maths=%d ",p->score.maths);
printf(" english=%d ",p->score.english);
printf("all=%d",p->score.chinese+p->score.maths+p->score.english);
printf("\n");
}
else
printf("\tThe name is missing\n");
}
system("pause");
}
void main()//主函数
{
st *head,*t,*s,*d;
int choice;
for(;;)
{
choice=menu();
switch(choice)
{
case 1:
head=create();
break;
case 2:
t=(st *)malloc(sizeof(st));
t->next=NULL;
printf("\t\tplease input the number of learning:");
scanf("%d",&t->num);
printf("\t\tplease input the name:");
scanf("%s",t->name);
printf("\t\tplease input the score of chinese:");
scanf("%d",&t->score.chinese);
printf("\t\tplease input the score of maths:");
scanf("%d",&t->score.maths);
printf("\t\tplease input the score of english:");
scanf("%d",&t->score.english);
head=insect(head,t);
break;
case 4:
d=(st *)malloc(sizeof(st));
d->next=NULL;
printf("\twhice student do you want to dellect?please input the name:");
scanf("%s",d->name);
head=dellect(head,d);
break;
case 3:
s=(st *)malloc(sizeof(st));
s->next=NULL;
printf("\twhice student do you want to look for?please input the name:");
scanf("%s",s->name);
find(head,s);
break;
case 5:
head=arrange(head);//number
break;
case 6:
output(head);
break;
case 7:
head=arrangeall(head);//score
break;
case 8:
printf("\t\tThank you,goodbye\n");
exit(0);
}
}
}
类似于这个?