c语言 13-3

Posted 小虾米2018

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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!\\n");
    else
    {
        while(fscanf(fp, "%s%lf%lf", name, &height, &weight) == 3)
        {
            printf("%-8s%8.2f%8.2f\\n", name, height, weight);
            lines++;
            hsum += height;
            wsum += weight;
        }
        puts("\\n===========================\\n");
        printf("average %8.2f%8.2f\\n", 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("\\n===========================\\n");
        for(i = 0; i < lines; i++)
            printf("%-8s%8.2f%8.2f\\n", x[i].name, x[i].height, x[i].weight);
        sort(x, lines);
        puts("\\n============================\\n");
        puts("sort_by_height.");
        for(i = 0; i < lines; i++)
            printf("%-8s%8.2f%8.2f\\n", x[i].name, x[i].height, x[i].weight);
        fclose(fp);
    }
    return 0;
}

 

以上是关于c语言 13-3的主要内容,如果未能解决你的问题,请参考以下文章

我的C语言学习进阶之旅解决 Visual Studio 2019 报错:错误 C4996 ‘fscanf‘: This function or variable may be unsafe.(代码片段

SQL Select 语句的用法

C语言100个经典算法源码片段

需要示例代码片段帮助

c语言 13-3

C语言必会面试题(3耶稣有13个门徒,当中有一个就是出卖耶稣的叛徒,请用排除法找出这位叛徒:13人围坐一圈,从第一个開始报号:1,2,3,1,2,3...。凡是报到“3”就退出圈子,...)(代码片段