用c语言写图书借阅管理系统 主要分为两⼤功能: 1)图书管理(增加图书、查询图书、删除图书、图书借阅、还书); 2)会员管理(增加会员、查询会员、删除会员、借书信息);的程序代码
时间: 2023-10-20 13:41:59 浏览: 129
好的,以下是一个简单的C语言图书借阅管理系统的程序代码示例,您可以根据自己的需求进行修改和完善:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//定义图书结构体
typedef struct Book {
char id[10]; //图书编号
char name[50]; //图书名称
char author[50]; //图书作者
int amount; //图书数量
int borrowed; //借出数量
} Book;
//定义会员结构体
typedef struct Member {
char id[10]; //会员编号
char name[50]; //会员名称
int borrowedNum; //已借数量
} Member;
//定义全局变量
Book books[100]; //图书数组
Member members[100]; //会员数组
int bookNum = 0; //图书数量
int memberNum = 0; //会员数量
//函数声明
void addBook();
void queryBook();
void deleteBook();
void borrowBook();
void returnBook();
void addMember();
void queryMember();
void deleteMember();
void queryBorrowed();
int main() {
int choice;
do {
//显示菜单
printf("\n\nWelcome to the library management system!\n");
printf("1. Add book\n");
printf("2. Query book\n");
printf("3. Delete book\n");
printf("4. Borrow book\n");
printf("5. Return book\n");
printf("6. Add member\n");
printf("7. Query member\n");
printf("8. Delete member\n");
printf("9. Query borrowed books\n");
printf("0. Exit\n");
printf("Please enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
addBook();
break;
case 2:
queryBook();
break;
case 3:
deleteBook();
break;
case 4:
borrowBook();
break;
case 5:
returnBook();
break;
case 6:
addMember();
break;
case 7:
queryMember();
break;
case 8:
deleteMember();
break;
case 9:
queryBorrowed();
break;
case 0:
printf("Goodbye!\n");
break;
default:
printf("Invalid choice!\n");
break;
}
} while (choice != 0);
return 0;
}
//添加图书
void addBook() {
if (bookNum >= 100) {
printf("The library is full!\n");
return;
}
printf("Please enter the book id: ");
scanf("%s", books[bookNum].id);
printf("Please enter the book name: ");
scanf("%s", books[bookNum].name);
printf("Please enter the author: ");
scanf("%s", books[bookNum].author);
printf("Please enter the amount: ");
scanf("%d", &books[bookNum].amount);
books[bookNum].borrowed = 0;
bookNum++;
printf("Add book successfully!\n");
}
//查询图书
void queryBook() {
char id[10];
printf("Please enter the book id: ");
scanf("%s", id);
for (int i = 0; i < bookNum; i++) {
if (strcmp(books[i].id, id) == 0) {
printf("id: %s name: %s author: %s amount: %d borrowed: %d\n", books[i].id, books[i].name, books[i].author, books[i].amount, books[i].borrowed);
return;
}
}
printf("Book not found!\n");
}
//删除图书
void deleteBook() {
char id[10];
printf("Please enter the book id: ");
scanf("%s", id);
for (int i = 0; i < bookNum; i++) {
if (strcmp(books[i].id, id) == 0) {
for (int j = i; j < bookNum - 1; j++) {
books[j] = books[j + 1];
}
bookNum--;
printf("Delete book successfully!\n");
return;
}
}
printf("Book not found!\n");
}
//借书
void borrowBook() {
char bookId[10];
char memberId[10];
printf("Please enter the book id: ");
scanf("%s", bookId);
printf("Please enter the member id: ");
scanf("%s", memberId);
//查找图书
int bookIndex = -1;
for (int i = 0; i < bookNum; i++) {
if (strcmp(books[i].id, bookId) == 0) {
bookIndex = i;
break;
}
}
if (bookIndex == -1) {
printf("Book not found!\n");
return;
}
//查找会员
int memberIndex = -1;
for (int i = 0; i < memberNum; i++) {
if (strcmp(members[i].id, memberId) == 0) {
memberIndex = i;
break;
}
}
if (memberIndex == -1) {
printf("Member not found!\n");
return;
}
//判断图书数量是否足够
if (books[bookIndex].amount - books[bookIndex].borrowed <= 0) {
printf("The book has been borrowed out!\n");
return;
}
//借书
books[bookIndex].borrowed++;
members[memberIndex].borrowedNum++;
printf("Borrow book successfully!\n");
}
//还书
void returnBook() {
char bookId[10];
char memberId[10];
printf("Please enter the book id: ");
scanf("%s", bookId);
printf("Please enter the member id: ");
scanf("%s", memberId);
//查找图书
int bookIndex = -1;
for (int i = 0; i < bookNum; i++) {
if (strcmp(books[i].id, bookId) == 0) {
bookIndex = i;
break;
}
}
if (bookIndex == -1) {
printf("Book not found!\n");
return;
}
//查找会员
int memberIndex = -1;
for (int i = 0; i < memberNum; i++) {
if (strcmp(members[i].id, memberId) == 0) {
memberIndex = i;
break;
}
}
if (memberIndex == -1) {
printf("Member not found!\n");
return;
}
//还书
if (books[bookIndex].borrowed <= 0 || members[memberIndex].borrowedNum <= 0) {
printf("No books borrowed!\n");
return;
}
books[bookIndex].borrowed--;
members[memberIndex].borrowedNum--;
printf("Return book successfully!\n");
}
//添加会员
void addMember() {
if (memberNum >= 100) {
printf("The library is full!\n");
return;
}
printf("Please enter the member id: ");
scanf("%s", members[memberNum].id);
printf("Please enter the member name: ");
scanf("%s", members[memberNum].name);
members[memberNum].borrowedNum = 0;
memberNum++;
printf("Add member successfully!\n");
}
//查询会员
void queryMember() {
char id[10];
printf("Please enter the member id: ");
scanf("%s", id);
for (int i = 0; i < memberNum; i++) {
if (strcmp(members[i].id, id) == 0) {
printf("id: %s name: %s borrowedNum: %d\n", members[i].id, members[i].name, members[i].borrowedNum);
return;
}
}
printf("Member not found!\n");
}
//删除会员
void deleteMember() {
char id[10];
printf("Please enter the member id: ");
scanf("%s", id);
for (int i = 0; i < memberNum; i++) {
if (strcmp(members[i].id, id) == 0) {
for (int j = i; j < memberNum - 1; j++) {
members[j] = members[j + 1];
}
memberNum--;
printf("Delete member successfully!\n");
return;
}
}
printf("Member not found!\n");
}
//查询已借阅图书
void queryBorrowed() {
char memberId[10];
printf("Please enter the member id: ");
scanf("%s", memberId);
int memberIndex = -1;
for (int i = 0; i < memberNum; i++) {
if (strcmp(members[i].id, memberId) == 0) {
memberIndex = i;
break;
}
}
if (memberIndex == -1) {
printf("Member not found!\n");
return;
}
printf("Borrowed books of member %s:\n", memberId);
for (int i = 0; i < bookNum; i++) {
if (strcmp(books[i].id, memberId) == 0) {
printf("id: %s name: %s author: %s amount: %d borrowed: %d\n", books[i].id, books[i].name, books[i].author, books[i].amount, books[i].borrowed);
}
}
}
```
注意:以上代码仅为示例,可能存在一些不足之处,如有需要,请根据自己的需求进行修改和完善。
阅读全文