有一个链表,节点包含学号,成绩,指向下一个节点的结构体指针,写一个函数实现,输出成绩高于平均成绩的学生学号。
#include<stdio.h>
#include <stdlib.h>
typedef struct stuNode{
int id;
double score;
stuNode *next;
}stu;
void printaboveaverage(stu *head){
stu *s=head->next;
double sumscore;
int count;
for(int i=0;i<3;i++){
sumscore+=s->score;
count++;
s=s->next;
}
double ave=sumscore/count;
printf("均分为 %lf\n",ave);
stu *s1=head->next;
while(s1!=NULL){
if(s1->score>ave){
printf("成绩高于均分的学号:%d",s1->id);
}
s1=s1->next;
}
}
int main()
{
stu *head,*p,*q;
head=(stu *)malloc(sizeof(stu));
head->next=NULL;
q=head;
int n;
long a;
double b;
for(int i=0;i<3;i++){
p=(stu *)malloc(sizeof(stu));
scanf("%d,%lf",&a,&b);
p->id=a;
p->score=b;
printf("学号和分数为:%d,%lf\n",p->id,p->score);
q->next=p;
q=p;
}
q->next=NULL;
printaboveaverage(head);
return 0;
}