这个图书管理系统包括了添加、查找、删除、编辑、显示和保存书籍信息的功能。书籍信息被保存在一个文本文件中,在程序启动时从文件中加载,退出程序时保存到文件中。用户可以通过命令行界面操作图书管理系统,并进行相应的功能操作。
一、代码
cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_BOOKS 100
struct Book {
char title[100];
char author[100];
int year;
};
struct Library {
struct Book books[MAX_BOOKS];
int count;
};
// 函数声明
void addBook(struct Library *library);
void searchBook(struct Library library);
void deleteBook(struct Library *library, char *title);
void editBook(struct Library *library, char *title);
void saveBooks(struct Library library, char *filename);
void loadBooks(struct Library *library, char *filename);
void displayBooks(struct Library library);
int main() {
struct Library library;
library.count = 0;
loadBooks(&library, "books.txt"); // 从文件加载书籍信息
int choice;
do {
printf("\nLibrary Management System\n");
printf("1. Add Book\n");
printf("2. Search Book\n");
printf("3. Delete Book\n");
printf("4. Edit Book\n");
printf("5. Display All Books\n");
printf("6. Save Books\n");
printf("7. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
addBook(&library); // 添加书籍
break;
case 2:
searchBook(library); // 查找书籍
break;
case 3: {
char title[100];
printf("Enter title of book to delete: ");
scanf("%s", title);
deleteBook(&library, title); // 删除书籍
break;
}
case 4: {
char title[100];
printf("Enter title of book to edit: ");
scanf("%s", title);
editBook(&library, title); // 编辑书籍信息
break;
}
case 5:
displayBooks(library); // 显示所有书籍
break;
case 6:
saveBooks(library, "books.txt"); // 保存书籍信息到文件
break;
case 7:
printf("Exiting program.\n");
break;
default:
printf("Invalid choice! Please enter a number from 1 to 7.\n");
}
} while (choice != 7);
return 0;
}
// 添加书籍
void addBook(struct Library *library) {
if (library->count >= MAX_BOOKS) {
printf("Library is full. Cannot add more books.\n");
return;
}
struct Book newBook;
printf("Enter title of book: ");
scanf("%s", newBook.title);
printf("Enter author of book: ");
scanf("%s", newBook.author);
printf("Enter year of publication: ");
scanf("%d", &newBook.year);
library->books[library->count] = newBook;
library->count++;
printf("Book added successfully.\n");
}
// 查找书籍
void searchBook(struct Library library) {
char title[100];
printf("Enter title of book to search: ");
scanf("%s", title);
int found = 0;
for (int i = 0; i < library.count; i++) {
if (strcmp(library.books[i].title, title) == 0) {
printf("Book found:\n");
printf("Title: %s\n", library.books[i].title);
printf("Author: %s\n", library.books[i].author);
printf("Year: %d\n", library.books[i].year);
found = 1;
break;
}
}
if (!found) {
printf("Book not found.\n");
}
}
// 删除书籍
void deleteBook(struct Library *library, char *title) {
int found = 0;
for (int i = 0; i < library->count; i++) {
if (strcmp(library->books[i].title, title) == 0) {
for (int j = i; j < library->count - 1; j++) {
library->books[j] = library->books[j + 1];
}
library->count--;
found = 1;
printf("Book deleted successfully.\n");
break;
}
}
if (!found) {
printf("Book not found.\n");
}
}
// 编辑书籍信息
void editBook(struct Library *library, char *title) {
int found = 0;
for (int i = 0; i < library->count; i++) {
if (strcmp(library->books[i].title, title) == 0) {
printf("Enter new title of book: ");
scanf("%s", library->books[i].title);
printf("Enter new author of book: ");
scanf("%s", library->books[i].author);
printf("Enter new year of publication: ");
scanf("%d", &library->books[i].year);
found = 1;
printf("Book edited successfully.\n");
break;
}
}
if (!found) {
printf("Book not found.\n");
}
}
// 保存书籍信息到文件
void saveBooks(struct Library library, char *filename) {
FILE *file = fopen(filename, "w");
if (file == NULL) {
printf("Error opening file.\n");
return;
}
for (int i = 0; i < library.count; i++) {
fprintf(file, "%s %s %d\n", library.books[i].title, library.books[i].author, library.books[i].year);
}
fclose(file);
printf("Books saved to file successfully.\n");
}
// 从文件加载书籍信息
void loadBooks(struct Library *library, char *filename) {
FILE *file = fopen(filename, "r");
if (file == NULL) {
printf("Error opening file.\n");
return;
}
while (!feof(file) && library->count < MAX_BOOKS) {
fscanf(file, "%s %s %d\n", library->books[library->count].title, library->books[library->count].author, &library->books[library->count].year);
library->count++;
}
fclose(file);
}
// 显示所有书籍
void displayBooks(struct Library library) {
printf("Books in library:\n");
for (int i = 0; i < library.count; i++) {
printf("Title: %s, Author: %s, Year: %d\n", library.books[i].title, library.books[i].author, library.books[i].year);
}
}
二、程序分析
数据结构定义
- Book 结构体: 包含书籍的标题、作者和出版年份等信息。
- Library 结构体: 包含一个 Book 结构体数组和一个计数器,用于跟踪图书馆中的书籍数量。
主函数 main()
- 初始化图书馆: 创建 Library 结构体变量,并将书籍数量计数器初始化为 0。
- 加载书籍信息: 调用
loadBooks()
函数从文件中加载之前保存的书籍信息。 - 用户界面: 使用 do-while 循环显示菜单,直到用户选择退出。
- 菜单选项: 用户可以选择添加书籍、查找书籍、删除书籍、编辑书籍、显示所有书籍、保存书籍到文件和退出程序。
- 根据用户选择调用相应函数: 根据用户选择调用不同的函数来执行相应的操作。
函数定义
- 添加书籍 (
addBook()
):用户输入书籍的标题、作者和出版年份,将新书籍添加到图书馆中。 - 查找书籍 (
searchBook()
):用户输入要查找的书籍标题,系统根据输入查找并显示书籍的详细信息。 - 删除书籍 (
deleteBook()
):用户输入要删除的书籍标题,系统根据输入删除相应的书籍。 - 编辑书籍 (
editBook()
):用户输入要编辑的书籍标题,系统根据输入允许用户编辑书籍的标题、作者和出版年份。 - 保存书籍 (
saveBooks()
):将图书馆中的书籍信息保存到文件中,以便下次程序启动时加载使用。 - 加载书籍 (
loadBooks()
):从文件中加载之前保存的书籍信息到图书馆中。 - 显示所有书籍 (
displayBooks()
):列出图书馆中所有书籍的详细信息。
文件操作
- 保存和加载书籍信息: 使用文件操作函数 (
fopen()
,fclose()
,fprintf()
,fscanf()
) 将书籍信息保存到文件或从文件中加载。
三、练习
把Library 结构体数组,改为链表来实现。