C语言结构体查找符合条件的数据

#include <stdio.h>
#include <string.h>

// 设置结构体类型Student,其中有字符类型name(姓名),整型id(学号),整形score(分数)
struct Student
{
    char name[20];
    int id;
    int score
};

int main(){
    // 结构体变量stu1和stu2
    struct Student stu1[5],stu2[5];

    //分别把五个学生的数据赋值给stu1数组中
    strcpy(stu1[0].name,"xiaoqi");
    stu1[0].id = 1;
    stu1[0].score = 80;

    strcpy(stu1[1].name,"laowang");
    stu1[1].id = 2;
    stu1[1].score = 100;

    strcpy(stu1[2].name,"laoli");
    stu1[2].id = 3;
    stu1[2].score = 100;

    strcpy(stu1[3].name,"xiaoliu");
    stu1[3].id = 4;
    stu1[3].score = 95;

    strcpy(stu1[4].name,"xiaozhao");
    stu1[4].id = 5;
    stu1[4].score = 85;
    // for (int i = 0;i < 5;i++){
    //     scanf("%s%d%d",stu1[i].name,&stu1[i].id,&stu1[i].score);
    // }


    // 找出最大值并存入到变量max
    int max = stu1[0].score;
    for (int i = 1;i < 5;i++){
        if (max < stu1[i].score)
        {
            max = stu1[i].score;
        }
    }

    /* 最高分可能不止一个人 
       只要分数等于max的学生信息都符合条件存入到stu2结构体变量中*/
    int count = 0;
    for (int j = 0;j < 5;j++){
        if (stu1[j].score == max)
        {
            stu2[count++] = stu1[j];
        }
    }

    // 输出结果
    for (int k = 0;k < count;k++){
        printf("%s %d %d\n",stu2[k].name,stu2[k].id,stu2[k].score);
    }
    // printf("%d\n",stu1[0].score);
    // printf("%d",max);

    return 0;
}

  • 11
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值