struct存储用户输入数据和qsort输出

时间:2012-08-24 12:45:29

标签: c

错误sort_structs_time。程序使用struct和qsort来接受和存储用户输入;名字,姓氏,国家和时间。输出应使用qsort按时排序,例如

输入

BEKELE Tariku ETH 27:31.43

RUPP Galen USA 27:30.90

FARAH Mo GB 27:30.42

输出

FARAH Mo GB 27:30.42

RUPP Galen USA 27:30.90

BEKELE Tariku ETH 27:31.43

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

struct olympics { 
    //char athlete[25];
    char fname[15];
    char lname[15];
    char country[5];
    float time;
};

/* qsort struct comparision function (time float field) */ 
int struct_cmp_by_time(const void *a, const void *b) 
{ 
    struct olympics *ia = (struct olympics *)a;
    struct olympics *ib = (struct olympics *)b;
    return (int)(60.f*ia->time - 60.f*ib->time); 
}

/* struct array printing function */ 
void print_struct_array(struct olympics *array, size_t len) 
{ 
    size_t i;

    for(i=0; i<len; i++) 
        printf("%s %s %s \t %.2f\n", array[i].fname, array[i].lname, array[i].country, array[i].time);

    puts("****");
} 

/* sorting structs using qsort() */ 
void sort_structs_time(void) 
{ 
    struct olympics structs[] = {
        scanf("%s %s %s %.2f\n", fname, lname, country, &time)
    };

    size_t structs_len = sizeof(structs) / sizeof(struct olympics);

    puts("**** Athletes finishing time...");

    /* print original struct array */ 
    print_struct_array(structs, structs_len);

    /* sort array using qsort functions */ 
    qsort(structs, structs_len, sizeof(struct olympics), struct_cmp_by_time);

    /* print sorted struct array */ 
    print_struct_array(structs, structs_len);
} 


/* call to the function) */ 
int main() 
{ 
    /* run the function */
    sort_structs_time();
    return 0;
}

1 个答案:

答案 0 :(得分:1)

您需要对sort_structs_timestruct_cmp_by_time函数进行一些更改。 很明显,你还没有理解C结构和矩阵,所以对这个主题进行修订。

int struct_cmp_by_time(const void *a, const void *b) 
{ 
    struct olympics *ia = (struct olympics *)a;
    struct olympics *ib = (struct olympics *)b;

    if (ia->time < ib->time) return -1;
    else if (ia->time == ib->time) return 0;
    else return 1;
}

请参阅this qsort文档,并查看其中提供的比较函数。

void sort_structs_time() 
{ 
int i, ath_num;

struct olympics *ath_recs;

printf("For how many athletes do you want to insert their records? \n");
scanf("%d", &ath_num);

ath_recs = (struct olympics *) malloc(ath_num*sizeof(struct olympics));

printf("Please insert athletes records. \n");
printf("type a random string and press ENTER when you done. \n");
for(i = 0; i < ath_num; i++){
    scanf("%s %s %s %f\n", ath_recs[i].fname, ath_recs[i].lname, ath_recs[i].country, &ath_recs[i].time); 
//Don't put %.2f on scanf!!! 
//Also, note that the fname, lname, country and time are struct fields, 
//so you have to access them this way.
}


puts("**** Athletes finishing time...");

/* print original struct array */ 
print_struct_array(ath_recs, ath_num);

/* sort array using qsort function */ 
qsort(ath_recs, (size_t) ath_num, sizeof(struct olympics), struct_cmp_by_time);

/* print sorted struct array */ 
print_struct_array(ath_recs, ath_num);
} 

还有其他方法可以纠正您的代码。我发现这更容易理解。

更好地代表时间,imo,就是这样:

struct time{
    int mins;
    int secs;
    int fsecs;
}

所以你可以用这种方式打印时间:

printf("%d:%d,%d\n", mins, secs, fsecs);

(如果使用此表示,则必须更改与时间相关的程序部分,即比较功能。)

相关问题