回复 2楼 qq1023569223
搞定了,但是不知道哪错了,能帮我看看吗?
# include <stdio.h>
# include <malloc.h>
# define NULLL 0
# define LEN sizeof(struct student)
struct sco
{
float A;
float B;
float C;
}score;
struct student
{
long num;
char name[20];
char sex;
struct sco score;
struct student * next;
};
int n;
/*n为全局变量,本文件模块中各函数均可使用它*/
struct student *creat(void)
/*定义函数,此函数带回一个指向链表头的指针*/
{
struct student * head;
struct student *p1,*p2;
n=0;
p1=p2=(struct student *) malloc(LEN);/*开辟一个新单元*/
scanf("%ld,%s,%c,%f,%f,%f",&p1->num,&p1->name,&p1->sex,&(*p1).score.A,&(*p1).score.B,&(*p1).score.C);
head=NULLL;
while(p1->num!=0)
{
n=n+1;
if(n==1) head=p1;
else p2=p1;
p1=(struct student *) malloc(LEN);
scanf("%ld,%s,%c,%f,%f,%f",&p1->num,&p1->name,&p1->sex,&(*p1).score.A,&(*p1).score.B,&(*p1).score.C);
}
p2->next=NULLL;
return(head);
}
void print(struct student *head)
/*输出链表的函数*/
{
float z;
struct student *p;
printf("\n学生总人数是:%d\n",n);
printf("NO.
Name
sex
A
B
C
Z\n");
p=head;
if(head!=NULLL)
do
{
z=(*p).score.A+(*p).score.B+(*p).score.C;
printf("%ld%s%c%4.1f%4.1f%.1f%4.1f\n",p->num,p->name,p->sex,(*p).score.A,(*p).score.B,(*p).score.C,z);
p=p->next;
}while(p!=NULLL);
}
void highest(struct student *head)
/*找出总分最高的学生和学生总人数的函数*/
{
float z1,z2;
struct student *p;
p=head;
for(;head->num!=0;)
{
z1=(*p).score.A+(*p).score.B+(*p).score.C;
z2=(*head).score.A+(*head).score.B+(*head).score.C;
if(z1<=z2)
p=head;
head=head->next;
}
printf("\n总分最高的学生叫:%s\n总分为:%4.1f\n学生总人数为:\n",p->name,z1,n);
}
struct student *hebing(struct student *m1,struct student *m2)
/*合并学生的函数*/
{
struct student * head,*p1,*p2;
head=m1;
for(;m2->num!=0;)
{
p2=m2->next;
m1=head;
for(;m1->num!=0;)
{
p1=m1->next;
if(m2->num<=m1->num)
{
m2->next=head;
head=m2;
}
if(m1->num<m2->num&&m2->num<=p1->num)
{
m1->next=m2; m2->next=p1;
}
if(m1->num<m2->num&&p1->num==0)
{
m1->next=m2;
m2->next=NULLL;
}
m1++;
}
m2=p2;
}
return(head);
}
struct student *del(struct student *head,long num)
/*删除学生的函数*/
{
struct student *p1,*p2;
if(head==NULLL)
{ printf("\n链表是空的!!\n");return head;}
p1=head;
while(num!=p1->num&&p1->next!=NULLL)
{p2=p1;p1=p1->next;}
if(num==p1->num)
{if(p1==head) head=p1->next;
/*找到的是首节点,把第二个节点地址赋给head*/
else p2->next=p1->next;
printf("删除;%ld\n",num);
n=n-1;;
}
else printf("%ld 没有找到!\n",num); /*找不到该结点*/
return(head);
}
struct student * insert(struct student * head,struct student *stu)
/*插入学生的函数*/
{struct student *p0,*p1,*p2;
p1=head;
p0=stu;
if(head==NULLL)
{head=p0;p0->next=NULLL;}
else
{
while((p0->num>p1->num)&&(p1->next!=NULLL))
{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=NULLL;}
}
n=n+1;
return(head);
}
void main()
{
char a;
struct student * head_1,*head_2,stu;
long del_num;
printf("请输入学生信息:\n");
head_1=creat();
printf("\n输入完成,是否输出学生信息? Y or N? ");
scanf("%c",&a);
if(a=='Y'||a=='y')
print(head_1);
printf("是否找出总分最高的学生信息? Y or N ? ");
scanf("%c",&a);
if(a=='Y'||a=='y')
highest(head_1);
printf("\n是否输入另一组学生信息,并与前一组合并? Y or N ?
");
scanf("%c",&a);
if(a=='Y'||a=='y')
{ printf("\n请输入学生信息:\n");
head_2=creat();
head_1=hebing(head_1,head_2);
printf("\n合并完成,是否输出合并后信息? Y or N?");
scanf("%c",&a);
if(a=='Y'||a=='y')
print(head_1);
}
printf("\n输入退学同学的学号");
scanf("%ld",&del_num);
head_1=del(head_1,del_num);
printf("操作完成,是否输出学生信息? Y or N ");
scanf("%c",&a);
if(a=='Y'||a=='y')
print(head_1);
printf("\n输入要插入学生的信息:");
scanf("%ld,%s,%c,%f,%f,%f",&stu.num,&stu.name,&stu.sex,&stu.score.A,&stu.score.B,&stu.score.C);
head_1=insert(head_1,&stu);
printf("\n是否输入插入学生后所有学生的情况? Y or N ");
scanf("%c",&a);
if(a=='Y'||a=='y')
print(head_1);
}