序号3—C语言结构体的综合练习

本文介绍了一个简单的学生成绩管理系统的设计与实现,通过链表结构存储学生的编号和分数,并提供了计算平均分及输出低于平均分的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

奋斗的蜗牛小猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值