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;

    }

  • 相关阅读:
    Chapter 1. 庞加莱群、单粒子态和时间空间反演
    QI Chapter 1
    Introduction to QFT
    在visual studio 2017中配置Qt
    BUAA软工第0次作业
    附加作业(个人)
    个人作业3——个人总结(Alpha阶段)
    单元测试
    英语学习app案例分析
    小学生四则运算改进版之
  • 原文地址:https://www.cnblogs.com/hmzxwky/p/5042661.html
Copyright © 2011-2022 走看看