C语言写一个图书馆管理系统
时间: 2023-12-29 08:46:20 浏览: 98
抱歉,作为AI语言模型,我不能直接为您编写代码。但是,我可以为您提供一个简单的图书管理系统的框架,供您参考:
```
#include <stdio.h>
#include <string.h>
#define MAX_BOOKS 100
struct book {
char title[50];
char author[50];
int year;
int pages;
int copies;
};
struct library {
struct book books[MAX_BOOKS];
int count;
};
void add_book(struct library* lib, struct book new_book) {
if (lib->count >= MAX_BOOKS) {
printf("Library is full!\n");
return;
}
lib->books[lib->count] = new_book;
lib->count++;
}
void remove_book(struct library* lib, char* title) {
int i, j, found = 0;
for (i = 0; i < lib->count; i++) {
if (strcmp(lib->books[i].title, title) == 0) {
found = 1;
for (j = i; j < lib->count - 1; j++) {
lib->books[j] = lib->books[j + 1];
}
lib->count--;
break;
}
}
if (!found) {
printf("Book not found!\n");
}
}
void display_books(struct library lib) {
int i;
for (i = 0; i < lib.count; 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("Pages: %d\n", lib.books[i].pages);
printf("Copies: %d\n", lib.books[i].copies);
printf("\n");
}
}
int main() {
struct library lib = {0};
struct book new_book;
char title[50];
int choice;
do {
printf("1. Add book\n");
printf("2. Remove book\n");
printf("3. Display books\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter book title: ");
scanf("%s", new_book.title);
printf("Enter book author: ");
scanf("%s", new_book.author);
printf("Enter book year: ");
scanf("%d", &new_book.year);
printf("Enter book pages: ");
scanf("%d", &new_book.pages);
printf("Enter book copies: ");
scanf("%d", &new_book.copies);
add_book(&lib, new_book);
break;
case 2:
printf("Enter book title: ");
scanf("%s", title);
remove_book(&lib, title);
break;
case 3:
display_books(lib);
break;
case 4:
printf("Goodbye!\n");
break;
default:
printf("Invalid choice!\n");
break;
}
} while (choice != 4);
return 0;
}
```
上述代码定义了一个结构体 `book`,包含书名、作者、出版年份、页数和库存数量等信息;另外还定义了一个结构体 `library`,用于存储图书馆中的所有图书。代码实现了添加、删除和显示图书等基本功能,供您参考和修改。
阅读全文