c语言 12-4

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 tmp = *x;
    *x = *y;
    *y = tmp;
}

void sort_hei(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 str[NUMBER];
    
    for(i = 0; i < NUMBER; i++)
    {
        printf("mem.%d.name = ", i + 1); scanf("%s", str[i].name);
        printf("mem.%d.height = ", i + 1); scanf("%d", &str[i].height);
        printf("mem.%d.weight = ", i + 1); scanf("%f", &str[i].weight);
        printf("mem.%d.schols = ", i + 1); scanf("%ld", &str[i].schols); 
    }
    
    /*Student str[] = {
    {"Sato", 178, 61.2, 80000},
    {"Sanaka", 175, 62.5, 73000},
    {"Takao", 173, 86.2, 0},
    {"Mike", 165, 72, 70000},
    {"Masaki", 179, 77.5, 70000},
    };*/
    
    puts("\n=============================");
    for(i = 0; i < NUMBER; i++)
        printf("%-8s %7d%7.2f%7ld\n", str[i].name, str[i].height, str[i].weight, str[i].schols);
        
    sort_hei(str, NUMBER);
    puts("\n==============================");
    for(i = 0; i < NUMBER; i++)
        printf("%-8s %7d%7.2f%7ld\n",str[i].name, str[i].height, str[i].weight, str[i].schols);
    return 0;
}

 

 

2、

#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 tmp = *x;
    *x = *y;
    *y = tmp;
}

void sort_hei(Student a[], int n)
{
    int choose;
    printf("choose = 0, sort by height\nchoose = 1, sort by name.\nchoose = "); scanf("%d", &choose);
    
    int i, j;
    if(choose == 0)
    {
        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]);
                }
            }
        }
    }
    if(choose == 1)
    {
        for(i = 0; i < n - 1; i++)
        {
            for(j = n - 1; j > i; j--)
            {
                if(strcmp(a[j - 1].name,a[j].name) > 0)
                {
                    swap_str(&a[j - 1], &a[j]);
                }
            }
        }
    }
}

int main(void)
{
    int i;
    
    Student str[] = {
    {"Sato", 178, 61.2, 80000},
    {"Sanaka", 175, 62.5, 73000},
    {"Takao", 173, 86.2, 0},
    {"Mike", 165, 72, 70000},
    {"Masaki", 179, 77.5, 70000},
    };
    

    for(i = 0; i < NUMBER; i++)
        printf("%-8s %7d%7.2f%7ld\n", str[i].name, str[i].height, str[i].weight, str[i].schols);
    
    puts("\n=============================");
    sort_hei(str, NUMBER);
    puts("\n==============================");
    for(i = 0; i < NUMBER; i++)
        printf("%-8s %7d%7.2f%7ld\n",str[i].name, str[i].height, str[i].weight, str[i].schols);
    return 0;
}

 

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