由键盘输入一个班(全班30人)学生的学号,姓名,M门课程的成绩,分别统计下列内容:
1。统计每个学生的总分和平均分,按总分由大到小排出名次。
2。打印出成绩在全班平均分之上的学生名单
(须用链表结构实现)
=================================================================================
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define M 3 /*把科目定为3.可以修改*/
#define P 30 /*用于测试时可以改小*/
typedef struct s{
int id;
float all,ave,score[M];
char name[10]; /*名字10个应该够了.*/
struct s *next;
}student;
student* createStu() /*建结点*/
{
int i;
student *s=(student *)malloc(sizeof(student));
printf("Please input the id/n");
scanf("%d",&s->id);
printf("Please input the name/n");
fflush(stdin);
gets(s->name);
printf("Please input the score/n");
i=0;s->all=0;
while(i<M)
{
scanf("%f",&s->score[i]);
s->all+=s->score[i++];
if(i==M)s->ave=s->all/i;
}
s->next=0;
return s;
}
student* insertLink(student *&head,student *node)
{
student *p=head,*q=p;
if(!head) /*插入头*/
{
head=node;
return head;
}
if(head->all<node->all) /*插入头前*/
{
node->next=head;
head=node;
return head;
}
while(p&&(p->all>=node->all))
{
q=p;p=p->next;
}
if(!p)p=node; /*插入尾*/
else /*插入中间*/
{
q->next=node;
node->next=p;
q=node;
}
return head;
}
void print(student *head)
{
student *p=head;
if(!p)return ;
printf("/nshow these students with other information/n");
while(p)
{
printf("ID:%d name:%s ALL:%f AVERAGE:%f/n",p->id,p->name,p->all,p->ave);
p=p->next;
}
}
void printUP(student *head)
{
float aveall,allall=0;
student *p=head;
if(!p)return ;
while(p)
{
allall+=p->all;
p=p->next;
}
aveall=allall/P;
p=head;
printf("/nthese students up the average!/n");
while(p)
{
if(p->all>aveall)printf("%s/n",p->name);
p=p->next;
}
}
int main()
{
int i=0;
student *head=0;
while(i++<P)head=insertLink(head,createStu());
print(head);
printUP(head);
getch();
return 0;
}