图书的读者管理系统C语言
时间: 2023-11-23 17:22:23 浏览: 33
好的,您需要一个使用C语言编写的图书读者管理系统。一般来说,图书读者管理系统的功能包括:读者信息管理、图书信息管理、借阅记录管理、归还记录管理等。以下是一个简单的图书读者管理系统的示例:
```c
#include <stdio.h>
#include <string.h>
#define MAX_BOOKS 100
#define MAX_READERS 100
struct Book {
char title[50];
char author[50];
int id;
int borrowed_by;
};
struct Reader {
char name[50];
int id;
int borrowed_books[MAX_BOOKS];
};
struct Book books[MAX_BOOKS];
struct Reader readers[MAX_READERS];
int num_books = 0;
int num_readers = 0;
void add_book() {
if (num_books >= MAX_BOOKS) {
printf("Too many books!\n");
return;
}
printf("Enter book title: ");
scanf("%s", books[num_books].title);
printf("Enter book author: ");
scanf("%s", books[num_books].author);
books[num_books].id = num_books;
books[num_books].borrowed_by = -1;
num_books++;
printf("Book added successfully.\n");
}
void add_reader() {
if (num_readers >= MAX_READERS) {
printf("Too many readers!\n");
return;
}
printf("Enter reader name: ");
scanf("%s", readers[num_readers].name);
readers[num_readers].id = num_readers;
for (int i = 0; i < MAX_BOOKS; i++) {
readers[num_readers].borrowed_books[i] = -1;
}
num_readers++;
printf("Reader added successfully.\n");
}
void borrow_book() {
int book_id, reader_id;
printf("Enter book id: ");
scanf("%d", &book_id);
if (book_id < 0 || book_id >= num_books) {
printf("Invalid book id!\n");
return;
}
if (books[book_id].borrowed_by != -1) {
printf("Book is already borrowed!\n");
return;
}
printf("Enter reader id: ");
scanf("%d", &reader_id);
if (reader_id < 0 || reader_id >= num_readers) {
printf("Invalid reader id!\n");
return;
}
for (int i = 0; i < MAX_BOOKS; i++) {
if (readers[reader_id].borrowed_books[i] == -1) {
readers[reader_id].borrowed_books[i] = book_id;
books[book_id].borrowed_by = reader_id;
printf("Book borrowed successfully.\n");
return;
}
}
printf("Reader has borrowed too many books!\n");
}
void return_book() {
int book_id, reader_id;
printf("Enter book id: ");
scanf("%d", &book_id);
if (book_id < 0 || book_id >= num_books) {
printf("Invalid book id!\n");
return;
}
if (books[book_id].borrowed_by == -1) {
printf("Book is not borrowed!\n");
return;
}
reader_id = books[book_id].borrowed_by;
for (int i = 0; i < MAX_BOOKS; i++) {
if (readers[reader_id].borrowed_books[i] == book_id) {
readers[reader_id].borrowed_books[i] = -1;
books[book_id].borrowed_by = -1;
printf("Book returned successfully.\n");
return;
}
}
}
void list_books() {
printf("Books:\n");
for (int i = 0; i < num_books; i++) {
printf("%d: %s by %s", books[i].id, books[i].title, books[i].author);
if (books[i].borrowed_by != -1) {
printf(" (borrowed by %s)", readers[books[i].borrowed_by].name);
}
printf("\n");
}
}
void list_readers() {
printf("Readers:\n");
for (int i = 0; i < num_readers; i++) {
printf("%d: %s (borrows ", readers[i].id, readers[i].name);
int first = 1;
for (int j = 0; j < MAX_BOOKS; j++) {
if (readers[i].borrowed_books[j] != -1) {
if (!first) {
printf(", ");
}
printf("%s", books[readers[i].borrowed_books[j]].title);
first = 0;
}
}
printf(")\n");
}
}
int main() {
int choice;
while (1) {
printf("1. Add book\n");
printf("2. Add reader\n");
printf("3. Borrow book\n");
printf("4. Return book\n");
printf("5. List books\n");
printf("6. List readers\n");
printf("7. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_book();
break;
case 2:
add_reader();
break;
case 3:
borrow_book();
break;
case 4:
return_book();
break;
case 5:
list_books();
break;
case 6:
list_readers();
break;
case 7:
return 0;
default:
printf("Invalid choice!\n");
break;
}
}
}
```
这个示例程序包括添加图书、添加读者、借阅图书、归还图书、列出图书、列出读者等基本功能。您可以根据自己的需求对其进行修改和完善。