我正在嘗試對姓氏和公司名稱進行排序。用戶必須輸入姓氏或公司名稱(只有一個)。這是我現在的代碼:C編程:使用兩個參數對結構進行排序
struct store {
unsigned long phone_num;
char *first_name;
char *last_name;
char *company_name;
char *email;
};
typedef struct store store;
void findContact(FILE *fp, long fileEnd)
{
/*variables*/
char fName [100];
char lName [100];
char cName [100];
char email [100];
int i, length;
int count = 1;
int size = sizeof(long);
int usize = sizeof(unsigned long);
unsigned long phone;
long nextPosition = 0;
long fNamePosition = 0;
long lNamePosition = 0;
long cNamePosition = 0;
long emailPosition = 0;
store *list;
list = malloc(sizeof(store));
/*Search for Contact position in file*/
fseek(fp, 0, SEEK_SET); /*Seeks to beginning of file*/
do {
i = count - 1;
fread(&phone, usize, 1, fp); /*reads phonenumber of contact*/
fread(&fNamePosition, size , 1, fp);
fread(&lNamePosition, size, 1, fp);
fread(&cNamePosition, size, 1, fp);
fread(&emailPosition, size, 1, fp);
fread(&nextPosition, size, 1, fp);
if(fNamePosition != 0) {
fseek(fp,fNamePosition,SEEK_SET);
if(lNamePosition == 0) {
length = cNamePosition - fNamePosition;
} else {
length = lNamePosition - fNamePosition;
}
fread(fName,sizeof(char),length,fp);
} else {
strcpy(fName," ");
}
if(lNamePosition != 0) {
fseek(fp,lNamePosition,SEEK_SET);
if (cNamePosition == 0) {
length = emailPosition - lNamePosition;
} else {
length = cNamePosition - lNamePosition;
}
fread(lName,sizeof(char), length,fp);
} else {
strcpy(lName," ");
}
if(cNamePosition != 0) {
fseek(fp,cNamePosition,SEEK_SET);
length = emailPosition-cNamePosition;
fread(cName,sizeof(char), length,fp);
} else {
strcpy(cName," ");
}
fseek(fp,emailPosition,SEEK_SET);
length = nextPosition - emailPosition;
fread(email,sizeof(char),length,fp);
list = realloc(list, count * sizeof(store));
list[i].phone_num = phone;
list[i].first_name = (char *) malloc(strlen(fName) + 1);
strcpy(list[i].first_name, fName);
list[i].last_name = (char *) malloc(strlen(lName) + 1);
strcpy(list[i].last_name, lName);
list[i].company_name = (char *) malloc(strlen(cName) + 1);
strcpy(list[i].company_name, cName);
list[i].email = (char *) malloc(strlen(email) + 1);
strcpy(list[i].email, email);
count++;
} while (ftell(fp) != fileEnd);
count--;
qsort(list, count, sizeof(store), compareStore);
/*Prints output*/
for(i=0;i<count;i++) {
printf("First Name: %s\n", list[i].first_name);
printf("Last Name: %s\n", list[i].last_name);
printf("Company Name: %s\n", list[i].company_name);
printf("Phone Number (enter only numbers): %ld\n", list[i].phone_num);
printf("Email: %s\n", list[i].email);
free(list[i].first_name);
free(list[i].last_name);
free(list[i].company_name);
free(list[i].email);
}
free(list);
return;
}
int compareStore (const void*a, const void *b)
{
const store *aa = a;
const store *bb = b;
return (strcmp(bb->last_name, aa->last_name));
}
這是我的輸出截至目前。它應該考慮姓氏和公司名稱作爲相同的參數,然後對它們進行排序:
First Name: Andre
Last Name: D'Souza
Company Name:
Phone Number (enter only numbers): 6474000964
Email: [email protected]
First Name:
Last Name:
Company Name: University of Guelph
Phone Number (enter only numbers): 5192137299
Email: [email protected]
First Name: Raf
Last Name:
Company Name: Raffy Taffy
Phone Number (enter only numbers): 1234567
Email: [email protected]
您可能會從查看[** qsort不會排序動態分配的結構數組**](http:// stackoverflow。com/questions/43163754/qsort-wont-sort-dynamic-allocated-arrays-of-structs) –