2012-08-24 1 views
0

sort_structs_time 오류입니다. 사용자 입력을 받아들이고 저장하기 위해 struct와 qsort를 사용하는 프로그램. 이름, 성, 국가 및 시간. 출력은 qsort를 사용하여 시간에 정렬해야합니다.struct 사용자 입력 데이터 및 qsort 출력을 저장합니다.

입력

베켈레 타 리쿠 ETH 27 : 31.43

RUPP 갈레노스 USA 27 : 30.90

FARAH 모 GB 27 : 30.42

출력

FARAH 모 GB 27 : 30.42

,451,515,

RUPP 갈렌 USA 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; 
} 
+5

그리고 질문은? –

+1

C의 기본 사항을 매우 느슨하게 파악한 것 같습니다. 언어에 대한 기본 자료를 읽으십시오. – unwind

+1

운동 선수의 배열을 정의하고 그 값을 읽는 코드는 중요한 작업이 필요할 것입니다. 당신이 가진 구문은 단순히 당신이하는 방식이 아닙니다. 배열의 크기를 정의해야합니다 (예 : 3). 그런 다음 표준 입력을 반복해야합니다. 세그먼트 화 된 숫자 (분, 초 및 1 초의 분수)가 포함 된 문자열로 시간을 읽으므로 1 초 단위로 변환해야합니다. 읽은 시간 정보를 초 단위로 변환하는 별도의 함수가 필요할 수도 있습니다. 시간은 10 초 또는 몇 시간 미만이 될 수 있습니다. –

답변

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

코드를 수정하는 다른 방법이 있습니다. 나는 이것을 이해하기가 더 쉽다.

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

그래서 당신이 이런 식으로 시간을 인쇄 할 수 있습니다 :

시간의 더 나은 표현은, IMO,이 같다

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

(당신은 관련 프로그램의 일부를 변경해야 이 표현 (예 : 비교 함수)을 사용하면 시간이 걸립니다.

+0

안녕하세요, Kapoios, 코드를 설명해 주셔서 감사합니다 .. 많이 감사하겠습니다. – user1612261

관련 문제