c语言图书管理系统
时间: 2023-07-03 14:07:53 浏览: 46
好的,下面是一个简单的C语言图书管理系统的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_BOOKS 100
struct book {
char title[50];
char author[50];
int year;
int num_copies;
};
struct library {
struct book books[MAX_BOOKS];
int num_books;
};
void add_book(struct library *lib) {
if (lib->num_books >= MAX_BOOKS) {
printf("The library is full!\n");
return;
}
struct book new_book;
printf("Enter the title of the book: ");
scanf("%s", new_book.title);
printf("Enter the author of the book: ");
scanf("%s", new_book.author);
printf("Enter the year of publication: ");
scanf("%d", &new_book.year);
printf("Enter the number of copies: ");
scanf("%d", &new_book.num_copies);
lib->books[lib->num_books++] = new_book;
printf("Book added successfully!\n");
}
void search_book(struct library *lib) {
char title[50];
printf("Enter the title of the book to search for: ");
scanf("%s", title);
int i;
for (i = 0; i < lib->num_books; i++) {
if (strcmp(lib->books[i].title, title) == 0) {
printf("Book found!\n");
printf("Title: %s\n", lib->books[i].title);
printf("Author: %s\n", lib->books[i].author);
printf("Year: %d\n", lib->books[i].year);
printf("Number of copies: %d\n", lib->books[i].num_copies);
return;
}
}
printf("Book not found!\n");
}
void display_books(struct library *lib) {
int i;
for (i = 0; i < lib->num_books; i++) {
printf("Title: %s\n", lib->books[i].title);
printf("Author: %s\n", lib->books[i].author);
printf("Year: %d\n", lib->books[i].year);
printf("Number of copies: %d\n", lib->books[i].num_copies);
printf("\n");
}
}
int main() {
struct library lib = {0};
int choice;
do {
printf("1. Add book\n");
printf("2. Search book\n");
printf("3. Display all books\n");
printf("4. Quit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_book(&lib);
break;
case 2:
search_book(&lib);
break;
case 3:
display_books(&lib);
break;
case 4:
printf("Goodbye!\n");
break;
default:
printf("Invalid choice! Please try again.\n");
break;
}
} while (choice != 4);
return 0;
}
```
这个图书管理系统包括添加图书、查找图书和显示所有图书等功能。可以根据需要进行修改和扩展。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)