0

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

输入

BEKELE Tariku ETH 27:31.43

RUPP 盖伦美国 27:30.90

法拉莫国标 27:30.42

输出

法拉莫国标 27:30.42

RUPP 盖伦美国 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;
}
4

1 回答 1

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;
}

请参阅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);

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

于 2012-08-24T14:34:48.377 回答