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;

    }

  • 相关阅读:
    CentOS 6.5下安装MySQL 5.6.21
    Java文件实时监控Commons-io
    quartz 实例记录
    Quartz任务调度快速入门(转)
    MySQL日期时间函数大全(转)
    struts2 jsp 传参 NullPointerException问题解决
    hibernate cascade=CascadeType.All
    struts2 学习记录 过滤器 国际化
    struts2 struts1.x 区别
    学习 自己的过滤器和监听器
  • 原文地址:https://www.cnblogs.com/hmzxwky/p/5042661.html
Copyright © 2011-2022 走看看