1,建立一张学生成绩表,每个学生包含“学号、姓名、性别、语文、数学、英语、平均分”
2,将该表中所有信息按平均分降序排列
3,按学号查找某学生所有成绩
4,插入某学生成绩在合适位置,不影响原来排序
(用链表实现)
https://blog.csdn.net/sdwujk160507140150/article/details/79764014
这是一个简化版的链表构建问题,主要考察增查,当然删是肯定的,更好前段时间写了一个链表,然后修改一下(然后发现和差不多重写差不多)
这个是在增加结点的时候就已经帮你按照平均成绩的降序排列了。
代码有点长,时间有点紧,所有内存信息泄露等就没怎么管了,你也可以先清除数据再删结点,提高安全信息,注释也没怎么敲,有什么疑问或者其他可以私信
或者留言,欢迎一起讨论。
需要注意的是采用scanf输入,是严格要求输入形式和设置形式一致,所以注意输入按照提醒去,代码很简单应该看一下差不多,不懂的变量自己查英文字典
还有就是输入和输出用的是%f,如果想精确少一点可以自己改,如果姓名和性别是汉字输入,那就需要注意一下了,我的编辑环境采用的是UTF-8,一般三个
字节对应一个汉字(有兴趣可以去查一下相关资料)
时间紧也就没怎么注意,基本需求应该还是可以满足的,其他还有什么可以私信或者留言
下面是代码
链表的头文件 list.h
#ifndef __LIST_H__
#define __LIST_H__
struct node
{
unsigned int student_num;
char name[20];
char gender[6];
float chinese;
float math;
float english;
float average;
struct node * next;
};
typedef struct node Node;
struct hnode
{
Node * first;
unsigned class_num;
};
typedef struct hnode Hnode;
Hnode * create_list();
Hnode * increase_element(Hnode *h,Node *element);
void average_score(Node *element);
void destory_list(Hnode *h);
void traverse_list(Hnode*h);
Node * search_list(Hnode *h,unsigned int num);
#endif
然后是list.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"list.h"
Hnode * create_list()
{
Hnode * h=(Hnode *)malloc(sizeof(Hnode));
h->class_num=0;
h->first=NULL;
return h;
}
Hnode * increase_element(Hnode *h,Node *element)
{
if(NULL==element)
{
return h;
}
Node *p=(Node *)malloc(sizeof(Node));
Node *q=NULL;
Node *before=NULL;
int i=0;
p->average=element->average;
p->chinese=element->chinese;
p->english=element->english;
p->math=element->math;
p->next=NULL;
p->student_num=element->student_num;
strcpy(p->gender,element->gender);
strcpy(p->name,element->name);
printf("succeed\n");
if(NULL==h->first)
{
h->first=p;
h->class_num++;
return h;
}
else
{
q=h->first;
while(q)
{
if(p->average<q->average)
{
before=q;
q=q->next;
i++;
}
else
{
break;
}
}
if(0==i)
{
h->first=p;
p->next=q;
h->class_num++;
return h;
}
else
{
before->next=p;
p->next=q;
h->class_num++;
return h;
}
}
return h;
}
void average_score(Node *element)
{
element->average=(element->chinese+element->english+element->math)/3;
}
Node * search_list(Hnode *h,unsigned int num)
{
if(NULL==h->first)
{
return NULL;
}
Node *end=h->first;
while(end)
{
if(num!=end->student_num)
{
end=end->next;
}
else
{
break;
}
}
return end;
}
void destory_list(Hnode *h)
{
if(NULL==h->first)
{
return;
}
Node *p=h->first;
while(p)
{
p=h->first->next;
free(h->first);
h->first=p;
}
free(h);
}
void traverse_list(Hnode*h)
{
Node *p=h->first;
while(p)
{
printf("%u %s %s %f %f %f %f\n",p->student_num,p->name,p->gender,p->chinese,p->math,p->english,p->average);
p=p->next;
}
}
最后就是man.c了,这是按照基础需求写的,你可以通过调用我的list.c自己弄应该精简版,也可以按照实际需求加
#include<stdio.h>
#include"list.h"
int main()
{
Hnode *h=create_list();
Node student;
Node* search=NULL;
int k=0;
printf("请分别输入学生的学号 姓名 性别 语文成绩 数学成绩 英语成绩\n");
scanf("%d%s%s%f%f%f",&student.student_num,student.name,student.gender,&student.chinese,&student.math,&student.english);
average_score(&student);
h=increase_element(h,&student);
while(1)
{
printf("请问是否输入完成,完成为1,继续为0\n");
scanf("%d",&k);
if(1==k)
{
break;
}
scanf("%u%s%s%f%f%f",&student.student_num,student.name,student.gender,&student.chinese,&student.math,&student.english);
average_score(&student);
h=increase_element(h,&student);
}
printf("这是输入信息\n");
traverse_list(h);
while(1)
{
printf("请问是否需要查找,不需要为-1,需要请输入学号\n");
scanf("%d",&k);
if(-1==k)
{
break;
}
search=search_list(h,k);
if(NULL==search)
{
printf("没有这个学生\n");
break;
}
printf("查找的学生的学号是:%u\n",search->student_num);
printf("查找的学生为:%s\n",search->name);
printf("性别为:%s\n",search->gender);
printf("语文成绩为:%f 数学成绩为:%f 英语成绩为:%f\n",search->chinese,search->math,search->english);
printf("平均成绩是:%f\n",search->average);
}
destory_list(h);
return 0;
}
结果
编译环境:gcc 编辑环境:sources insight