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;
    }

  • 相关阅读:
    CentOS中文件夹基本操作命令
    Apache和Nginx下禁止访问特定的目录或文件
    自适应网页设计(Responsive Web Design)
    使用Google Https搜索
    AMD 3600+ X2 CPU配合昂达A69T主板超频教程
    dedecms上传图片相对路径改成绝对路径方法
    安装ecshop默认安装后的错误解决方案
    动态加载JS脚本的4种方法
    Java虚拟机(二)对象的创建与OOP-Klass模型
    Android系统启动流程(四)Launcher启动过程与系统启动流程
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/14849128.html
Copyright © 2011-2022 走看看