帮我用c语言写一个图书管理系统
时间: 2023-06-16 16:05:12 浏览: 80
好的,以下是一个简单的图书管理系统的代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_BOOKS 100
struct Book {
char title[50];
char author[50];
int id;
int availability;
};
struct Library {
struct Book books[MAX_BOOKS];
int num_books;
};
void add_book(struct Library* library);
void remove_book(struct Library* library, int book_id);
void display_books(struct Library* library);
void borrow_book(struct Library* library, int book_id);
void return_book(struct Library* library, int book_id);
int main() {
struct Library library;
library.num_books = 0;
int choice = 0;
do {
printf("\n1. Add book\n");
printf("2. Remove book\n");
printf("3. Display books\n");
printf("4. Borrow book\n");
printf("5. Return book\n");
printf("6. Exit\n");
printf("Enter choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
add_book(&library);
break;
case 2:
printf("Enter book ID to remove: ");
int book_id;
scanf("%d", &book_id);
remove_book(&library, book_id);
break;
case 3:
display_books(&library);
break;
case 4:
printf("Enter book ID to borrow: ");
scanf("%d", &book_id);
borrow_book(&library, book_id);
break;
case 5:
printf("Enter book ID to return: ");
scanf("%d", &book_id);
return_book(&library, book_id);
break;
case 6:
printf("Exiting program...\n");
break;
default:
printf("Invalid choice. Please try again.\n");
break;
}
} while (choice != 6);
return 0;
}
void add_book(struct Library* library) {
if (library->num_books >= MAX_BOOKS) {
printf("Library is full. Cannot add more books.\n");
return;
}
struct Book book;
printf("\nEnter book title: ");
scanf("%s", book.title);
printf("Enter author name: ");
scanf("%s", book.author);
printf("Enter book ID: ");
scanf("%d", &book.id);
book.availability = 1;
library->books[library->num_books] = book;
library->num_books++;
printf("Book added successfully.\n");
}
void remove_book(struct Library* library, int book_id) {
int i;
for (i = 0; i < library->num_books; i++) {
if (library->books[i].id == book_id) {
// Shift all the books after i one position to the left
int j;
for (j = i; j < library->num_books - 1; j++) {
library->books[j] = library->books[j + 1];
}
library->num_books--;
printf("Book removed successfully.\n");
return;
}
}
printf("Book not found.\n");
}
void display_books(struct Library* library) {
if (library->num_books == 0) {
printf("Library is empty.\n");
return;
}
printf("\nBooks in library:\n");
printf("ID\tTitle\t\tAuthor\t\tAvailability\n");
int i;
for (i = 0; i < library->num_books; i++) {
printf("%d\t%s\t%s\t%s\n", library->books[i].id, library->books[i].title,
library->books[i].author, library->books[i].availability == 1 ? "Available" : "Not available");
}
}
void borrow_book(struct Library* library, int book_id) {
int i;
for (i = 0; i < library->num_books; i++) {
if (library->books[i].id == book_id) {
if (library->books[i].availability == 1) {
library->books[i].availability = 0;
printf("Book borrowed successfully.\n");
return;
} else {
printf("Book is not available for borrowing.\n");
return;
}
}
}
printf("Book not found.\n");
}
void return_book(struct Library* library, int book_id) {
int i;
for (i = 0; i < library->num_books; i++) {
if (library->books[i].id == book_id) {
if (library->books[i].availability == 0) {
library->books[i].availability = 1;
printf("Book returned successfully.\n");
return;
} else {
printf("Book was not borrowed.\n");
return;
}
}
}
printf("Book not found.\n");
}
```
这个程序使用了结构体来表示图书和图书馆。具体来说,每本书都有一个标题、作者、ID 和可用性状态(1 表示可借,0 表示不可借),而图书馆则包含一个 Book 数组以及记录图书数量的 num_books 变量。程序中提供了以下功能:
- 添加图书(add_book)
- 移除图书(remove_book)
- 显示图书馆中的所有图书(display_books)
- 借阅图书(borrow_book)
- 归还图书(return_book)
你可以根据实际需求修改这些功能,也可以添加其他功能,例如搜索图书、排序图书等等。
阅读全文