链表
cc-test08-1链表的输入与输出 |
|||
|
|||
description |
|||
读入学生的信息:学号(8位整数)、姓名(20个字符)、性别(1个字符)3门课的成绩(2位小数)。计算每个学生的平均成绩(2位小数),总成绩(2位小数)信息。 要求用函数实现部分子功能,要求用链表完成数据的存储。 |
|||
input |
|||
输入数据有1组,每组有n个学生信息。 |
|||
output |
|||
输出按总成绩从高到低排序输出,每行1个学生信息,各信息之间用2个空格分隔。 |
|||
sample_input |
|||
5 20140101 Li Ming M 85 90 92 20140202 Zhao Li F 98 78 88 20140013 Qiao En M 92.5 85.6 78.5 20140404 Tian Ya M 88.5 68.6 94 20140015 Lu Yao M 89.4 86.5 88 |
|||
sample_output |
|||
20140101 Li Ming M 85.00 90.00 92.00 89.00 267.00 20140202 Zhao Li F 98.00 78.00 88.00 88.00 264.00 20140015 Lu Yao M 89.40 86.50 88.00 87.97 263.90 20140013 Qiao En M 92.50 85.60 78.50 85.53 256.60 20140404 Tian Ya M 88.50 68.60 94.00 83.70 251.10 |
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct student
{
int sno;
char name[20];
char gender;
float grade[3];
float sum;
float avg;
struct student *next;
};
typedef struct student stu_node;
void output(stu_node *phead)
{
stu_node *p=phead->next;
while(p!=NULL)
{
printf("%d %s %c %.2f %.2f %.2f %.2f %.2f\n",p->sno,p->name,p->gender,p->grade[0],p->grade[1],p->grade[2],p->avg,p->sum);
p=p->next;
}
}
stu_node *create(int n)
{
int i=0;
stu_node *phead=(stu_node *)malloc(sizeof(stu_node));
stu_node *pnew, *ptail;
if(phead!=NULL)
{
ptail=phead;
while(i<n)
{
pnew=(stu_node *)malloc(sizeof(stu_node));
if(pnew==NULL) return NULL;
scanf("%d",&pnew->sno);
getchar();
gets(pnew->name);
pnew->gender=getchar();
scanf("%f%f%f",&pnew->grade[0],&pnew->grade[1],&pnew->grade[2]);
pnew->sum=pnew->grade[0]+pnew->grade[1]+pnew->grade[2];
pnew->avg=(pnew->sum)/3;
ptail->next=pnew;
ptail=pnew;
pnew->next=NULL;
i++;
}
}
else
return NULL;
return phead;
}
void sort(stu_node *phead)
{
stu_node *p,*q,*max;
stu_node temp;
int i;
p=phead->next;
while(p)
{
max=p;
q=p->next;
while(q)
{
if(q->sum >max->sum) max=q;
q=q->next;
}
if(max!=p)
{
temp.sno=max->sno;
max->sno=p->sno;
p->sno=temp.sno;
strcpy(temp.name,max->name);
strcpy(max->name,p->name);
strcpy(p->name,temp.name);
temp.gender =max->gender;
max->gender =p->gender;
p->gender= temp.gender;
temp.avg =max->avg;
max->avg =p->avg;
p->avg= temp.avg;
temp.sum =max->sum;
max->sum =p->sum;
p->sum= temp.sum;
for(i=0;i<3;i++)
{
temp.grade[i]=p->grade[i];
p->grade[i]=max->grade[i];
max->grade[i]=temp.grade[i];
}
}
p=p->next;
}
}
int main()
{
int n;
scanf("%d",&n);
stu_node *s=create(n);
sort(s);
output(s);
return 0;
}