c语言中结构体数组

c语言中结构体数组。

1、

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

#define NUMBER 5
#define NAME_LEN 64

typedef struct{
    char name[NAME_LEN];
    int height;
    float weight;
    long schols;
} Student;

void swap_str(Student *x, Student *y)
{
    Student temp = *x;
    *x = *y;
    *y = temp;
}

void sort_height(Student a[], int n)
{
    int i, j;
    for(i = 0; i < n - 1; i++)
    {
        for(j = n - 1; j > i; j--)
        {
            if(a[j - 1].height > a[j].height)
            {
                swap_str(&a[j - 1], &a[j]);
            }
        }
    }
}

int main(void)
{
    int i;
    Student std[] = {
    {"Sato", 178, 61.2, 80000},
    {"Sanaka", 175, 62.5, 73000},
    {"Takao", 173, 86.2, 0},
    {"Mike", 165, 72.3, 70000},
    {"Masaki", 179, 77.5, 70000}
    };
    
    for(i = 0; i < NUMBER; i++)
        printf("%-8s %7d%7.2f%7ld\n", std[i].name,std[i].height,std[i].weight,std[i].schols);
        
    sort_height(std, NUMBER);
    puts("\n===========================");
    for(i = 0; i < NUMBER; i++)
        printf("%-8s %7d%7.2f%7ld\n", std[i].name, std[i].height, std[i].weight, std[i].schols); 
    
    return 0;
}

 

posted @ 2021-06-04 12:14  小鲨鱼2018  阅读(152)  评论(0编辑  收藏  举报