zoukankan      html  css  js  c++  java
  • c语言 13-3

    1、

    #include <stdio.h>
    
    typedef struct{
        char name[128];
        double height;
        double weight;
    }Type1;
    
    void swap(Type1 *x, Type1 *y)
    {
        Type1 tmp = *x;
        *x = *y;
        *y = tmp;
    }
    
    void sort(Type1 x[], int n)
    {
        int i, j;
        for(i = 0; i < n - 1; i++)
        {
            for(j = n - 1; j > i; j--)
            {
                if(x[j - 1].height > x[j].height)
                {
                    swap(&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);
            sort(x, lines);
            puts("
    ============================
    ");
            puts("sort_by_height.");
            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;
    }

  • 相关阅读:
    OO开发
    重构:Extract Method (提炼函数)
    Vue 一个组件引用另一个组件
    闭包
    视差滚动原理与实现
    JS中的constructor 和 prototype
    jQuery 为动态添加的元素绑定事件
    setTimeout 的理解
    关于Vertical Align的理解
    网页特效:滚动视差设计指南
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/14860085.html
Copyright © 2011-2022 走看看