1.完成学生成绩信息存储的设计,输出高于平均成绩的学生编号
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
//设计学生成绩信息的数据结构
typedef struct StuNode{
int id;
float score;
struct StuNode *next;
}StuNode,*Stulist;
Stulist InitStuInf(void){
StuNode *r,*p,*head;
head=(StuNode *)malloc(sizeof(StuNode));
head->next=NULL;
r=head;
int i,id;//i为学生个数
float score;
printf("学生数量:");
scanf("%d",&i);
while((p=(StuNode *)malloc(sizeof(StuNode))) && i){//创建新节点
printf("学生编号、学生分数分别是:");
scanf("%d %f",&id,&score);
p->id=id;
p->score=score;
p->next=r->next;
r->next=p;
r=r->next;
i--;
}
r->next=NULL;
return head;
}
//输出打印
void printAllStuNode(Stulist head){
head=head->next;//链表头节点为空节点
while(head!=NULL){
printf("编号:%d,分数:%.2f\n",head->id,head->score);
head=head->next;
}
}
//计算平均分
float getAveScores(Stulist head){
head=head->next;
int count=0;
float sum=0;
while(head!=NULL){
sum+=head->score;
count++;
head=head->next;
}
return sum/count;
}
//输出小于平均分的信息
void printLowAverage(Stulist head,float ave){
head=head->next;//链表头节点为空节点
printf("低于平均分%.2f\n",ave);
while(head!=NULL){
if(head->score<ave)
printf("编号:%d,分数:%.2f\n",head->id,head->score);
head=head->next;
}
}
int main(void){
Stulist list;//相当于StuNode *
list=InitStuInf();//初始化分数信息
printAllStuNode(list);//打印所有信息
printLowAverage(list,getAveScores(list));//打印低于平均分信息
return 0;
}