แต่เวลานำไปใช้งานต้องมีการเรียงข้อมูลก่อน
เลยไปค้นข้อมูลวิธีการ ได้ตัวอย่างมาแล้ว เอามาฝากไว้ก่อน
แล้วจะดัดแปลงอีกครั้ง
/*
http://www.thescripts.com/forum/thread461852.html
Sorting Array of Structures
How would I go about sorting this structure by title?
*/
include
include
typedef struct {
char author[40];
char title[40];
char code[4];
int hold;
int loan;
} LIBRARY;
int complib(const void *vp1, const void *vp2)
{
const LIBRARY *p1 = vp1;
const LIBRARY *p2 = vp2;
// return strcmp(p1->title, p2->title);
/* case Number */
if(p1->loan > p2->loan) return 1;
if(p1->loan < p2->loan) return -1;
return 0;
}
void printyourdata(LIBRARY *b ,int n )
{
int i;
for (i = 0; i < n ; i++){
printf("%-12s\t%-25s\t%s\t%d\t%d\n" ,
b[i].author, b[i].title, b[i].code , b[i].hold ,b[i].loan );
}
}
int main()
{
int n;
LIBRARY book[]= {
{"Galvin","Operating Systems","G02",15,2} ,
{"Golumbic","Graph Theory","G01",5,2} ,
{"Habermann","Operating Systems","H01",10,5} ,
{"Herzog","Computing Structures","H03",10,7} ,
{"Holub","Compiler Design","H05",11,8} ,
{"Horowitz","Programming Languages","H06",16,10} ,
{"Hughs","Structured Programming","H02",4,4} ,
{"Hunter","Understanding C","H04",6,6} ,
{"Jacobs","Database Logic","J01",3,1} ,
{"Kanter","Management Information","K01",8,1} ,
{"Kindred","Data Systems","K04",2,0} ,
{"Kleinrock","Queueing Systems","K03",14,0} ,
{"Kuo","Numerical Methods","K02",2,0} ,
{"Lane","Data Communications","L01",20,3} ,
{"Mano","Computer Architecture","M01",2,2}
};
n = sizeof book / sizeof book[0];
printyourdata(book, n);
qsort(book, n, sizeof book[0], complib);
printf("\n\n");
printf("==============================================\n");
printyourdata(book, n);
system("PAUSE");
return 0;
}
/*
Function getData(t1, t2, t3, t4, t5)
str1 = "{"
str1 = str1 & Chr(34) & t1 & Chr(34) & ","
str1 = str1 & Chr(34) & t2 & Chr(34) & ","
str1 = str1 & Chr(34) & t3 & Chr(34) & ","
str1 = str1 & t4 & ","
str1 = str1 & t5 & "}"
getData = str1
End Function
*/

ไม่มีความคิดเห็น:
แสดงความคิดเห็น