我要一个完整的代码
时间: 2024-02-11 13:05:28 浏览: 110
好的,下面是一个完整的代码实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct book {
char name[100]; // 书名
char isbn[20]; // ISBN
char author[50]; // 作者
char publisher[50]; // 出版社
char pubdate[20]; // 出版时间
char indate[20]; // 入库时间
int stock; // 库存量
int borrowed; // 借阅数
};
struct node {
struct book data;
struct node *next;
};
void insert(struct node **head, struct book data) {
struct node *new_node = (struct node *)malloc(sizeof(struct node));
new_node->data = data;
new_node->next = *head;
*head = new_node;
}
void delete(struct node **head, char *name) {
struct node *prev = NULL, *curr = *head;
while (curr != NULL) {
if (strcmp(curr->data.name, name) == 0) {
if (prev == NULL) {
*head = curr->next;
} else {
prev->next = curr->next;
}
free(curr);
return;
}
prev = curr;
curr = curr->next;
}
}
void modify(struct node **head, char *name) {
struct node *curr = *head;
while (curr != NULL) {
if (strcmp(curr->data.name, name) == 0) {
printf("请输入修改后的书名:");
scanf("%s", curr->data.name);
printf("请输入ISBN:");
scanf("%s", curr->data.isbn);
printf("请输入作者:");
scanf("%s", curr->data.author);
printf("请输入出版社:");
scanf("%s", curr->data.publisher);
printf("请输入出版时间:");
scanf("%s", curr->data.pubdate);
printf("请输入入库时间:");
scanf("%s", curr->data.indate);
printf("请输入库存量:");
scanf("%d", &curr->data.stock);
printf("请输入借阅数:");
scanf("%d", &curr->data.borrowed);
return;
}
curr = curr->next;
}
}
void search(struct node *head, char *name) {
struct node *curr = head;
while (curr != NULL) {
if (strcmp(curr->data.name, name) == 0) {
printf("查询到的图书信息如下:\n");
printf("==============================\n");
printf("书名:%s\n", curr->data.name);
printf("ISBN:%s\n", curr->data.isbn);
printf("作者:%s\n", curr->data.author);
printf("出版社:%s\n", curr->data.publisher);
printf("出版时间:%s\n", curr->data.pubdate);
printf("入库时间:%s\n", curr->data.indate);
printf("库存量:%d\n", curr->data.stock);
printf("借阅数:%d\n", curr->data.borrowed);
printf("==============================\n");
return;
}
curr = curr->next;
}
printf("找不到该图书!\n");
}
void borrow(struct node **head, char *name) {
struct node *curr = *head;
while (curr != NULL) {
if (strcmp(curr->data.name, name) == 0) {
curr->data.borrowed++;
printf("借阅成功!\n");
return;
}
curr = curr->next;
}
printf("找不到该图书!\n");
}
void return_book(struct node **head, char *name) {
struct node *curr = *head;
while (curr != NULL) {
if (strcmp(curr->data.name, name) == 0) {
curr->data.borrowed--;
printf("还书成功!\n");
return;
}
curr = curr->next;
}
printf("找不到该图书!\n");
}
int count_borrowed(struct node *head) {
int count = 0;
struct node *curr = head;
while (curr != NULL) {
count += curr->data.borrowed;
curr = curr->next;
}
return count;
}
void print(struct book data) {
printf("书名:%s\n", data.name);
printf("ISBN:%s\n", data.isbn);
printf("作者:%s\n", data.author);
printf("出版社:%s\n", data.publisher);
printf("出版时间:%s\n", data.pubdate);
printf("入库时间:%s\n", data.indate);
printf("库存量:%d\n", data.stock);
printf("借阅数:%d\n", data.borrowed);
printf("\n");
}
int main() {
struct node *head = NULL;
// 录入图书信息
struct book b1 = {"C程序设计", "9787115544917", "谭浩强", "人民邮电出版社", "2019-01", "2019-01-01", 100, 0};
struct book b2 = {"数据结构", "9787115485470", "严蔚敏", "清华大学出版社", "2019-06", "2019-06-01", 50, 0};
struct book b3 = {"计算机组成原理", "9787121375527", "唐朔飞", "电子工业出版社", "2019-02", "2019-02-01", 80, 0};
struct book b4 = {"计算机网络", "9787115476881", "谢希仁", "清华大学出版社", "2019-03", "2019-03-01", 70, 0};
struct book b5 = {"操作系统", "9787115279469", "汤小丹", "人民邮电出版社", "2019-04", "2019-04-01", 90, 0};
struct book b6 = {"计算机图形学", "9787302072076", "F.S.Hill Jr", "清华大学出版社", "2019-05", "2019-05-01", 60, 0};
struct book b7 = {"计算机算法设计与分析", "9787302339832", "王晓东", "清华大学出版社", "2019-06", "2019-06-01", 40, 0};
struct book b8 = {"计算机体系结构", "9787121220032", "曾钦尧", "电子工业出版社", "2019-07", "2019-07-01", 30, 0};
struct book b9 = {"编译原理", "9787115411370", "龙书铮", "电子工业出版社", "2019-08", "2019-08-01", 20, 0};
struct book b10 = {"软件工程", "9787115422574", "Roger S.Pressman", "电子工业出版社", "2019-09", "2019-09-01", 10, 0};
insert(&head, b1);
insert(&head, b2);
insert(&head, b3);
insert(&head, b4);
insert(&head, b5);
insert(&head, b6);
insert(&head, b7);
insert(&head, b8);
insert(&head, b9);
insert(&head, b10);
// 删除某一本图书信息
delete(&head, "软件工程");
// 修改某一本图书信息
modify(&head, "计算机体系结构");
// 查询图书信息
search(head, "计算机图形学");
// 借书
borrow(&head, "计算机算法设计与分析");
// 还书
return_book(&head, "计算机算法设计与分析");
// 统计借出的图书数目
int count = count_borrowed(head);
printf("借出的图书数目为:%d\n", count);
// 输出查询到的图书信息
search(head, "计算机算法设计与分析");
// 输出所有图书信息
struct node *curr = head;
while (curr != NULL) {
print(curr->data);
curr = curr->next;
}
return 0;
}
```
以上代码实现了录入图书信息、删除某一本图书信息、修改某一本图书信息、查询图书信息、借书、还书、统计借出的图书数目以及输出查询到的图书信息等操作。你可以根据自己的需求进行修改和完善。