请帮我生成一个图书管理c语言代码
时间: 2023-06-01 12:03:05 浏览: 78
以下是一个简单的图书管理系统的C语言代码:
```
#include <stdio.h>
#include <string.h>
#define MAX_BOOKS 100
struct Book {
char title[50];
char author[50];
int year;
};
struct Book books[MAX_BOOKS];
int num_books = 0;
void add_book() {
struct Book book;
printf("Enter book title: ");
scanf("%s", book.title);
printf("Enter book author: ");
scanf("%s", book.author);
printf("Enter book year: ");
scanf("%d", &book.year);
books[num_books] = book;
num_books++;
printf("Book added successfully.\n");
}
void print_books() {
if (num_books == 0) {
printf("No books in the library.\n");
return;
}
printf("Books in the library:\n");
for (int i = 0; i < num_books; i++) {
printf("%s by %s (%d)\n", books[i].title, books[i].author, books[i].year);
}
}
void search_book() {
char title[50];
printf("Enter book title: ");
scanf("%s", title);
for (int i = 0; i < num_books; i++) {
if (strcmp(title, books[i].title) == 0) {
printf("%s by %s (%d)\n", books[i].title, books[i].author, books[i].year);
return;
}
}
printf("Book not found.\n");
}
int main() {
int choice;
do {
printf("1. Add book\n");
printf("2. Print books\n");
printf("3. Search book\n");
printf("4. Quit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_book();
break;
case 2:
print_books();
break;
case 3:
search_book();
break;
case 4:
printf("Goodbye!\n");
break;
default:
printf("Invalid choice.\n");
}
} while (choice != 4);
return 0;
}
```
阅读全文
相关推荐















