程序代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct book
{
char name[50];
float price;
int classification;
};
int bookcmp( const void* a, const void* b )
{
const struct book* p = (const struct book*)a;
const struct book* q = (const struct book*)b;
if( p->price < q->price ) return -1;
if( p->price > q->price ) return +1;
return strcmp(p->name,q->name);
}
int main( void )
{
struct book books[100];
size_t n;
scanf( "%zu", &n );
for( size_t i=0; i!=n; ++i )
scanf( "%s%f%d", books[i].name, &books[i].price, &books[i].classification );
qsort( books, n, sizeof(*books), &bookcmp );
for( size_t i=0; i!=n; ++i )
printf( "%s,%.2f,%d\n", books[i].name, books[i].price, books[i].classification );
}