• 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("
    =============================");
        for(i = 0; i < NUMBER; i++)
            printf("%-8s %7d%7.2f%7ld
    ", str[i].name, str[i].height, str[i].weight, str[i].schols);
            
        sort_hei(str, NUMBER);
        puts("
    ==============================");
        for(i = 0; i < NUMBER; i++)
            printf("%-8s %7d%7.2f%7ld
    ",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
    choose = 1, sort by name.
    choose = "); 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
    ", str[i].name, str[i].height, str[i].weight, str[i].schols);
        
        puts("
    =============================");
        sort_hei(str, NUMBER);
        puts("
    ==============================");
        for(i = 0; i < NUMBER; i++)
            printf("%-8s %7d%7.2f%7ld
    ",str[i].name, str[i].height, str[i].weight, str[i].schols);
        return 0;
    }

  • 相关阅读:
    ajax
    导入操作
    游标的使用
    多行编辑
    IOS开发之--NSPredicate
    asp.net DataTables
    asp.net 汉字转拼音的车祸现场
    Git 连接细节
    Aspose.Words 操作指北
    码云代码管理插件备忘
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/14849128.html
Copyright © 2020-2023  润新知