zoukankan      html  css  js  c++  java
  • 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;
    }

  • 相关阅读:
    logstash过滤器使用ruby把时间格式转UNIX_MS
    logstash使用“|”切分日志报错:Exception caught while applying mutate filter {:exception=>"Invalid FieldReference: `message[1]`"}
    win10安装FileBeat
    win10安装ELK
    向量组的秩一定是有的吗?
    SCIE与SCI的区别
    安装MIUI+出现无法定位程序输入点 GetDpiForWindow 于动态链接库 USER32.dll上如何解决?
    photoshop反色
    二值图像像素交换
    矩阵翻转
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/14849128.html
Copyright © 2011-2022 走看看