0
我有這個部分,首先(我將使用它來創建員工結構):使用的qsort到結構(由「姓」字段排序)排序
typedef struct
{
char first_name[20], last_name[20];
int birthdate, temporary;
//date of birth YEAR/MONTH/DAY
//temporary employees: 1, else 0
}factory;
我會讀使用下面的函數中的數據:
void reading(factory *employee,int *nr)
{
++(*nr);
printf("First name:\n");
fflush(stdin);
gets((employee+*nr)->first_name);
printf("Last name:\n");
fflush(stdin);
gets((employee+*nr)->last_name);
printf("Birthdate:\n");
fflush(stdin);
scanf ("%d" , &((employee + *nr)->birthdate));
printf("Temporary employee? 1 for YES, 0 for NO");
fflush(stdin);
scanf("%d", &((employee + *nr)->temporary));
}
和比較功能(我不認爲這是正確寫入,就如何修改這將是巨大的任何建議 - 它應該是「工廠員工* IA」,而不是的結構?):
int struct_cmp_by_name(const void *a, const void *b)
{
struct employee *ia = (struct employee *)a;
struct employee *ib = (struct employee *)b;
return strcmp(ia->last_name, ib->last_name);
}
我也有一個簡單的顯示功能,其原型我將下面複製:
void display(factory *employee, int nr)
的代碼其他部分已經針對的空間起見,省略。在這種情況下,我將如何實現qsort功能?我有比較函數,但我不知道基本數組應該是什麼或如何找到其他兩個大小參數。期待感謝您提供的任何幫助。
只是說明:調用'fflush(stdin)'是未定義的行爲。你可能想找到另一種方式來做到這一點。另外,拋出'gets()'並使用'fgets()'代替。實際調用到'qsort()'的地方在哪裏?這對解決您的問題似乎有點重要。假設你的整理牀是一個有效的工廠數組,你的比較器看起來是正確的(除了那些指針明顯錯誤地忽略了const)。 – WhozCraig
使用標準庫'qsort','++(* nr);':,'(* nr)'是開始'-1'嗎? – BLUEPIXY