zoukankan      html  css  js  c++  java
  • 明解C语言,练习13-3,从文件中读入个人信息,按身高排序后显示

    #include <stdio.h>

    #define NUMBER 6
    #define F_PATH "D:\C_C++\ec13-3\hw.dat"
    typedef struct {
        char name[20];
        int height;
        float weight;
    } student;
    void swap(student *x,student *y)
    {
        student tmp = *x;
        *x = *y;
        *y = tmp;
    }
    void sort(student data[],int n)
    {
        int k = n - 1;
        while( k >= 0) {
            int i,j;
            for(i = 1,j = -1;i <= k;i++)
                if(data[i-1].height > data[i].height){
                    j = i -1;
                    swap(&data[i],&data[j]);
                }
            k = j;
        }
    }
    int main(void)
    {
        FILE *fp;
        student str[10];
        if ((fp = fopen(F_PATH,"r")) == NULL)
            printf("aIt is error!
    ");
        else {
            for(int i=0;i < NUMBER;i++)
            {
                fscanf(fp,"%s%d%f",str[i].name,&str[i].height,&str[i].weight);
    //            while((fscanf(fp,"%s%d%f",str[i].name,&str[i].height,&str[i].weight))==3);
            }
            sort(str,NUMBER);
            puts("-----------------------------------");
            for(int i=0;i < NUMBER;i++) {
                printf("%-8s %6d %6.1f
    ",str[i].name,str[i].height,str[i].weight);
            }
            puts("-----------------------------------");
            fclose(fp);
        }
        return (0);
    }

    花間酒氣;竹裹棋聲。 山奔海立;沙起雷行。 風雲論道;筆墨通天。
  • 相关阅读:
    JSONObject登录接口
    HttpClient跨域请求post
    线段树个人理解及模板
    Python基本语法(一)
    Boyer-Moore算法
    Sunday算法浅谈
    Kmp算法浅谈
    bm坏字符 , Horspool算法 以及Sunday算法的不同
    字典树的建立和基本查找
    CF Round551 Div2 题解
  • 原文地址:https://www.cnblogs.com/banshiliuli1990/p/3795647.html
Copyright © 2011-2022 走看看