以下是使用C语言编写的程序,用于从键盘输入书籍名称和定价,然后查找定价最高和最低的书籍,并输出它们的名称和定价。 c
时间: 2023-11-22 19:52:18 浏览: 33
```c
#include <stdio.h>
#include <string.h>
#define MAX_BOOKS 100
struct book {
char name[50];
double price;
};
int main() {
struct book books[MAX_BOOKS];
int num_books;
// 获取书籍数量
printf("请输入书籍数量:");
scanf("%d", &num_books);
// 获取书籍信息
for (int i = 0; i < num_books; i++) {
printf("请输入第 %d 本书的名称和价格,用空格分隔:", i+1);
scanf("%s %lf", books[i].name, &books[i].price);
}
// 查找最高价和最低价的书籍
int max_index = 0, min_index = 0;
for (int i = 1; i < num_books; i++) {
if (books[i].price > books[max_index].price) {
max_index = i;
}
if (books[i].price < books[min_index].price) {
min_index = i;
}
}
// 输出结果
printf("定价最高的书籍是 %s,价格为 %.2lf\n", books[max_index].name, books[max_index].price);
printf("定价最低的书籍是 %s,价格为 %.2lf\n", books[min_index].name, books[min_index].price);
return 0;
}
```
这个程序使用了结构体来存储书籍的名称和价格,然后查找出定价最高和最低的书籍。在输入书籍名称时,我们使用了 `scanf()` 函数,并使用 `%s` 标识符来读取字符串,而使用 `%lf` 标识符来读取价格。最后,我们使用 `printf()` 函数将结果输出到屏幕上。