数据结构课设C语言图书馆管理系统
时间: 2023-12-02 14:05:13 浏览: 68
以下是一个简单的C语言图书馆管理系统的示例,包括图书管理和会员管理的基本功能:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_BOOKS 1000
#define MAX_MEMBERS 1000
// 图书结构体
typedef struct {
char id[20]; // 书号
char name[50]; // 书名
char author[50]; // 第一作者
int edition; // 版次
char publisher[50]; // 出版社
int year; // 出版年
int borrowed; // 是否被借出
} Book;
// 会员结构体
typedef struct {
char id[20]; // 会员号
char name[50]; // 姓名
char phone[20]; // 电话
char email[50]; // 邮箱
char borrowed[20]; // 借阅的书号
} Member;
Book books[MAX_BOOKS];
Member members[MAX_MEMBERS];
int bookCount = 0;
int memberCount = 0;
// 添加图书
void addBook() {
if (bookCount >= MAX_BOOKS) {
printf("Error: The library is full.\n");
return;
}
Book book;
printf("Please enter the book information:\n");
printf("ID: ");
scanf("%s", book.id);
printf("Name: ");
scanf("%s", book.name);
printf("Author: ");
scanf("%s", book.author);
printf("Edition: ");
scanf("%d", &book.edition);
printf("Publisher: ");
scanf("%s", book.publisher);
printf("Year: ");
scanf("%d", &book.year);
book.borrowed = 0;
books[bookCount++] = book;
printf("The book has been added successfully.\n");
}
// 查询图书
void searchBook() {
char id[20];
printf("Please enter the book ID: ");
scanf("%s", id);
for (int i = 0; i < bookCount; i++) {
if (strcmp(books[i].id, id) == 0) {
printf("ID: %s\n", books[i].id);
printf("Name: %s\n", books[i].name);
printf("Author: %s\n", books[i].author);
printf("Edition: %d\n", books[i].edition);
printf("Publisher: %s\n", books[i].publisher);
printf("Year: %d\n", books[i].year);
if (books[i].borrowed) {
printf("Status: Borrowed\n");
} else {
printf("Status: Available\n");
}
return;
}
}
printf("Error: The book is not found.\n");
}
// 删除图书
void deleteBook() {
char id[20]; printf("Please enter the book ID: ");
scanf("%s", id);
for (int i = 0; i < bookCount; i++) {
if (strcmp(books[i].id, id) == 0) {
for (int j = i; j < bookCount - 1; j++) {
books[j] = books[j + 1];
}
bookCount--;
printf("The book has been deleted successfully.\n");
return;
}
}
printf("Error: The book is not found.\n");
}
// 借阅图书
void borrowBook() {
char id[20];
printf("Please enter the book ID: ");
scanf("%s", id);
for (int i = 0; i < bookCount; i++) {
if (strcmp(books[i].id, id) == 0) {
if (books[i].borrowed) {
printf("Error: The book is already borrowed.\n");
} else {
books[i].borrowed = 1;
printf("The book has been borrowed successfully.\n");
}
return;
}
}
printf("Error: The book is not found.\n");
}
// 归还图书
void returnBook() {
char id[20];
printf("Please enter the book ID: ");
scanf("%s", id);
for (int i = 0; i < bookCount; i++) {
if (strcmp(books[i].id, id) == 0) {
if (books[i].borrowed) {
books[i].borrowed = 0;
printf("The book has been returned successfully.\n");
} else {
printf("Error: The book is not borrowed.\n");
}
return;
}
}
printf("Error: The book is not found.\n");
}
// 添加会员
void addMember() {
if (memberCount >= MAX_MEMBERS) {
printf("Error: The member list is full.\n");
return;
}
Member member;
printf("Please enter the member information:\n");
printf("ID: ");
scanf("%s", member.id);
printf("Name: ");
scanf("%s", member.name);
printf("Phone: ");
scanf("%s", member.phone);
printf("Email: ");
scanf("%s", member.email);
member.borrowed[0] = '\0';
members[memberCount++] = member;
printf("The member has been added successfully.\n");
}
// 查询会员
void searchMember() {
char id[20];
printf("Please enter the member ID: ");
scanf("%s", id);
for (int i = 0; i < memberCount; i++) {
if (strcmp(members[i].id, id) == 0) {
printf("ID: %s\n", members[i].id);
printf("Name: %s\n", members[i].name);
printf("Phone: %s\n", members[i].phone);
printf("Email: %s\n", members[i].email);
printf("Borrowed book ID: %s\n", members[i].borrowed);
return;
}
}
printf("Error: The member is not found.\n");
}
// 删除会员
void deleteMember() {
char id[20];
printf("Please enter the member ID: ");
scanf("%s", id);
for (int i = 0; i < memberCount; i++) {
if (strcmp(members[i].id, id) == 0) {
for (int j = i; j < memberCount - 1; j++) {
members[j] = members[j + 1];
}
memberCount--;
printf("The member has been deleted successfully.\n");
return;
}
}
printf("Error: The member is not found.\n");
}
// 借阅信息
void borrowInfo() {
char id[20];
printf("Please enter the member ID: ");
scanf("%s", id);
for (int i = 0; i < memberCount; i++) {
if (strcmp(members[i].id, id) == 0) {
printf("Borrowed book ID: %s\n", members[i].borrowed);
return;
}
}
printf("Error: The member is not found.\n");
}
int main() {
int choice;
while (1) {
printf("Please select the operation:\n");
printf("1. Add book\n");
printf("2. Search book\n");
printf("3. Delete book\n");
printf("4. Borrow book\n");
printf("5. Return book\n");
printf("6. Add member\n");
printf("7. Search member\n");
printf("8. Delete member\n");
printf("9. Borrow info\n");
printf("0. Exit\n");
printf("Your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
addBook();
break;
case 2:
searchBook();
break;
case 3:
deleteBook();
break;
case 4:
borrowBook();
break;
case 5:
returnBook();
break;
case 6:
addMember();
break;
case 7:
searchMember();
break;
case 8:
deleteMember();
break;
case 9:
borrowInfo();
break;
case 0:
exit(0);
default:
printf("Error: Invalid choice.\n");
}
}
return 0;
}
```