c语言设计一个图书管理系统,其中包括管理员可以查看用户的账号,姓名,性别,和借阅的书籍数量,用户可以借阅归还书籍.并给出代码
时间: 2023-09-05 21:08:06 浏览: 199
图书馆管理系统C语言程序设计.doc
5星 · 资源好评率100%
以下是一个简单的图书管理系统的C语言代码实现,包括管理员和用户的操作:
```c
#include <stdio.h>
#include <string.h>
#define MAX_USERS 100
#define MAX_BOOKS 100
struct Book {
char name[100];
char author[100];
int quantity;
};
struct User {
char id[100];
char name[100];
char gender[10];
int borrowed;
struct Book *books[10];
};
struct Book books[MAX_BOOKS];
struct User users[MAX_USERS];
int num_books = 0;
int num_users = 0;
void add_book() {
struct Book book;
printf("Enter book name: ");
scanf("%s", book.name);
printf("Enter author name: ");
scanf("%s", book.author);
printf("Enter quantity: ");
scanf("%d", &book.quantity);
books[num_books++] = book;
printf("Book added successfully.\n");
}
void view_books() {
printf("List of books:\n");
for (int i = 0; i < num_books; i++) {
printf("%d. %s by %s (%d)\n", i+1, books[i].name, books[i].author, books[i].quantity);
}
}
void add_user() {
struct User user;
printf("Enter user ID: ");
scanf("%s", user.id);
printf("Enter user name: ");
scanf("%s", user.name);
printf("Enter gender: ");
scanf("%s", user.gender);
user.borrowed = 0;
users[num_users++] = user;
printf("User added successfully.\n");
}
void view_users() {
printf("List of users:\n");
for (int i = 0; i < num_users; i++) {
printf("%d. %s (%s, borrowed %d books)\n", i+1, users[i].name, users[i].gender, users[i].borrowed);
}
}
int find_user(char *id) {
for (int i = 0; i < num_users; i++) {
if (strcmp(users[i].id, id) == 0) {
return i;
}
}
return -1;
}
void borrow_book() {
char id[100], book_name[100];
printf("Enter user ID: ");
scanf("%s", id);
int user_index = find_user(id);
if (user_index == -1) {
printf("User not found.\n");
return;
}
printf("Enter book name: ");
scanf("%s", book_name);
int book_index = -1;
for (int i = 0; i < num_books; i++) {
if (strcmp(books[i].name, book_name) == 0) {
book_index = i;
break;
}
}
if (book_index == -1) {
printf("Book not found.\n");
return;
}
if (books[book_index].quantity == 0) {
printf("No copies of this book available.\n");
return;
}
books[book_index].quantity--;
users[user_index].books[users[user_index].borrowed++] = &books[book_index];
printf("Book borrowed successfully.\n");
}
void return_book() {
char id[100], book_name[100];
printf("Enter user ID: ");
scanf("%s", id);
int user_index = find_user(id);
if (user_index == -1) {
printf("User not found.\n");
return;
}
printf("Enter book name: ");
scanf("%s", book_name);
int book_index = -1;
for (int i = 0; i < users[user_index].borrowed; i++) {
if (strcmp(users[user_index].books[i]->name, book_name) == 0) {
book_index = i;
break;
}
}
if (book_index == -1) {
printf("Book not found.\n");
return;
}
books[book_index].quantity++;
users[user_index].books[book_index] = users[user_index].books[--users[user_index].borrowed];
printf("Book returned successfully.\n");
}
int main() {
int choice;
do {
printf("Menu:\n");
printf("1. Add book\n");
printf("2. View books\n");
printf("3. Add user\n");
printf("4. View users\n");
printf("5. Borrow book\n");
printf("6. Return book\n");
printf("0. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_book();
break;
case 2:
view_books();
break;
case 3:
add_user();
break;
case 4:
view_users();
break;
case 5:
borrow_book();
break;
case 6:
return_book();
break;
case 0:
printf("Exiting program...\n");
break;
default:
printf("Invalid choice. Please try again.\n");
break;
}
} while (choice != 0);
return 0;
}
```
这个程序可以实现添加书籍、查看书籍、添加用户、查看用户、借阅书籍和归还书籍等功能。请注意,这里只是一个简单的示例程序,实际的图书管理系统可能需要更多的功能和更复杂的实现。
阅读全文