实验作业:用C语言设计函数输出对n个学生三门课程成绩总分排名。
一个小小的实验作业,感觉挺有意思的,这是我个人设计的代码,代码也不算复杂,主要就是涉及到链表的使用,很简单,代码如下:
#include<stdio.h>
#include<stdlib.h>
struct node
{
char name[50];
char eng[20];
int math1;
int math2;
int math3;
int sum=0;
int ran;
struct node *next;
}*head,*q,*t;
void my_sort(struct node *head)//学生成绩排序
{
struct node *cur, *tail,*tt;
cur=head;
tail=NULL;
if(cur==NULL||cur->next==NULL)return;
while(cur!=tail)
{
while(cur->next!=tail)
{
if(cur->sum>cur->next->sum)
{
cur->ran=cur->ran-1;
}
cur=cur->next;
}
if(cur->next==NULL)tt=cur;
tail=cur;
cur=head;
}
while(cur!=tt)
{
if(tt->sum>cur->sum)tt->ran--;
cur=cur->next;
}
}
void input(int n)//输入信息
{
struct node *p;
p=(struct node *)malloc(sizeof(struct node));
p->next= NULL;
scanf("%s%s%d%d%d",p->name,p->eng,&p->math1,&p->math2,&p->math3);
p->sum=p->math1+p->math2+p->math3;
p->ran=n;
if(head==NULL)head=p;
else q->next=p;
q=p;
}
void output()//输出信息
{
t=head;
while(t!=NULL)
{
printf("姓名:%s\n学号:%s\n总成绩:%d\n排名:%d\n",t->name,t->eng,t->sum,t->ran);
t=t->next;
}
}
int main()
{
head=NULL;
printf("请输入有几名同学:\n");
int n;
scanf("%d",&n);
printf("请分别输入姓名,学号,各科成绩:\n");
for(int i=0;i<n;i++)
{
input(n);
}
my_sort(head);
printf("学生成绩及排名如下:\n");
output();
}