BJFU——205基于顺序存储结构的图书信息表的排序
1 #include<stdio.h> 2 #include<stdlib.h> 3 4 #define MAX 1000 5 typedef struct{ 6 double no; 7 char name[MAX]; 8 double price; 9 10 }Book; 11 12 int createList(Book b[]); 13 void traverse(Book b[],int len); 14 void sortList(Book b[],int len); 15 int main() 16 { 17 Book book[MAX]; 18 19 int len; 20 len = createList(book); 21 sortList(book,len); 22 traverse(book,len); 23 24 return 0; 25 } 26 27 int createList(Book b[]) 28 { 29 int i=0; 30 while(1) 31 { 32 scanf("%lf",&b[i].no); 33 scanf("%s",&b[i].name); 34 scanf("%lf",&b[i].price); 35 36 if(b[i].no==0&&b[i].name[0]=='0'&&b[i].price==0) 37 break; 38 39 i++; 40 41 } 42 43 return i; 44 } 45 46 void traverse(Book b[],int len) 47 { 48 for(int i=0;i<len;i++) 49 { 50 printf("%.0lf ",b[i].no); 51 printf("%s ",b[i].name); 52 printf("%.2f",b[i].price); 53 printf("\n"); 54 } 55 56 } 57 58 void sortList(Book b[],int len) 59 { 60 int i,j; 61 Book t; 62 63 for(i=0;i<len-1;i++) 64 { 65 int max_index = i; 66 67 for(j=i+1;j<len;j++) 68 { 69 if(b[j].price >= b[max_index].price) 70 max_index = j; 71 } 72 if(i!=max_index) 73 { 74 t = b[i]; 75 b[i] = b[max_index]; 76 b[max_index] = t; 77 78 } 79 } 80 }