• c语言 13-3


    1、

    #include <stdio.h>
    #include <string.h>
    
    typedef struct{
        char name[128];
        double height;
        double weight;
    }Type1;
    
    void swap2(Type1 *x, Type1 *y)
    {
        Type1 tmp = *x;
        *x = *y;
        *y = tmp;
    }
    
    void sort2(Type1 x[], int n)
    {
        int i, j;
        for(i = 0; i < n - 1; i++)
        {
            for(j = n - 1; j > i; j--)
            {
                if(strcmp(x[j - 1].name, x[j].name) > 0)
                {
                    swap2(&x[j - 1], &x[j]);
                }
            }
        }
    }
    
    int main(void)
    {
        FILE *fp;
        
        int lines = 0;
        char name[128];
        double height, weight;
        double hsum = 0, wsum = 0;
        
        fp = fopen("hw.dat", "r");
        if(fp == NULL)
            printf("afile does not exist!
    ");
        else
        {
            while(fscanf(fp, "%s%lf%lf", name, &height, &weight) == 3)
            {
                printf("%-8s%8.2f%8.2f
    ", name, height, weight);
                lines++;
                hsum += height;
                wsum += weight;
            }
            puts("
    ============================
    ");
            printf("average %8.2f%8.2f
    ", hsum/lines, wsum/lines);
            
            fp = fopen("hw.dat", "r");
            int i = 0;
            Type1 x[lines];
            while(fscanf(fp, "%s%lf%lf", x[i].name, &x[i].height, &x[i].weight) == 3)
            {
                i++;
            }
            puts("
    ============================
    ");
            for(i = 0; i < lines; i++)
                printf("%-8s%8.2f%8.2f
    ", x[i].name, x[i].height, x[i].weight);
            
            sort2(x, lines);
            puts("
    ==============================
    ");
            puts("sort_by_name.");
            for(i = 0; i < lines; i++)
                printf("%-8s%8.2f%8.2f
    ", x[i].name, x[i].height, x[i].weight);
            fclose(fp);
        }
        return 0;
    }

  • 相关阅读:
    iframe引入网页
    input同名
    混合框架
    <header><footer>引用
    <dl>
    凸包性质——cf1044C
    几何求叉积+最短路——cf1032D
    fresco 设置资源路径时的一个坑
    马拉车+贪心——cf1326D
    【模板变形】凸壳二分+斜率优化dp——cf1083E
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/14860128.html
Copyright © 2020-2023  润新知