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

标签 c

错误sort_structs_time。使用struct和qsort接受和存储用户输入的程序;名字、姓氏、国家/地区和时间。应使用 qsort 按时对输出进行排序,例如

输入

贝克勒·塔里库 ETH 27:31.43

RUPP Galen 美国 27:30.90

法拉莫 GB 27:30.42

输出

法拉莫 GB 27:30.42

RUPP Galen 美国 27:30.90

贝克勒·塔里库 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;
}

最佳答案

您需要对 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);
} 

还有其他方法可以纠正您的代码。我只是觉得这更容易理解。

在我看来,时间的更好表示是这样的:

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

这样你就可以这样打印时间:

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

(如果使用这种表示方式,则必须更改程序中与时间相关的部分,即比较函数。)

关于c - struct 存储用户输入数据和 qsort 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12109654/

相关文章:

c - 省略符号会自动默认为 C 中的带符号变量吗?

c - 反字符 a 到 z

c - 嵌套 strtok_r : command line argument parsing

c - 使用 C 返回数组

c - 为什么 -O1 比 -O2 快

c - 用于 Ubuntu 14.04 的 C 中的 UINPUT 设备程序不起作用。为什么?

c - 在线程池实现中保留锁顺序

线条的颜色没有变化?

C fopen 和 fgets 返回奇怪的字符而不是文件内容

调用使用指针接收数组的函数