用C语言写一个图书借阅系统,包括查看图书,添加图书,删除图书,借阅图书,归还图书,用户反馈等功能
时间: 2024-03-21 08:44:34 浏览: 121
好的,下面是一个简单的图书借阅系统的代码实现,包括上述提到的功能:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_BOOKS 100
#define MAX_USERS 100
#define MAX_BOOK_NAME 50
#define MAX_USER_NAME 20
typedef struct book {
char name[MAX_BOOK_NAME];
char author[MAX_USER_NAME];
char publisher[MAX_USER_NAME];
int pub_year;
int isbn;
float price;
int available;
} Book;
typedef struct user {
char name[MAX_USER_NAME];
int borrowed_books[MAX_BOOKS];
int borrowed_count;
} User;
Book books[MAX_BOOKS];
int book_count = 0;
User users[MAX_USERS];
int user_count = 0;
void add_book() {
Book book;
printf("Enter book name: ");
scanf("%s", book.name);
printf("Enter author name: ");
scanf("%s", book.author);
printf("Enter publisher name: ");
scanf("%s", book.publisher);
printf("Enter publication year: ");
scanf("%d", &book.pub_year);
printf("Enter ISBN: ");
scanf("%d", &book.isbn);
printf("Enter price: ");
scanf("%f", &book.price);
book.available = 1;
books[book_count++] = book;
printf("\nBook added successfully!\n\n");
}
void display_book(Book book) {
printf("Name: %s\n", book.name);
printf("Author: %s\n", book.author);
printf("Publisher: %s\n", book.publisher);
printf("Publication Year: %d\n", book.pub_year);
printf("ISBN: %d\n", book.isbn);
printf("Price: %.2f\n", book.price);
printf("Availability: %s\n", book.available ? "Available" : "Not Available");
}
void view_books() {
if (book_count == 0) {
printf("\nNo books found.\n\n");
return;
}
printf("\nList of books:\n\n");
for (int i = 0; i < book_count; i++) {
printf("Book %d:\n", i + 1);
display_book(books[i]);
printf("\n");
}
}
void delete_book() {
int isbn;
printf("Enter ISBN of book to be deleted: ");
scanf("%d", &isbn);
for (int i = 0; i < book_count; i++) {
if (books[i].isbn == isbn) {
for (int j = i; j < book_count - 1; j++) {
books[j] = books[j + 1];
}
book_count--;
printf("\nBook deleted successfully!\n\n");
return;
}
}
printf("\nBook not found.\n\n");
}
void borrow_book() {
int isbn;
char user_name[MAX_USER_NAME];
printf("Enter ISBN of book to be borrowed: ");
scanf("%d", &isbn);
printf("Enter your name: ");
scanf("%s", user_name);
for (int i = 0; i < book_count; i++) {
if (books[i].isbn == isbn) {
if (books[i].available == 0) {
printf("\nBook not available for borrowing.\n\n");
return;
}
for (int j = 0; j < user_count; j++) {
if (strcmp(users[j].name, user_name) == 0) {
if (users[j].borrowed_count == MAX_BOOKS) {
printf("\nYou have reached the maximum limit of borrowed books.\n\n");
return;
}
users[j].borrowed_books[users[j].borrowed_count++] = i;
books[i].available = 0;
printf("\nBook borrowed successfully!\n\n");
return;
}
}
User user;
strcpy(user.name, user_name);
user.borrowed_books[user.borrowed_count++] = i;
users[user_count++] = user;
books[i].available = 0;
printf("\nBook borrowed successfully!\n\n");
return;
}
}
printf("\nBook not found.\n\n");
}
void return_book() {
int isbn;
char user_name[MAX_USER_NAME];
printf("Enter ISBN of book to be returned: ");
scanf("%d", &isbn);
printf("Enter your name: ");
scanf("%s", user_name);
for (int i = 0; i < book_count; i++) {
if (books[i].isbn == isbn) {
for (int j = 0; j < user_count; j++) {
if (strcmp(users[j].name, user_name) == 0) {
for (int k = 0; k < users[j].borrowed_count; k++) {
if (users[j].borrowed_books[k] == i) {
users[j].borrowed_count--;
for (int l = k; l < users[j].borrowed_count; l++) {
users[j].borrowed_books[l] = users[j].borrowed_books[l + 1];
}
books[i].available = 1;
printf("\nBook returned successfully!\n\n");
return;
}
}
printf("\nYou have not borrowed this book.\n\n");
return;
}
}
printf("\nUser not found.\n\n");
return;
}
}
printf("\nBook not found.\n\n");
}
void user_feedback() {
char feedback[1000];
printf("Enter your feedback: ");
scanf("\n%[^\n]", feedback);
FILE *fp = fopen("feedback.txt", "a");
if (fp == NULL) {
printf("Error: Unable to open file for writing.\n");
return;
}
fprintf(fp, "%s\n", feedback);
fclose(fp);
printf("\nThank you for your feedback!\n\n");
}
int main() {
int choice;
while (1) {
printf("1. Add book\n");
printf("2. View books\n");
printf("3. Delete book\n");
printf("4. Borrow book\n");
printf("5. Return book\n");
printf("6. User feedback\n");
printf("7. Exit\n");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_book();
break;
case 2:
view_books();
break;
case 3:
delete_book();
break;
case 4:
borrow_book();
break;
case 5:
return_book();
break;
case 6:
user_feedback();
break;
case 7:
exit(0);
default:
printf("\nInvalid choice. Please try again.\n\n");
break;
}
}
return 0;
}
```
以上是一个简单的图书借阅系统的代码实现,包括查看图书、添加图书、删除图书、借阅图书、归还图书、用户反馈等功能。需要注意的是,这只是一个简单的示例,实际应用中可能需要更多的功能和更复杂的实现。
阅读全文