c代码实现图书管理系统
时间: 2023-09-09 18:05:46 浏览: 33
以下是一个简单的图书管理系统的 C 代码实现,包含基本的借书、还书、查看书籍信息等功能:
```
#include <stdio.h>
#include <string.h>
struct book {
char title[50];
char author[50];
int id;
int status;
} books[100];
int count = 0;
void add_book() {
printf("\nEnter book title: ");
scanf("%s", books[count].title);
printf("Enter book author: ");
scanf("%s", books[count].author);
printf("Enter book ID: ");
scanf("%d", &books[count].id);
books[count].status = 1;
count++;
printf("\nBook added successfully!\n");
}
void display_books() {
printf("\n%-20s%-20s%-20s%s\n", "Title", "Author", "ID", "Status");
for (int i = 0; i < count; i++) {
printf("%-20s%-20s%-20d", books[i].title, books[i].author, books[i].id);
if (books[i].status == 1) {
printf("Available\n");
} else {
printf("Not Available\n");
}
}
}
void borrow_book() {
int id;
printf("\nEnter book ID: ");
scanf("%d", &id);
for (int i = 0; i < count; i++) {
if (books[i].id == id && books[i].status == 1) {
books[i].status = 0;
printf("\nBook borrowed successfully!\n");
return;
}
}
printf("\nBook not available!\n");
}
void return_book() {
int id;
printf("\nEnter book ID: ");
scanf("%d", &id);
for (int i = 0; i < count; i++) {
if (books[i].id == id && books[i].status == 0) {
books[i].status = 1;
printf("\nBook returned successfully!\n");
return;
}
}
printf("\nBook not found or already returned!\n");
}
int main() {
int choice;
while (1) {
printf("\n1. Add book\n2. Display books\n3. Borrow book\n4. Return book\n5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_book();
break;
case 2:
display_books();
break;
case 3:
borrow_book();
break;
case 4:
return_book();
break;
case 5:
return 0;
default:
printf("\nInvalid choice! Try again.\n");
}
}
}
```
这段代码定义了一个书籍结构体 `book`,包含书名、作者、ID和状态(1 表示可借,0 表示已借出)。然后定义了四个功能函数,分别是添加书籍、显示所有书籍、借书和还书。最后在主函数中使用一个死循环来不断接收用户输入的选项,并调用相应的功能函数。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)