用c语言制作图书管理系统
时间: 2023-06-13 15:03:04 浏览: 157
c语言实现图书管理系统
5星 · 资源好评率100%
要用C语言制作图书管理系统,需要考虑以下几个方面:
1. 数据存储:需要选择一种数据存储方式,例如使用文本文件、二进制文件,或者使用数据库等。建议使用文件存储,因为它比较简单,而且可以直接读取和修改。
2. 用户界面:需要设计一个用户界面,包括菜单、输入输出等。可以使用C语言提供的控制台输出和输入函数,也可以使用图形界面库。
3. 功能实现:需要实现图书管理系统的基本功能,例如添加图书、删除图书、查询图书、借阅图书等。建议使用结构体来定义图书信息,方便存储和操作。
下面是一个简单的图书管理系统的代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_BOOKS 100 // 最大图书数量
#define MAX_TITLE 50 // 书名最大长度
#define MAX_AUTHOR 20 // 作者最大长度
// 图书结构体
typedef struct {
char title[MAX_TITLE]; // 书名
char author[MAX_AUTHOR]; // 作者
int available; // 是否可借
} Book;
// 图书馆结构体
typedef struct {
Book books[MAX_BOOKS]; // 图书数组
int count; // 图书数量
} Library;
// 添加图书
void add_book(Library *lib) {
if (lib->count >= MAX_BOOKS) {
printf("The library is full!\n");
return;
}
Book book;
printf("Enter the book title: ");
scanf("%s", book.title);
printf("Enter the book author: ");
scanf("%s", book.author);
book.available = 1;
lib->books[lib->count] = book;
lib->count++;
printf("The book has been added to the library.\n");
}
// 删除图书
void delete_book(Library *lib) {
char title[MAX_TITLE];
printf("Enter the book title: ");
scanf("%s", title);
int found = 0;
for (int i = 0; i < lib->count; i++) {
if (strcmp(lib->books[i].title, title) == 0) {
found = 1;
lib->books[i] = lib->books[lib->count - 1];
lib->count--;
printf("The book has been deleted from the library.\n");
break;
}
}
if (!found) {
printf("The book was not found in the library.\n");
}
}
// 查询图书
void search_book(Library *lib) {
char title[MAX_TITLE];
printf("Enter the book title: ");
scanf("%s", title);
int found = 0;
for (int i = 0; i < lib->count; i++) {
if (strcmp(lib->books[i].title, title) == 0) {
found = 1;
printf("Book title: %s\n", lib->books[i].title);
printf("Book author: %s\n", lib->books[i].author);
printf("Book availability: %s\n", lib->books[i].available ? "available" : "not available");
break;
}
}
if (!found) {
printf("The book was not found in the library.\n");
}
}
// 借阅图书
void borrow_book(Library *lib) {
char title[MAX_TITLE];
printf("Enter the book title: ");
scanf("%s", title);
int found = 0;
for (int i = 0; i < lib->count; i++) {
if (strcmp(lib->books[i].title, title) == 0) {
found = 1;
if (lib->books[i].available) {
lib->books[i].available = 0;
printf("You have borrowed the book %s.\n", lib->books[i].title);
} else {
printf("The book %s is not available.\n", lib->books[i].title);
}
break;
}
}
if (!found) {
printf("The book was not found in the library.\n");
}
}
// 归还图书
void return_book(Library *lib) {
char title[MAX_TITLE];
printf("Enter the book title: ");
scanf("%s", title);
int found = 0;
for (int i = 0; i < lib->count; i++) {
if (strcmp(lib->books[i].title, title) == 0) {
found = 1;
if (!lib->books[i].available) {
lib->books[i].available = 1;
printf("You have returned the book %s.\n", lib->books[i].title);
} else {
printf("The book %s is already available.\n", lib->books[i].title);
}
break;
}
}
if (!found) {
printf("The book was not found in the library.\n");
}
}
// 显示菜单
void display_menu() {
printf("1. Add a book\n");
printf("2. Delete a book\n");
printf("3. Search for a book\n");
printf("4. Borrow a book\n");
printf("5. Return a book\n");
printf("6. Exit\n");
}
int main() {
Library lib = { .count = 0 };
int choice;
do {
display_menu();
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: add_book(&lib); break;
case 2: delete_book(&lib); break;
case 3: search_book(&lib); break;
case 4: borrow_book(&lib); break;
case 5: return_book(&lib); break;
case 6: break;
default: printf("Invalid choice. Please try again.\n"); break;
}
} while (choice != 6);
return 0;
}
```
该代码实现了基本的图书管理系统功能,使用了结构体来定义图书信息,使用了文件系统来存储数据。但是这个代码还有很多不足之处,例如没有进行错误处理、输入输出不够友好等,需要根据具体的需求进行优化。
阅读全文