1、定义书籍
需要定义一个结构体来表示一本书,包含书名、作者、ISBN、出版日期等信息。可以根据需要增加或减少字段。
#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_BOOKS 100 #define MAX_TITLE_LEN 100 #define MAX_AUTHOR_LEN 100 typedef struct { int id; char title[MAX_TITLE_LEN]; char author[MAX_AUTHOR_LEN]; } Book; Book library[MAX_BOOKS]; int book_count = 0; int main() { printf("Hello, World!"); return 0; }
2、添加新书籍
需要实现一个函数,用于向系统中添加新的书籍。这个函数需要接收用户输入的书籍信息,并将其存储在合适的位置。
#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_BOOKS 100 #define MAX_TITLE_LEN 100 #define MAX_AUTHOR_LEN 100 typedef struct { int id; char title[MAX_TITLE_LEN]; char author[MAX_AUTHOR_LEN]; } Book; Book library[MAX_BOOKS]; int book_count = 0; void add_book() { if (book_count >= MAX_BOOKS) { printf("图书馆已满,无法添加更多书籍。\n"); return; } Book new_book; new_book.id = book_count + 1; printf("请输入书名: "); getchar(); // 清除缓冲区的换行符 fgets(new_book.title, MAX_TITLE_LEN, stdin); new_book.title[strcspn(new_book.title, "\n")] = '\0'; // 移除换行符 printf("请输入作者: "); fgets(new_book.author, MAX_AUTHOR_LEN, stdin); new_book.author[strcspn(new_book.author, "\n")] = '\0'; // 移除换行符 library[book_count] = new_book; book_count++; printf("书籍添加成功!\n"); } int main() { printf("Hello, World!"); return 0; }
3、删除书籍
通过遍历书籍库找到指定ID的书籍,然后将其后面的书籍向前移动以填补空缺,最后减少书籍总数。
#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_BOOKS 100 #define MAX_TITLE_LEN 100 #define MAX_AUTHOR_LEN 100 typedef struct { int id; char title[MAX_TITLE_LEN]; char author[MAX_AUTHOR_LEN]; } Book; Book library[MAX_BOOKS]; int book_count = 0; void delete_book() { int id; printf("请输入要删除的书籍ID: "); scanf("%d", &id); int found = 0; for (int i = 0; i < book_count; i++) { if (library[i].id == id) { found = 1; for (int j = i; j < book_count - 1; j++) { library[j] = library[j + 1]; } book_count--; printf("书籍删除成功!\n"); break; } } if (!found) { printf("未找到该ID的书籍。\n"); } } int main() { printf("Hello, World!"); return 0; }
4、查找书籍
定义了一个search_book函数,该函数接收一个Book数组(代表图书馆)、书籍数量和要查找的书籍ID作为参数。函数通过遍历图书馆数组,查找与给定ID匹配的书籍,并输出相关信息。
#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_BOOKS 100 #define MAX_TITLE_LEN 100 #define MAX_AUTHOR_LEN 100 typedef struct { int id; char title[MAX_TITLE_LEN]; char author[MAX_AUTHOR_LEN]; } Book; Book library[MAX_BOOKS]; int book_count = 0; void search_book() { char title[MAX_TITLE_LEN]; printf("请输入要查找的书名: "); getchar(); // 清除缓冲区的换行符 fgets(title, MAX_TITLE_LEN, stdin); title[strcspn(title, "\n")] = '\0'; // 移除换行符 int found = 0; for (int i = 0; i < book_count; i++) { if (strcmp(library[i].title, title) == 0) { printf("找到书籍: ID=%d, 书名=%s, 作者=%s\n", library[i].id, library[i].title, library[i].author); found = 1; } } if (!found) { printf("未找到该书籍。\n"); } } int main() { printf("Hello, World!"); return 0; }
5、显示所有书籍
book_count
表示图书馆中书籍的数量。display_books
函数通过一个for循环遍历图书馆中的每一本书,并打印其ID、书名和作者。
#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_BOOKS 100 #define MAX_TITLE_LEN 100 #define MAX_AUTHOR_LEN 100 typedef struct { int id; char title[MAX_TITLE_LEN]; char author[MAX_AUTHOR_LEN]; } Book; Book library[MAX_BOOKS]; int book_count = 0; void display_books() { if (book_count == 0) { printf("图书馆中没有书籍。\n"); return; } printf("图书馆中的书籍:\n"); for (int i = 0; i < book_count; i++) { printf("ID=%d, 书名=%s, 作者=%s\n", library[i].id, library[i].title, library[i].author); } } int main() { printf("Hello, World!"); return 0; }
6、完整代码
一个简易的图书管理系统,可以根据自己的需求进行扩展和完善。
#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_BOOKS 100 #define MAX_TITLE_LEN 100 #define MAX_AUTHOR_LEN 100 typedef struct { int id; char title[MAX_TITLE_LEN]; char author[MAX_AUTHOR_LEN]; } Book; Book library[MAX_BOOKS]; int book_count = 0; void add_book() { if (book_count >= MAX_BOOKS) { printf("图书馆已满,无法添加更多书籍。\n"); return; } Book new_book; new_book.id = book_count + 1; printf("请输入书名: "); getchar(); // 清除缓冲区的换行符 fgets(new_book.title, MAX_TITLE_LEN, stdin); new_book.title[strcspn(new_book.title, "\n")] = '\0'; // 移除换行符 printf("请输入作者: "); fgets(new_book.author, MAX_AUTHOR_LEN, stdin); new_book.author[strcspn(new_book.author, "\n")] = '\0'; // 移除换行符 library[book_count] = new_book; book_count++; printf("书籍添加成功!\n"); } void delete_book() { int id; printf("请输入要删除的书籍ID: "); scanf("%d", &id); int found = 0; for (int i = 0; i < book_count; i++) { if (library[i].id == id) { found = 1; for (int j = i; j < book_count - 1; j++) { library[j] = library[j + 1]; } book_count--; printf("书籍删除成功!\n"); break; } } if (!found) { printf("未找到该ID的书籍。\n"); } } void search_book() { char title[MAX_TITLE_LEN]; printf("请输入要查找的书名: "); getchar(); // 清除缓冲区的换行符 fgets(title, MAX_TITLE_LEN, stdin); title[strcspn(title, "\n")] = '\0'; // 移除换行符 int found = 0; for (int i = 0; i < book_count; i++) { if (strcmp(library[i].title, title) == 0) { printf("找到书籍: ID=%d, 书名=%s, 作者=%s\n", library[i].id, library[i].title, library[i].author); found = 1; } } if (!found) { printf("未找到该书籍。\n"); } } void display_books() { if (book_count == 0) { printf("图书馆中没有书籍。\n"); return; } printf("图书馆中的书籍:\n"); for (int i = 0; i < book_count; i++) { printf("ID=%d, 书名=%s, 作者=%s\n", library[i].id, library[i].title, library[i].author); } } int main() { int choice; do { printf("\n图书管理系统\n"); printf("1. 添加书籍\n"); printf("2. 删除书籍\n"); printf("3. 查找书籍\n"); printf("4. 显示所有书籍\n"); printf("5. 退出\n"); printf("请选择操作(1-5): "); scanf("%d", &choice); switch (choice) { case 1: add_book(); break; case 2: delete_book(); break; case 3: search_book(); break; case 4: display_books(); break; case 5: printf("退出系统。\n"); break; default: printf("无效的选择,请重新选择。\n"); } } while (choice != 5); return 0; }