C语言实现图书馆管理系统
该程序使用布局体`Book`来表现图书,其中包罗书名、作者、出版商、出版年份和可用性信息。`bookList`数组存储所有图书,`numBooks`变量存储当前图书数目。程序实现了以下功能:
- 添加书籍:向数组中添加一本书籍,输入书名、作者、出版商和出版年份;
- 搜索书籍:根据书名查找图书,并显示该书的详细信息;
- 显示所有书籍:显示所有图书的列表,包括书名、作者、出版商、出版年份和可用性信息;
- 借书:根据书名查找图书,并将其标记为不可用;
- 还书:根据书名查找图书,并将其标记为可用。
当添加的书籍凌驾数组巨细时,程序将提示图书馆已满。当搜索到的书籍不存在或已经借出时,程序将相应提示。
该程序使用循环菜单,用户可以重复实行上述操纵,直到选择退出。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_BOOKS 100
typedef struct Book {
char title;
char author;
char publisher;
int year;
int available;
} Book;
Book bookList;
int numBooks = 0;
void displayMenu() {
printf("Library Management System\n");
printf("=========================\n");
printf("1. Add a book\n");
printf("2. Search for a book\n");
printf("3. Display all books\n");
printf("4. Borrow a book\n");
printf("5. Return a book\n");
printf("6. Exit\n");
printf("Enter your choice: ");
}
void addBook() {
if (numBooks >= MAX_BOOKS) {
printf("Library is full.\n");
return;
}
printf("Enter book title: ");
scanf("%s", bookList.title);
printf("Enter book author: ");
scanf("%s", bookList.author);
printf("Enter book publisher: ");
scanf("%s", bookList.publisher);
printf("Enter book publication year: ");
scanf("%d", &bookList.year);
bookList.available = 1;
numBooks++;
printf("Book added successfully.\n");
}
void searchBook() {
char title;
printf("Enter book title: ");
scanf("%s", title);
for (int i = 0; i < numBooks; i++) {
if (strcmp(title, bookList.title) == 0) {
printf("Title: %s\n", bookList.title);
printf("Author: %s\n", bookList.author);
printf("Publisher: %s\n", bookList.publisher);
printf("Publication Year: %d\n", bookList.year);
printf("Availability: %s\n", bookList.available ? "Yes" : "No");
return;
}
}
printf("Book not found.\n");
}
void displayBooks() {
printf("Title\tAuthor\tPublisher\tPublication Year\tAvailability\n");
for (int i = 0; i < numBooks; i++) {
printf("%s\t%s\t%s\t%d\t%s\n", bookList.title, bookList.author, bookList.publisher, bookList.year, bookList.available ? "Yes" : "No");
}
}
void borrowBook() {
char title;
printf("Enter book title: ");
scanf("%s", title);
for (int i = 0; i < numBooks; i++) {
if (strcmp(title, bookList.title) == 0) {
if (bookList.available) {
bookList.available = 0;
printf("Book borrowed successfully.\n");
} else {
printf("Book not available.\n");
}
return;
}
}
printf("Book not found.\n");
}
void returnBook() {
char title;
printf("Enter book title: ");
scanf("%s", title);
for (int i = 0; i < numBooks; i++) {
if (strcmp(title, bookList.title) == 0) {
if (!bookList.available) {
bookList.available = 1;
printf("Book returned successfully.\n");
} else {
printf("Book already available.\n");
}
return;
}
}
printf("Book not found.\n");
}
int main() {
int choice;
do {
displayMenu();
scanf("%d", &choice);
switch (choice) {
case 1:
addBook();
break;
case 2:
searchBook();
break;
case 3:
displayBooks();
break;
case 4:
borrowBook();
break;
case 5:
returnBook();
break;
case 6:
printf("Goodbye.\n");
break;
default:
printf("Invalid choice.\n");
break;
}
} while (choice != 6);
return 0;
}
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页:
[1]