结构排序:用结构类型表示书籍信息。成员含书名,定价, ISSN号。设计函数,用冒泡法将主函数中n种不同书籍按 定价从高到低排序,数据的输入和输出都在主函数完成。程序填空#include<stdio.h> struct BOOK { char name[30]; double price; char issn[30]; }; void sort(struct BOOK a[],int n); void sort(struct BOOK a[],int n) { /*空*/ } int main() { struct BOOK aa[10]={{"C++",38.5,"97870654"},{"python",40.0,"97970658"},{"java",18.9,"97870655"}}; int n=3,i; sort(aa,n) for(i=0;i<n;i++) printf("%s %.2f %s\n",aa[i].name,aa[i].price,aa[i].issn); return 0; }
时间: 2024-03-02 14:48:10 浏览: 165
请看下面的程序:
```c
#include <stdio.h>
struct BOOK {
char name[30];
double price;
char issn[30];
};
void sort(struct BOOK a[], int n);
int main() {
struct BOOK aa[10] = {{"C++", 38.5, "97870654"}, {"python", 40.0, "97970658"}, {"java", 18.9, "97870655"}};
int n = 3, i;
sort(aa, n);
for (i = 0; i < n; i++)
printf("%s %.2f %s\n", aa[i].name, aa[i].price, aa[i].issn);
return 0;
}
void sort(struct BOOK a[], int n) {
int i, j;
struct BOOK temp;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (a[j].price < a[j + 1].price) {
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
}
```
程序输出:
```
python 40.00 97970658
C++ 38.50 97870654
java 18.90 97870655
```
阅读全文