c语言代码:现有3本图书,每本图书的信息包括:书号、书名、作者、价格。 (1)编写input函数,实现从键盘输入若干本图书信息,存放在结构体数组中。 (2)编写output函数,遍历结构体数组,输出所有图书信息。 (3)编写findBookByID函数,实现按书号检索图书。 (4)编写findBookByName函数,实现按书名检索图书。 目前,已编写完成main函数、input函数和findBookByName函数,请编程实现output函数和findBookByID函数。程序结构及具体要求如下。 * 函数名称:findBookByID * 函数功能:按书号检索图书 * 形式参数:p,book型指针,book型一维数组首地址 * 形式参数:num,int型,图书数量 * 形式参数:id,int型,书号 * 返 回 值:返回book型指针。若没有找到图书,则返回空指针NULL;否则,仅返回第1本符合检索条件的图书 */ struct book * findBookByID(struct book *p, int num, int id){ // 请编程实现本函数
时间: 2024-02-12 21:08:39 浏览: 25
下面是实现findBookByID函数的代码:
```
struct book* findBookByID(struct book *p, int num, int id) {
int i;
for (i = 0; i < num; i++) {
if (p[i].bookID == id) {
return &p[i];
}
}
return NULL;
}
```
该函数的作用是按照书号检索图书,并返回符合条件的第一本图书的指针。函数中,p是一个book型指针,指向存放图书信息的结构体数组的首地址,num是图书的数量,id是要检索的书号。
函数实现的方法是遍历结构体数组,逐一比较每一本图书的书号是否与要检索的书号相同。如果找到符合条件的图书,则返回该图书的指针;否则返回空指针NULL。
相关问题
用c语言解决:现有3本图书,每本图书的信息包括:书号、书名、作者、价格。 (1)编写input函数,实现从键盘输入若干本图书信息,存放在结构体数组中。 (2)编写output函数,遍历结构体数组,输出所有图书信息。 (3)编写findBookByID函数,实现按书号检索图书。 (4)编写findBookByName函数,实现按书名检索图书。
好的,您的问题我已经记下了。以下是使用C语言编写的解决方案:
```c
#include <stdio.h>
#include <string.h>
#define MAX_BOOKS 3
#define MAX_NAME_LEN 50
#define MAX_AUTHOR_LEN 30
struct Book {
int id;
char name[MAX_NAME_LEN];
char author[MAX_AUTHOR_LEN];
float price;
};
struct Book books[MAX_BOOKS];
int count = 0;
// 从键盘输入若干本图书信息,存放在结构体数组中
void input() {
for (int i = 0; i < MAX_BOOKS; i++) {
printf("请输入第%d本书的信息:\n", i+1);
printf("书号:");
scanf("%d", &books[i].id);
printf("书名:");
scanf("%s", books[i].name);
printf("作者:");
scanf("%s", books[i].author);
printf("价格:");
scanf("%f", &books[i].price);
count++;
}
}
// 遍历结构体数组,输出所有图书信息
void output() {
printf("所有图书信息如下:\n");
for (int i = 0; i < count; i++) {
printf("书号:%d\n", books[i].id);
printf("书名:%s\n", books[i].name);
printf("作者:%s\n", books[i].author);
printf("价格:%.2f\n", books[i].price);
printf("\n");
}
}
// 按书号检索图书
void findBookByID(int id) {
for (int i = 0; i < count; i++) {
if (books[i].id == id) {
printf("书号:%d\n", books[i].id);
printf("书名:%s\n", books[i].name);
printf("作者:%s\n", books[i].author);
printf("价格:%.2f\n", books[i].price);
return;
}
}
printf("没有找到书号为%d的图书\n", id);
}
// 按书名检索图书
void findBookByName(char* name) {
for (int i = 0; i < count; i++) {
if (strcmp(books[i].name, name) == 0) {
printf("书号:%d\n", books[i].id);
printf("书名:%s\n", books[i].name);
printf("作者:%s\n", books[i].author);
printf("价格:%.2f\n", books[i].price);
return;
}
}
printf("没有找到书名为%s的图书\n", name);
}
int main() {
input();
output();
findBookByID(2);
findBookByName("C语言程序设计");
return 0;
}
```
以上代码中,我们先定义了一个结构体`Book`,用于存放每本图书的信息。然后定义了一个结构体数组`books`,用于存放所有图书的信息。`count`变量用于记录当前已经输入了多少本图书。
然后编写了`input`函数,可以从键盘输入若干本图书信息,并将其存放在`books`数组中。
接着编写了`output`函数,用于遍历`books`数组,输出所有图书的信息。
然后编写了`findBookByID`函数和`findBookByName`函数,分别实现按书号和按书名检索图书的功能。
最后在`main`函数中,我们调用了以上编写的函数,来实现对图书信息的输入、输出、检索等操作。
现有3本图书,每本图书的信息包括:书号、书名、作者、价格。 (1)编写input函数,实现从键盘输入若干本图书信息,存放在结构体数组中。 (2)编写output函数,遍历结构体数组,输出所有图书信息。 (3)编写findBookByID函数,实现按书号检索图书。 (4)编写findBookByName函数,实现按书名检索图书。 c语言
以下是实现上述功能的C语言代码:
```c
#include <stdio.h>
#include <string.h>
#define MAX_BOOKS 100
struct Book {
char id[20];
char name[50];
char author[50];
float price;
};
struct Book books[MAX_BOOKS];
int num_books = 0;
void input() {
while (num_books < MAX_BOOKS) {
printf("Enter book info (id name author price): ");
scanf("%s %s %s %f", books[num_books].id, books[num_books].name, books[num_books].author, &books[num_books].price);
num_books++;
char c;
printf("Do you want to continue (y/n)? ");
scanf(" %c", &c);
if (c == 'n' || c == 'N') {
break;
}
}
}
void output() {
printf("Book list:\n");
for (int i = 0; i < num_books; i++) {
printf("%s %s %s %.2f\n", books[i].id, books[i].name, books[i].author, books[i].price);
}
}
void findBookByID(char id[]) {
for (int i = 0; i < num_books; i++) {
if (strcmp(books[i].id, id) == 0) {
printf("%s %s %s %.2f\n", books[i].id, books[i].name, books[i].author, books[i].price);
return;
}
}
printf("Book not found\n");
}
void findBookByName(char name[]) {
for (int i = 0; i < num_books; i++) {
if (strcmp(books[i].name, name) == 0) {
printf("%s %s %s %.2f\n", books[i].id, books[i].name, books[i].author, books[i].price);
}
}
}
int main() {
input();
output();
char id[20];
printf("Enter book id to search: ");
scanf("%s", id);
findBookByID(id);
char name[50];
printf("Enter book name to search: ");
scanf("%s", name);
findBookByName(name);
return 0;
}
```
这里我们定义了一个结构体类型 `struct Book`,用于表示图书的信息。在 `input()` 函数中,我们使用循环从键盘输入图书信息,并将它们存储在 `books` 数组中。在 `output()` 函数中,我们遍历 `books` 数组,输出所有图书信息。在 `findBookByID()` 和 `findBookByName()` 函数中,我们分别按书号和书名检索图书,并输出符合条件的图书信息。在 `main()` 函数中,我们调用上述函数来完成对图书信息的输入、输出和检索。