图书馆管理系统C语言(用数组、循环、选择、字符串结构、结构体实现)
时间: 2023-08-09 14:05:25 浏览: 83
好的,下面是用C语言数组、循环、选择、字符串结构、结构体实现的图书馆管理系统。
首先,我们需要定义图书和读者的结构体,其数据成员可以根据实际需要进行定义,例如:
```
struct Book {
char title[50]; //书名
char author[20]; //作者
char publisher[20]; //出版社
int ISBN; //书籍编号
int quantity; //书籍数量
int borrowed; //已借出的数量
};
struct Reader {
char name[20]; //读者姓名
int ID; //读者ID号
int borrowed; //已借阅的数量
int limit; //最大借阅数量
};
```
然后,我们需要定义一些基本操作,如添加图书、添加读者、查找图书、查找读者、借阅图书和归还图书等操作。这些操作可以通过循环、选择结构和字符串操作来实现。
例如,添加图书的函数可以这样实现:
```
void add_book(struct Book books[], int *count) {
if (*count >= MAX_BOOKS) {
printf("The library is full.\n");
return;
}
printf("Please enter the title of the book: ");
scanf("%s", books[*count].title);
printf("Please enter the author of the book: ");
scanf("%s", books[*count].author);
printf("Please enter the publisher of the book: ");
scanf("%s", books[*count].publisher);
printf("Please enter the ISBN of the book: ");
scanf("%d", &books[*count].ISBN);
printf("Please enter the quantity of the book: ");
scanf("%d", &books[*count].quantity);
books[*count].borrowed = 0;
(*count)++;
printf("Successfully added the book.\n");
}
```
类似地,添加读者的函数可以这样实现:
```
void add_reader(struct Reader readers[], int *count) {
if (*count >= MAX_READERS) {
printf("The library is full.\n");
return;
}
printf("Please enter the name of the reader: ");
scanf("%s", readers[*count].name);
printf("Please enter the ID of the reader: ");
scanf("%d", &readers[*count].ID);
printf("Please enter the limit of the reader: ");
scanf("%d", &readers[*count].limit);
readers[*count].borrowed = 0;
(*count)++;
printf("Successfully added the reader.\n");
}
```
查询图书的函数可以这样实现:
```
void search_book(struct Book books[], int count) {
int ISBN;
printf("Please enter the ISBN of the book: ");
scanf("%d", &ISBN);
for (int i = 0; i < count; i++) {
if (books[i].ISBN == ISBN) {
printf("Title: %s\n", books[i].title);
printf("Author: %s\n", books[i].author);
printf("Publisher: %s\n", books[i].publisher);
printf("Quantity: %d\n", books[i].quantity);
printf("Borrowed: %d\n", books[i].borrowed);
return;
}
}
printf("The book is not found.\n");
}
```
查询读者的函数可以这样实现:
```
void search_reader(struct Reader readers[], int count) {
int ID;
printf("Please enter the ID of the reader: ");
scanf("%d", &ID);
for (int i = 0; i < count; i++) {
if (readers[i].ID == ID) {
printf("Name: %s\n", readers[i].name);
printf("Limit: %d\n", readers[i].limit);
printf("Borrowed: %d\n", readers[i].borrowed);
return;
}
}
printf("The reader is not found.\n");
}
```
借阅图书的函数可以这样实现:
```
void borrow_book(struct Book books[], int book_count, struct Reader readers[], int reader_count) {
int ISBN, ID;
printf("Please enter the ISBN of the book: ");
scanf("%d", &ISBN);
printf("Please enter the ID of the reader: ");
scanf("%d", &ID);
int book_index = -1;
int reader_index = -1;
for (int i = 0; i < book_count; i++) {
if (books[i].ISBN == ISBN) {
book_index = i;
break;
}
}
for (int i = 0; i < reader_count; i++) {
if (readers[i].ID == ID) {
reader_index = i;
break;
}
}
if (book_index == -1) {
printf("The book is not found.\n");
return;
}
if (reader_index == -1) {
printf("The reader is not found.\n");
return;
}
if (books[book_index].borrowed >= books[book_index].quantity) {
printf("The book is out of stock.\n");
return;
}
if (readers[reader_index].borrowed >= readers[reader_index].limit) {
printf("The reader has reached the maximum borrowing limit.\n");
return;
}
books[book_index].borrowed++;
readers[reader_index].borrowed++;
printf("Successfully borrowed the book.\n");
}
```
归还图书的函数可以这样实现:
```
void return_book(struct Book books[], int book_count, struct Reader readers[], int reader_count) {
int ISBN, ID;
printf("Please enter the ISBN of the book: ");
scanf("%d", &ISBN);
printf("Please enter the ID of the reader: ");
scanf("%d", &ID);
int book_index = -1;
int reader_index = -1;
for (int i = 0; i < book_count; i++) {
if (books[i].ISBN == ISBN) {
book_index = i;
break;
}
}
for (int i = 0; i < reader_count; i++) {
if (readers[i].ID == ID) {
reader_index = i;
break;
}
}
if (book_index == -1) {
printf("The book is not found.\n");
return;
}
if (reader_index == -1) {
printf("The reader is not found.\n");
return;
}
if (books[book_index].borrowed <= 0) {
printf("The book has not been borrowed.\n");
return;
}
if (readers[reader_index].borrowed <= 0) {
printf("The reader has not borrowed any book.\n");
return;
}
books[book_index].borrowed--;
readers[reader_index].borrowed--;
printf("Successfully returned the book.\n");
}
```
最后,我们还需要一个主函数来调用这些操作函数,例如:
```
int main() {
struct Book books[MAX_BOOKS];
struct Reader readers[MAX_READERS];
int book_count = 0;
int reader_count = 0;
int choice;
do {
printf("1. Add a book.\n");
printf("2. Add a reader.\n");
printf("3. Search for a book.\n");
printf("4. Search for a reader.\n");
printf("5. Borrow a book.\n");
printf("6. Return a book.\n");
printf("7. Exit.\n");
printf("Please enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_book(books, &book_count);
break;
case 2:
add_reader(readers, &reader_count);
break;
case 3:
search_book(books, book_count);
break;
case 4:
search_reader(readers, reader_count);
break;
case 5:
borrow_book(books, book_count, readers, reader_count);
break;
case 6:
return_book(books, book_count, readers, reader_count);
break;
case 7:
break;
default:
printf("Invalid choice.\n");
break;
}
} while (choice != 7);
return 0;
}
```
以上就是用C语言数组、循环、选择、字符串结构、结构体实现的图书馆管理系统。
阅读全文