zoukankan      html  css  js  c++  java
  • 12月11号 结构体排序(按照学生年龄)

    #include <stdio.h>

    typedef struct student{

        char name[20];

        long id;

        int age;

        float height;

    }Student;//定义一个结构体

     

    //输入

    void input(Student array[]){

        for (int i = 0; i < 3; i++) {

            printf("请输入名字:");

            scanf("%s", array[i].name);

            printf("请输入学号:");

            scanf("%ld", &array[i].id);

            printf("请输入年龄:");

            scanf("%d", &array[i].age);

            printf("请输入身高:");

            scanf("%f", &array[i].height);

            printf(" ");

        }

    }

     

    //按年龄进行冒泡排序 

    void bubbleSort(Student array[], int elementNum){

        Student temp;

        for (int i = 0; i < elementNum-1; i++) {

            for (int j = elementNum - 2;j>=i ;j--) {

                if (array[j].age > array[j+1].age) {

                    temp = array[j];

                    array[j] = array[j+1];

                    array[j+1] = temp;

                }

            }

        }

    }

     

    int main(int argc, const char * argv[]) {

        Student array[10] = {};

        

        input(array);

        bubbleSort(array,3);

        

        for (int i = 0; i < 3; i++) {

            printf("名字:%s 学号:%ld 年龄:%d 身高:%.1f ", array[i].name, array[i].id, array[i].age, array[i].height);

        }

        return 0;

    }

  • 相关阅读:
    work 2
    chapter02
    7.23作业
    第五章
    三层交换机
    基于nginx结合openssl实现https
    chapter10--进程和计划任务管理
    Linux系统管理08--服务器RAID及配置实战
    chapter07-- LVM逻辑卷
    Linux系统管理07--文件系统与LVM
  • 原文地址:https://www.cnblogs.com/hmzxwky/p/5042661.html
Copyright © 2011-2022 走看看