写一个图书管理系统c语言
时间: 2023-07-13 07:35:12 浏览: 45
好的,下面是一个简单的图书管理系统的C语言代码示例:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_BOOKS 100
typedef struct {
char title[100];
char author[50];
int year;
int copies;
} Book;
Book bookList[MAX_BOOKS];
int numBooks = 0;
void addBook() {
if (numBooks >= MAX_BOOKS) {
printf("Error: book list is full\n");
return;
}
Book newBook;
printf("Enter book title: ");
scanf(" %[^\n]s", newBook.title);
printf("Enter book author: ");
scanf(" %[^\n]s", newBook.author);
printf("Enter publication year: ");
scanf("%d", &newBook.year);
printf("Enter number of copies: ");
scanf("%d", &newBook.copies);
bookList[numBooks++] = newBook;
printf("Book added successfully!\n");
}
void displayBooks() {
if (numBooks == 0) {
printf("No books in library\n");
return;
}
printf("%-30s %-20s %-10s %-10s\n", "Title", "Author", "Year", "Copies");
for (int i = 0; i < numBooks; i++) {
printf("%-30s %-20s %-10d %-10d\n", bookList[i].title, bookList[i].author, bookList[i].year, bookList[i].copies);
}
}
void searchBooks() {
if (numBooks == 0) {
printf("No books in library\n");
return;
}
char query[100];
printf("Enter search query: ");
scanf(" %[^\n]s", query);
printf("%-30s %-20s %-10s %-10s\n", "Title", "Author", "Year", "Copies");
for (int i = 0; i < numBooks; i++) {
if (strstr(bookList[i].title, query) != NULL || strstr(bookList[i].author, query) != NULL) {
printf("%-30s %-20s %-10d %-10d\n", bookList[i].title, bookList[i].author, bookList[i].year, bookList[i].copies);
}
}
}
void borrowBook() {
if (numBooks == 0) {
printf("No books in library\n");
return;
}
char query[100];
printf("Enter book title to borrow: ");
scanf(" %[^\n]s", query);
for (int i = 0; i < numBooks; i++) {
if (strcmp(bookList[i].title, query) == 0) {
if (bookList[i].copies > 0) {
bookList[i].copies--;
printf("Book borrowed successfully!\n");
return;
} else {
printf("All copies of this book are borrowed\n");
return;
}
}
}
printf("Book not found\n");
}
void returnBook() {
if (numBooks == 0) {
printf("No books in library\n");
return;
}
char query[100];
printf("Enter book title to return: ");
scanf(" %[^\n]s", query);
for (int i = 0; i < numBooks; i++) {
if (strcmp(bookList[i].title, query) == 0) {
bookList[i].copies++;
printf("Book returned successfully!\n");
return;
}
}
printf("Book not found\n");
}
int main() {
int choice;
do {
printf("\nLibrary Management System\n");
printf("---------------------------\n");
printf("1. Add book\n");
printf("2. Display all books\n");
printf("3. Search books\n");
printf("4. Borrow book\n");
printf("5. Return book\n");
printf("0. Exit\n");
printf("Enter choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
addBook();
break;
case 2:
displayBooks();
break;
case 3:
searchBooks();
break;
case 4:
borrowBook();
break;
case 5:
returnBook();
break;
case 0:
printf("Exiting...\n");
break;
default:
printf("Invalid choice\n");
}
} while (choice != 0);
return 0;
}
```
这个图书管理系统可以实现添加、展示、搜索、借书和还书等基本功能。需要注意的是,这只是一个简单的示例代码,实际使用中需要根据具体需求进行修改和完善。