用链表做的,大家来看看排序的时候有没有什么再好一点的方法,谢谢咯
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct student
{
int number;
char name[15];
float Chinese;
float Math;
float English;
float average_grade;
struct student *next;
};
typedef struct student node;
typedef node* Link;
/**********************************输入数据*******************************/
Link creat_list(Link Head)
{
int i=1;
Link point;
Link New;
char choice='y';
Head=(Link)malloc(sizeof(node));
point=Head;
printf("请输入第%d个学生的学号\n",i);
scanf("%d",&Head->number);
printf("请输入第%d个学生的姓名\n",i);
scanf("%s",Head->name);
printf("请输入第%d个学生的语文成绩\n",i);
scanf("%f",&Head->Chinese);
printf("请输入第%d个学生的数学成绩\n",i);
scanf("%f",&Head->Math);
printf("请输入第%d个学生的英语成绩\n",i);
scanf("%f",&Head->English);
Head->average_grade=(Head->Chinese+Head->Math+Head->English)/3;
Head->next=NULL;
point=Head;
while(choice=='y')
{
i++;
New=(Link)malloc(sizeof(node));
printf("请输入第%d个学生的学号\n",i);
scanf("%d",&New->number);
printf("请输入第%d个学生的姓名\n",i);
scanf("%s",New->name);
printf("请输入第%d个学生的语文成绩\n",i);
scanf("%f",&New->Chinese);
printf("请输入第%d个学生的数学成绩\n",i);
scanf("%f",&New->Math);
printf("请输入第%d个学生的英语成绩\n",i);
scanf("%f",&New->English);
New->average_grade=(New->Chinese+New->Math+New->English)/3;
New->next=NULL;
point->next=New;
point=New;
printf("是否继续添加学生成绩,y or n?");
fflush(stdin);
scanf("%c",&choice);
}
return Head;
}
/*******************************添加学生资料******************************/
Link add_list(Link Head)
{
Link New;
Link point;
New=(Link)malloc(sizeof(node));
fflush(stdin);
printf("请输入这个学生的学号\n");
scanf("%d",&New->number);
printf("请输入这个学生的姓名\n");
scanf("%s",New->name);
printf("请输入这个学生的语文成绩\n");
scanf("%f",&New->Chinese);
printf("请输入这个学生的数学成绩\n");
scanf("%f",&New->Math);
printf("请输入这个学生的英语成绩\n");
fflush(stdin);
scanf("%f",&New->English);
New->average_grade=(New->Chinese+New->Math+New->English)/3;
New->next=NULL; //添加在链表最前面
point=Head;
New->next=point;
Head=New;
return Head;
}
/*******************************删除学生资料******************************/
Link reduce_list(Link Head,int student_number)
{
Link point;
Link Back;
point=Head;
while(1)
{
if(point->next==NULL)
{
printf("没有找到这个学号\n");
break;
}
if(Head->number==student_number)
{
Head=point->next;
free(point);
break;
}
Back=point;
point=point->next;
if(point->number==student_number)
{
Back->next=point->next;
free(point);
break;
}
}
return Head;
}
/******************************对学生成绩排序*****************************/
Link compositor_list(Link Head)
{
Link temp;
Link point_1;
Link point_2;
Link point_3;
point_1=Head;
point_2=Head;
point_3=Head;
temp=(Link)malloc(sizeof(node));
while(point_1!=NULL)
{
point_2=point_2->next;
point_3=point_3->next;
while(point_2!=NULL)
{
if(point_1->average_grade<point_2->average_grade)
{
temp->number=point_1->number;
strcpy(temp->name,point_1->name);
temp->Chinese=point_1->Chinese;
temp->Math=point_1->Math;
temp->English=point_1->English;
temp->average_grade=point_1->average_grade;
point_1->number=point_2->number;
strcpy(point_1->name,point_2->name);
point_1->Chinese=point_2->Chinese;
point_1->Math=point_2->Math;
point_1->English=point_2->English;
point_1->average_grade=point_2->average_grade;
point_2->number=temp->number;
strcpy(point_2->name,temp->name);
point_2->Chinese=temp->Chinese;
point_2->Math=temp->Math;
point_2->English=temp->English;
point_2->average_grade=temp->average_grade;
}
point_2=point_2->next;
}
point_2=point_3;
point_1=point_1->next;
}
return Head;
}
/*******************************输入学生信息******************************/
void print_list(Link Head)
{
Link point;
point=Head;
while(point!=NULL)
{
printf(" 学号 :%d\n ",point->number);
printf(" 姓名 :%s\n",point->name);
printf("语文成绩:%7.2f\n ",point->Chinese);
printf("数学成绩:%7.2f\n ",point->Math);
printf("英语成绩:%7.2f\n ",point->English);
printf("平均成绩:%7.2f\n ",point->average_grade);
point=point->next;
}
}
/*********************************释放链表********************************/
void free_list(Link Head)
{
Link point;
while(Head!=NULL)
{
point = Head;
Head=point->next;
free(point);
}
}
/***********************************主函数********************************/
void main()
{
Link Head;
Head=creat_list(Head);
int select;
int student_number;
if(Head==NULL)
{
printf("内存配置失败!\n");
}
else
{
printf("输出学生信息:\n");
print_list(Head);
Head=compositor_list(Head);
printf("输入排序之后的学生信息:\n");
print_list(Head);
while(1)
{
printf("\n是否添加和删除学生:\n");
printf("1.添加学生信息\n");
printf("2.删除学生信息\n");
printf("3.既不删除学生信息也不添加学生信息\n");
scanf("%d",&select);
switch(select)
{
case 1: Head=add_list(Head);
break;
case 2: printf("请输入学生的学号:");
fflush(stdin);
scanf("%d",&student_number);
Head=reduce_list(Head,student_number);
break;
case 3: break;
default:printf("输入错误,请重新输入!\n");
}
if(select==3)
break;
}
compositor_list(Head);
printf("经过添加删除排序之后的学生信息!\n");
print_list(Head);
free_list(Head);
}
}
----------------解决方案--------------------------------------------------------
大哥,这么长怎么看啊,至少也要说清楚太约是哪个地方有BUG吧
----------------解决方案--------------------------------------------------------
晕了,我是说代码还能不能优化一下
----------------解决方案--------------------------------------------------------