链表的问题,这个怎么改呢?
#include<malloc.h>
#include <stdio.h>
#include<string.h>
#define len sizeof(struct student)
#define NULL 0
struct student{
char num[20];
char name[20];
float score[3];
float zongfen;
struct student *next;
};
int count=0;
struct student * input();
struct student * inset(struct student *head,struct student *stu);
struct student * deleted(struct student *head,char *name);
void search(struct student *head,char *name);
struct student * paixu(struct student *head,int count);
struct student * jisuan(struct student *head);
int main(void)
{
struct student *head,*p;
char num[20];
char name[20];
int choice,i;
do{
printf("1:input() 2:inset() 3:deleted() 4:search() 5:paixu() 6:jisuan()");
printf("enter chioce:");
scanf("%d",&choice);
switch(choice){
case 1:
head=input();
break;
case 2:
printf("input num and name:");
scanf("%d%s",num,name);
p= (struct student * ) malloc(len);
strcpy(p->num,num);
strcpy(p->name,name);
printf("enter shuxu english cyuyan:");
for(i=0;i<3;i++)
scanf("%s",p->score[i]);
head=inset(head,p);
break;
case 3:
printf("input name:");
scanf("%s",name);
head=deleted(head,name);
break;
case 4:
printf("input name:");
scanf("%s",name);
search(head,name);
break;
case 5:
head=paixu(head,count);
break;
case 6:
head=jisuan(head);
break;
case 0:
break;
}
}while(choice!=0);
return 0;
}
struct student * input()
{struct student *head,*p1,*p2;
int i;
p1=p2=(struct student * ) malloc(len);
printf("enter num and name:");
scanf("%s%s",p1->num,p1->name);
printf("enter shuxu english cyuyan:");
for(i=0;i<3;i++)
scanf("%s",p1->score[i]);
head=NULL;
while(p1->num!=0){
count=count+1;
if(count==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=p2=(struct student * ) malloc(len);
printf("enter num and name:");
scanf("%s%s",p1->num,p1->name);
printf("enter shuxu english cyuyan:");
for(i=0;i<3;i++)
scanf("%s",p1->score[i]);}
p2->next=NULL;
return head;
}
struct student * insert(struct student *head,struct student *stud)
{
struct student *p0,*p1,*p2;
p1=head;
p0=stud;
if(head==NULL){
head=p0;
p0->next=NULL;}
else
{
while((p0->num>p1->num)&&(p1->next!=NULL))
{
p2=p1;
p1=p1->next ;}
if(p0->num <=p1->num )
{
if(head==p1)
head=p0;
else
p2->next=p0;
p0->next=p1;}
else
{
p1->next =p0;
p0->next =NULL;}
count=count+1;
return (head);
}return 0;}
struct student * deleted(struct student *head,char *name)
{
struct student *p1,*p2;
if(head==NULL)
{
printf("not list");
return head;
}
p1=head;
while((name!=p1->name)&&(p1->next!=NULL))
{
p2=p1;
p1=p1->next ;}
if(name==p1->name )
{
if(head==p1)
head=p1->next;
else
p2->next=p1->next;
printf("deletr:%s\n",name);}
else
printf("%s not been found\n",name);
count=count-1;
return head;}
void search(struct student *head,char *name)
{
struct student *p1;char *p3;
p3=name;
int flag,i;
p1=head;
flag=1;
for(p1=head;p1;p1->next )
if(p1->name==p3)
{
flag=1;
break;
}
else
printf("not found");
if(flag)
{
printf("%s%20s\n",p1->num,p1->name);
for(i=0;i<3;i++)
printf("%f\n",p1->score[i]);
}}
struct student * paixu(struct student *head,int count)
{
void shuxuepx(struct student *head,int count);
void englishpx(struct student *head,int count);
void cyuyanpx(struct student *head,int count);
void zongfenpx(struct student *head,int count);
int choice;
do{
printf("1:shuxuepx 2:englishpx 3:cyuyanpx 4:zongfenpx 0:退出");
scanf("%d",&choice);
switch(choice){
case 1:
shuxuepx(head,count);
break;
case 2:
englishpx(head,count);
break;
case 3:
cyuyanpx(head,count);
break;
case 4:
zongfenpx(head,count);
break;}
}while(choice!=0);
return 0;
}
void shuxuepx(struct student *head,int count)
{
struct student *p1,*p2;
int i,j;
float temp;
for(p1=head,i=1;i<count;p1=p1->next ,count++){
p2=p1;
for(p1=p1->next,j=i+1;j<=count;p1->next,j++)
if(p1->score[0]<p2->score[0])
p2->score[0]=p1->score[0];
temp=p2->score[0];
p2->score[0]=head->score[0];
head->score[0]=temp;
head=head->next;}
}
void englishpx(struct student *head,int count)
{
struct student *p1,*p2;
int i,j;
float temp;
for(p1=head,i=1;i<count;p1=p1->next ,count++){
p2=p1;
for(p1=p1->next,j=i+1;j<=count;p1->next,j++)
if(p1->score[1]<p2->score[1])
p2->score[1]=p1->score[1];
temp=p2->score[1];
p2->score[1]=head->score[1];
head->score[1]=temp;
head=head->next;
}
}
void cyuyanpx(struct student *head,int count)
{
struct student *p1,*p2;
int i,j;
float temp;
for(p1=head,i=1;i<count;p1=p1->next ,count++){
p2=p1;
for(p1=p1->next,j=i+1;j<=count;p1->next,j++)
if(p1->score[2]<p2->score[2])
p2->score[2]=p1->score[2];
temp=p2->score[2];
p2->score[2]=head->score[2];
head->score[2]=temp;
head=head->next;
}
}
struct student * jisuan(struct student *head)
{
void gerenzongfen(struct student *head);
void syrdkpjf(struct student *head);
int choice;
do{
printf("1:计算个人总分平均分 2:所有人的单科平均分0:退出:");
scanf("%d",&choice);
switch(choice){
case 1:
gerenzongfen(head);
break;
case 2:
syrdkpjf(head);
break;}
}while(choice!=0);
return 0;
}
void gerenzongfen(struct student *head)
{
struct student *p1;
int i;
float sum,averge;
p1=head;
for(p1=head;p1;p1=p1->next )
{
sum=0;
for(i=0;i<3;i++)
sum=sum+p1->score[0];
averge=sum/3;
printf("%s%f%f",p1->name,sum,averge);
}}
void syrdkpjf(struct student *head)
{
void shuxue(struct student *head);
void english(struct student *head);
void cyuyan(struct student *head);
int choice;
do{
printf("1:shuxue 2:english: 3:c yuyan 0:退出");
scanf("%d",&choice);
switch(choice){
case 1:
shuxue(head);
break;
case 2:
english(head);
break;
case 3:
cyuyan(head);
break;}
}while(choice!=0);
}
void shuxue(struct student *head)
{
struct student *p1;
int n=0;
float sum=0;
for(p1=head;p1;p1->next )
{
sum=sum+p1->score[0];
n++;}
printf("shuxue averge%f",sum/n);
}
void english(struct student *head)
{
struct student *p1;
int n=0;
float sum=0;
for(p1=head;p1;p1->next )
{
sum=sum+p1->score[0];
n++;}
printf("english averge%f",sum/n);
}
void cyuyan(struct student *head)
{
struct student *p1;
int n=0;
float sum=0;
for(p1=head;p1;p1->next )
{
sum=sum+p1->score[0];
n++;}
printf("c yuyan averge%f",sum/n);
}