void Search(book*h)//书名查找 { char name[20]; book*p = h->next; printf("输入要查找的书名:"); scanf("%s",&name); while (p!=NULL) { if (strcmp(p->date.name,name)!=0) { p = p->next; } else { printf("ISBN:%s\n",p->date.ISBN); printf("书名:%s\n",p->date.name); printf("作者:%s\n",p->date.writer); printf("出版社:%s\n",p->date.ad); printf("价格:%s\n",p->date.price); return; } } if (p == NULL) { printf("没有查询到!\n"); } }
时间: 2024-04-01 09:33:35 浏览: 13
这是一个书名查找的函数,接收一个指向链表头节点的指针。它会要求用户输入要查找的书名,然后在链表中遍历查找,找到后输出该书的相关信息。如果没有找到则输出“没有查询到!”。其中,每个节点的数据结构体包含ISBN、书名、作者、出版社和价格等信息。
需要注意的是,在输入要查找的书名时,应该使用%s而不是&name。因为name本身就是一个数组,已经是一个指向首元素的指针了。
相关问题
#include <stdio.h> #include <string.h> #include <stdlib.h> #define max 1000 int num=0; struct book //书 { char name[20]; //名 double price; //价格 int lent=0; //是否已借 int id=NULL; //图书编号 struct book *next; }; struct user //用户结构 { char name[30]; //名字 int account ; //账号 int password; //密码 int borrow; //借书的id }; int lookupb(book *l,char name[20]) //书名查找,(有)返回id值(无)返回 NULL { int i=0; book *p=l->next ; for(i=0;i<num;i++) { if(strcmp(p->name ,name )==0) { return l->id ; break; } p=p->next ; } return NULL; } void out(book *l) //输出图书 { int i; book *p=l->next ; printf("name price"); for(i=1;i<=num;i++) { puts(p->name ); printf("%lf\n",p->price ); if(i%12==0) { printf("%d/%d",i,num/12); //i/总页数 } p=p->next ; } } void add() //管理员 注册新图书 { book *l,*s,*r; int i,n,k; if(num==0) { l=(book *)malloc(sizeof(book)); l->next =NULL; } r=l; printf("需要添加几本\n"); scanf("%d",&n); getchar(); //消化回车键 for(i=0;i<n;i++) { s=(book *)malloc(sizeof(book)); printf("请输入书名\n"); gets(s->name ); k=lookupb(l,s->name); if(k!=NULL) { printf("重复命名,请重新输入\n"); add(); } printf("%c",s->name ); printf("请输入价格\n"); scanf("%lf",&s->price ); getchar(); //消化回车键 strcpy(l->name,s->name); s->id =1; r->next =s; r=s; num++; printf("添加成功!"); system("pause"); } r->next=NULL; out(l); } int main() { add(); printf("%d\n",sizeof(book)); }
这段代码缺少了一些定义,例如 `book` 结构体的定义、`lookupb` 函数和 `out` 函数的参数类型,以及 `book` 结构体中的一些成员变量没有正确初始化(例如 `lent` 和 `id`)。此外,代码中存在一些语法错误和逻辑错误。
下面是经过修改后的代码:
```c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define max 1000
struct book {
char name[20]; // 名称
double price; // 价格
int lent; // 是否已借出
int id; // 图书编号
struct book *next;
};
struct user {
char name[30]; // 名字
int account; // 账号
int password; // 密码
int borrow; // 借书的id
};
int lookupb(struct book *l, char name[20]) {
int i = 0;
struct book *p = l->next;
for (i = 0; i < max; i++) {
if (strcmp(p->name, name) == 0) {
return p->id;
}
p = p->next;
}
return -1;
}
void out(struct book *l) {
int i;
struct book *p = l->next;
printf("name\tprice\n");
for (i = 1; i <= max; i++) {
printf("%s\t%.2f\n", p->name, p->price);
if (i % 12 == 0) {
printf("%d/%d\n", i, max / 12); // i/总页数
}
p = p->next;
}
}
void add() {
struct book *l, *s, *r;
int i, n, k;
if (max == 0) {
l = (struct book *)malloc(sizeof(struct book));
l->next = NULL;
}
r = l;
printf("需要添加几本:\n");
scanf("%d", &n);
getchar(); // 消化回车键
for (i = 0; i < n; i++) {
s = (struct book *)malloc(sizeof(struct book));
printf("请输入书名:\n");
gets(s->name);
k = lookupb(l, s->name);
if (k != -1) {
printf("重复命名,请重新输入\n");
add();
}
printf("请输入价格:\n");
scanf("%lf", &s->price);
getchar(); // 消化回车键
s->lent = 0;
s->id = i + 1;
r->next = s;
r = s;
printf("添加成功!\n");
}
r->next = NULL;
out(l);
}
int main() {
add();
return 0;
}
```
这段代码实现的功能是,管理员可以注册新书,输入书名和价格,系统自动分配编号,并将书名、价格、编号等信息存储在一个链表中。最后,输出所有已注册的图书信息。注意,这段代码并没有实现完整的图书馆管理系统,只是实现了其中的一个功能。如果需要实现完整的图书馆管理系统,还需要考虑很多其他方面的功能和细节。
void modifyBook(Book *head, char *ISBN, char *name, char *author, char *publisher, float price, int quantity) { Book *p = head->next; while (p != NULL && strcmp(p->ISBN, ISBN) != 0) { p = p->next; } if (p == NULL) { printf("未找到指定的图书信息!\n"); } else { if (strlen(name) > 0) { strcpy(p->name, name); } if (strlen(author) > 0) { strcpy(p->author, author); } if (strlen(publisher) > 0) { strcpy(p->publisher, publisher); } if (price > 0) { p->price = price; } if (quantity > 0) { p->quantity = quantity; } printf("图书信息修改成功!\n"); } }
这段代码是一个 C 语言函数,用于在链表中修改指定 ISBN 号的图书信息。它接收七个参数:一个指向链表头结点的指针 head,以及指向要修改的图书信息的 ISBN 号、书名、作者、出版社、价格和数量的指针或值。
函数的主体代码如下:
1. Book *p = head->next; // 定义一个指向链表第一个节点的指针 p
2. while (p != NULL && strcmp(p->ISBN, ISBN) != 0) { // 遍历链表,查找指定 ISBN 号的节点
3. p = p->next; // 更新 p 指针为当前节点的下一个节点
4. }
5. if (p == NULL) { // 如果遍历完整个链表都没有找到指定 ISBN 号的节点
6. printf("未找到指定的图书信息!\n");
7. } else { // 否则,找到了指定 ISBN 号的节点
8. if (strlen(name) > 0) { // 如果传入了要修改的书名
9. strcpy(p->name, name); // 将当前节点的书名修改为传入的书名
10. }
11. if (strlen(author) > 0) { // 如果传入了要修改的作者名
12. strcpy(p->author, author); // 将当前节点的作者名修改为传入的作者名
13. }
14. if (strlen(publisher) > 0) { // 如果传入了要修改的出版社名
15. strcpy(p->publisher, publisher); // 将当前节点的出版社名修改为传入的出版社名
16. }
17. if (price > 0) { // 如果传入了要修改的价格
18. p->price = price; // 将当前节点的价格修改为传入的价格
19. }
20. if (quantity > 0) { // 如果传入了要修改的数量
21. p->quantity = quantity; // 将当前节点的数量修改为传入的数量
22. }
23. printf("图书信息修改成功!\n");
24. }
在这段代码中,首先定义了一个指针变量 p,用于遍历链表。然后通过 while 循环遍历链表,直到找到指定 ISBN 号的节点或者遍历完整个链表。
如果遍历完整个链表都没有找到指定 ISBN 号的节点,函数会输出一个提示信息。否则,函数会根据传入的参数修改当前节点的图书信息,并输出一个修改成功的提示信息。
需要注意的是,该函数没有返回值,因为它直接修改了链表数据结构的内容。另外,使用该函数时需要确保传入的链表头结点的 next 指针指向链表的第一个节点,否则可能会引起错误。