请你用c++写一个图书管理系统
时间: 2023-02-08 14:25:42 浏览: 118
好的,那么你可以参考以下的代码来编写你的图书管理系统。这是一个简单的例子,它可以帮助你了解如何使用 C 语言编写一个图书管理系统。
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_BOOKS 100
struct Book {
char title[100];
char author[50];
int pages;
int year;
};
struct Library {
struct Book books[MAX_BOOKS];
int num_books;
};
void print_book(struct Book* book) {
printf("书名:%s\n", book->title);
printf("作者:%s\n", book->author);
printf("页数:%d\n", book->pages);
printf("出版年份:%d\n", book->year);
}
int main() {
struct Library library;
library.num_books = 0;
while (1) {
printf("1. 添加图书\n");
printf("2. 查询图书\n");
printf("3. 退出\n");
int choice;
scanf("%d", &choice);
if (choice == 1) {
if (library.num_books == MAX_BOOKS) {
printf("图书馆已满!\n");
} else {
printf("请输入图书信息:\n");
printf("书名:");
scanf("%s", library.books[library.num_books].title);
printf("作者:");
scanf("%s", library.books[library.num_books].author);
printf("页数:");
scanf("%d", &library.books[library.num_books].pages);
printf("出版年份:");
scanf("%d", &library.books[library.num_books].year);
library.num_books++;
}
} else if (choice == 2) {
printf("请输入要查询的书名:");
char title[100];
scanf("%s", title);
int found = 0;
for (int i = 0; i < library.num_books; i++) {
if (strcmp(library.books[i].title, title) == 0) {
print_book(&library.books[i]);
found = 1;
break;
}
阅读全文