这个排序很好写,用stdlib的qsort函数就行啦~
用这个函数可以实现自定义的快排,可以排序的对象很多,基本上所有的数据结构能可以用它排的,结构体也很好排。
具体用法网上有。我只给出我写的一个例子吧:
#define MAX_ITEMS 10
typedef struct _Item Item;
typedef struct _Data_info Data_info;
typedef enum _Sort_Method Sort_Method;
struct _Item {
long num;
float math;
float eng;
float phy;
float prog_c;
};
struct _Data_info {
int max_items;
int items_now;
//char* date;
Item* pItems[MAX_ITEMS];
};
enum _Sort_Method
{
SORT_M_NUM,
SORT_M_SCORE_M,
SORT_M_SCORE_E,
SORT_M_SCORE_P,
SORT_M_SCORE_C
};
static int cmp_num(const void* a,const void* b){
return (*(Item *)a).num > (*(Item *)b).num ? 1 : -1;
}
static int cmp_phy(const void* a,const void* b){
return (*(Item *)a).phy > (*(Item *)b).phy ? 1 : -1;
}
static int cmp_math(const void* a,const void* b){
return (*(Item *)a).math > (*(Item *)b).math ? 1 : -1;
}
static int cmp_eng(const void* a,const void* b){
return (*(Item *)a).eng > (*(Item *)b).eng ? 1 : -1;
}
static int cmp_prog_c(const void* a,const void* b){
return (*(Item *)a).prog_c > (*(Item *)b).prog_c ? 1 : -1;
}
static int (*pCMP[5])(const void*,const void*)={&cmp_num,&cmp_math,cmp_eng,cmp_phy,cmp_prog_c};
//此处加不加&都是一样的,编译器会把函数名会转换为函数指针
//所以pCMP[1](a,b)与*pCMP[1](a,b)也是一样的
void sort_all_items(Data_info* mydatabase,Sort_Method method){
qsort(mydatabase->pItems,MAX_ITEMS,sizeof(mydatabase->pItems[0]),pCMP[method]);
}
这是从一个工程中截取出来的一小部分,所以代码不完整
#include
#include
struct Book
{
char BookTitle[100];
char Author[100];
int Date;
char publishUnit[100];
int lendNum;
int stockNum;
};
void Input(struct Book *p);
void Output(struct Book *pp,int n);
int main(int argc, const char * argv[])
{
int n,i;
printf("请输入图书的数目:\n");
scanf("%d",&n);
getchar();
struct Book book[255];
for (i = 0; i < n; i++)
{
Input(&book[i]);
}
Output(book,n);
return 0;
}
void Input(struct Book *p)
{
printf("输入书的名称:\n");
scanf("%s",p->BookTitle);
getchar();
printf("输入书的作者:\n");
scanf("%s",p->Author);
getchar();
printf("输入书的出版日期:\n");
scanf("%d",&p->Date);
getchar();
printf("输入出版社名字:\n");
scanf("%s",p->publishUnit);
getchar();
printf("输入借出数目:\n");
scanf("%d",&p->lendNum);
getchar();
printf("输入库存数目:\n");
scanf("%d",&p->stockNum);
getchar();
}
void Output(struct Book *pp ,int n)
{
struct Book *p1=pp;
char ch[100];
int i,j,k,temp;
for(i=1;i
for(j=0;j
{
temp=(pp+j)->Date;
(pp+j)->Date=(pp+j+1)->Date;
(pp+j+1)->Date=temp;
temp=(pp+j)->lendNum;
(pp+j)->lendNum=(pp+j+1)->lendNum;
(pp+j+1)->lendNum=temp;
temp=(pp+j)->stockNum;
(pp+j)->stockNum=(pp+j+1)->stockNum;
(pp+j+1)->stockNum=temp;
strcpy(ch,(pp+j)->Author);
strcpy((pp+j)->Author,(pp+j+1)->Author);
strcpy((pp+j+1)->Author,ch);
strcpy(ch,(pp+j)->BookTitle);
strcpy((pp+j)->BookTitle,(pp+j+1)->BookTitle);
strcpy((pp+j+1)->BookTitle,ch);
strcpy(ch,(pp+j)->publishUnit);
strcpy((pp+j)->publishUnit,(pp+j+1)->publishUnit);
strcpy((pp+j+1)->publishUnit,ch);
}
}
for(i=0;i
printf("书名:%s\n",(pp+i)->BookTitle);
printf("作者:%s\n",(pp+i)->Author);
printf("出版日期:%d\n",(pp+i)->Date);
printf("出版社:%s\n",(pp+i)->publishUnit);
printf("借出数目:%d\n",(pp+i)->lendNum);
printf("库存数目:%d\n",(pp+i)->stockNum);
}
}