C:结构体简单练习

定义一个结构体来描述一个学生信息(学号/姓名/成绩/出身年月日),再从键盘上输入5个学生的信息,按成绩的降序依次输出每个学生的成绩。

#include <stdio.h>
#define N 5

//构造结构体 日期
struct date
{
    int y, m, d;    //年 月 日
};
//构造结构体 学生
struct student
{
    int num;
    char name[20];
    struct date birthday;
    float score;
};

int main(int argc, char** argv)
{
    //定义结构体数组
    struct student s[N];
    int i, j;
    //依次输入学生信息 
    for(i=0; i<N; i++)
    {
        scanf(
            "%d %s %d/%d/%d %f", &(s[i].num), s[i].name,
            &(s[i].birthday.y), &(s[i].birthday.m),
            &(s[i].birthday.d), &(s[i].score)
            );
    }
    //冒泡排序
    for(i=0; i<N-1; i++)
    {
        for(j=0; j<N-i-1; j++)

        {
            if(s[j].score<s[j+1].score)
            {
                //结构体变量可以相互赋值
                struct student temp = s[j];
                s[j] = s[j+1];
                s[j+1] = temp;
            }
        }
    }
    //降序输出结构体数组元素
    printf("\n");
    for(i=0; i<N; i++)
    {
        printf(
            //用制表位,输出美观
            "%d\t%s\t%d/%d/%d\t%.1f\n", s[i].num, s[i].name, 
            s[i].birthday.y, s[i].birthday.m, 
            s[i].birthday.d, s[i].score
            );
    }
    
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值