简单c语言图书馆管理系统注册登录借书还书
时间: 2023-07-05 20:02:14 浏览: 62
图书馆管理系统是一个用C语言编写的简单程序,它实现了用户的注册、登录、借书和还书功能。
首先,用户需要进行注册。系统会要求输入用户名和密码,并将其保存在系统的用户数据库中。注册成功后,用户可以使用注册时设置的用户名和密码进行登录。
登录之后,用户可以选择借书或还书的功能。系统会显示图书的列表供用户选择。如果用户选择借书,系统会要求输入要借阅的图书编号,并检查图书是否可借。如果可以借阅,则会将图书的状态修改为"借出"并记录借阅者的信息。如果不可借阅,则会提示用户图书已借出或不存在。
如果用户选择还书,系统会要求输入要归还的图书编号。系统会检查图书是否是该用户借阅的,并将图书状态修改为"可借"。如果图书不是该用户借阅的,则会提示用户该图书不存在或未借阅。
用户还可选择退出系统,退出系统后需要重新登录才能再次使用系统功能。
总的来说,这个简单的图书馆管理系统实现了用户的注册、登录、借书和还书功能。用户通过注册账号和登录,可以借阅图书并在还书时归还图书。这个系统可以辅助图书馆进行图书的管理和借还记录的维护。
相关问题
C语言写图书管理系统,要有登录注册存储借书信息功能
好的,下面是一个简单的C语言图书管理系统的代码示例,包括登录、注册、存储和借书功能:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_USERS 100 // 最大用户数
#define MAX_BOOKS 1000 // 最大图书数
#define MAX_BORROWS 1000 // 最大借阅数
// 用户结构体
typedef struct {
char username[20]; // 用户名
char password[20]; // 密码
} User;
// 图书结构体
typedef struct {
char title[50]; // 书名
char author[20]; // 作者
char publisher[20]; // 出版社
int year; // 出版年份
int id; // 图书编号
int borrowed; // 是否借出
} Book;
// 借阅结构体
typedef struct {
char username[20]; // 用户名
int book_id; // 图书编号
} Borrow;
int user_count = 0; // 用户数
User users[MAX_USERS]; // 用户数组
int book_count = 0; // 图书数
Book books[MAX_BOOKS]; // 图书数组
int borrow_count = 0; // 借阅数
Borrow borrows[MAX_BORROWS]; // 借阅数组
// 登录函数
int login() {
char username[20], password[20];
printf("请输入用户名:");
scanf("%s", username);
printf("请输入密码:");
scanf("%s", password);
for (int i = 0; i < user_count; i++) {
if (strcmp(users[i].username, username) == 0 && strcmp(users[i].password, password) == 0) {
printf("登录成功!\n");
return i;
}
}
printf("用户名或密码错误!\n");
return -1;
}
// 注册函数
void register_user() {
if (user_count >= MAX_USERS) {
printf("用户数已达到上限!\n");
return;
}
char username[20], password[20];
printf("请输入用户名:");
scanf("%s", username);
printf("请输入密码:");
scanf("%s", password);
for (int i = 0; i < user_count; i++) {
if (strcmp(users[i].username, username) == 0) {
printf("该用户名已被注册!\n");
return;
}
}
strcpy(users[user_count].username, username);
strcpy(users[user_count].password, password);
user_count++;
printf("注册成功!\n");
}
// 添加图书函数
void add_book() {
if (book_count >= MAX_BOOKS) {
printf("图书数已达到上限!\n");
return;
}
char title[50], author[20], publisher[20];
int year;
printf("请输入书名:");
scanf("%s", title);
printf("请输入作者:");
scanf("%s", author);
printf("请输入出版社:");
scanf("%s", publisher);
printf("请输入出版年份:");
scanf("%d", &year);
books[book_count].id = book_count + 1;
strcpy(books[book_count].title, title);
strcpy(books[book_count].author, author);
strcpy(books[book_count].publisher, publisher);
books[book_count].year = year;
books[book_count].borrowed = 0;
book_count++;
printf("添加成功!\n");
}
// 显示所有图书函数
void show_books() {
printf("图书编号\t书名\t作者\t出版社\t出版年份\t是否借出\n");
for (int i = 0; i < book_count; i++) {
printf("%d\t%s\t%s\t%s\t%d\t%s\n", books[i].id, books[i].title, books[i].author, books[i].publisher, books[i].year, books[i].borrowed ? "是" : "否");
}
}
// 借书函数
void borrow_book(int user_index) {
if (borrow_count >= MAX_BORROWS) {
printf("借阅数已达到上限!\n");
return;
}
int book_id;
printf("请输入要借的图书编号:");
scanf("%d", &book_id);
if (book_id <= 0 || book_id > book_count) {
printf("该图书不存在!\n");
return;
}
if (books[book_id - 1].borrowed) {
printf("该图书已被借出!\n");
return;
}
strcpy(borrows[borrow_count].username, users[user_index].username);
borrows[borrow_count].book_id = book_id;
books[book_id - 1].borrowed = 1;
borrow_count++;
printf("借阅成功!\n");
}
// 还书函数
void return_book(int user_index) {
int book_id;
printf("请输入要还的图书编号:");
scanf("%d", &book_id);
if (book_id <= 0 || book_id > book_count) {
printf("该图书不存在!\n");
return;
}
for (int i = 0; i < borrow_count; i++) {
if (strcmp(borrows[i].username, users[user_index].username) == 0 && borrows[i].book_id == book_id) {
books[book_id - 1].borrowed = 0;
for (int j = i; j < borrow_count - 1; j++) {
borrows[j] = borrows[j + 1];
}
borrow_count--;
printf("还书成功!\n");
return;
}
}
printf("您没有借阅该图书!\n");
}
int main() {
int choice, user_index = -1;
while (1) {
printf("欢迎使用图书管理系统!\n");
printf("1.登录 2.注册 3.退出\n");
scanf("%d", &choice);
if (choice == 1) {
user_index = login();
if (user_index == -1) {
continue;
}
} else if (choice == 2) {
register_user();
continue;
} else if (choice == 3) {
break;
} else {
printf("无效的选项!\n");
continue;
}
while (1) {
printf("1.添加图书 2.显示所有图书 3.借书 4.还书 5.退出\n");
scanf("%d", &choice);
if (choice == 1) {
add_book();
} else if (choice == 2) {
show_books();
} else if (choice == 3) {
borrow_book(user_index);
} else if (choice == 4) {
return_book(user_index);
} else if (choice == 5) {
break;
} else {
printf("无效的选项!\n");
}
}
}
return 0;
}
```
需要注意的是,上述代码仅为示例,实际应用中还需要考虑更多的情况,比如输入的数据是否合法、数据存储方式等等。此外,代码中的数据存储是在内存中,如果需要实现永久存储,需要使用文件等外部存储方式。
图书管理系统实现借书,还书以及数据的统计C语言
好的,这是一个相对比较复杂的问题,需要涉及到数据结构和文件操作。不过我会尽力回答你的问题。
首先,我们需要定义图书的数据结构,可以使用结构体来实现:
```c
typedef struct {
char name[50];
char author[50];
int num;
int borrowed_num;
} Book;
```
其中,`name`表示书名,`author`表示作者,`num`表示该书总共的数量,`borrowed_num`表示目前被借出去的数量。
接下来,我们需要将图书信息存储在文件中,可以使用文件操作来实现。我们可以将图书信息存储在一个文本文件中,每一行表示一本书,每一行的信息用逗号隔开,例如:
```
计算机网络,谢希仁,10,2
C程序设计语言, Brian W.Kernighan,15,5
```
其中,第一列为书名,第二列为作者,第三列为总共的数量,第四列为目前被借出去的数量。
接下来,我们来实现图书的借书和还书功能。具体代码如下:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_BOOKS 100 // 最多支持100本书
typedef struct {
char name[50];
char author[50];
int num;
int borrowed_num;
} Book;
// 读取图书信息文件
int read_books(Book *books) {
FILE *fp = fopen("books.txt", "r");
if (fp == NULL) {
printf("Failed to read books file.\n");
return -1;
}
int i = 0;
char line[100];
while (fgets(line, sizeof(line), fp) != NULL) {
char *p = strtok(line, ",");
strcpy(books[i].name, p);
p = strtok(NULL, ",");
strcpy(books[i].author, p);
p = strtok(NULL, ",");
books[i].num = atoi(p);
p = strtok(NULL, ",");
books[i].borrowed_num = atoi(p);
i++;
}
fclose(fp);
return i;
}
// 写入图书信息文件
void write_books(Book *books, int num_books) {
FILE *fp = fopen("books.txt", "w");
if (fp == NULL) {
printf("Failed to write books file.\n");
return;
}
for (int i = 0; i < num_books; i++) {
fprintf(fp, "%s,%s,%d,%d\n", books[i].name, books[i].author, books[i].num, books[i].borrowed_num);
}
fclose(fp);
}
// 查找图书
int find_book(Book *books, int num_books, const char *name) {
for (int i = 0; i < num_books; i++) {
if (strcmp(books[i].name, name) == 0) {
return i;
}
}
return -1;
}
// 借书
void borrow_book(Book *books, int num_books, const char *name) {
int index = find_book(books, num_books, name);
if (index == -1) {
printf("Cannot find book %s.\n", name);
return;
}
if (books[index].num <= books[index].borrowed_num) {
printf("No available books of %s.\n", name);
return;
}
books[index].borrowed_num++;
printf("Borrow book %s successfully.\n", name);
write_books(books, num_books);
}
// 还书
void return_book(Book *books, int num_books, const char *name) {
int index = find_book(books, num_books, name);
if (index == -1) {
printf("Cannot find book %s.\n", name);
return;
}
if (books[index].borrowed_num == 0) {
printf("No borrowed books of %s.\n", name);
return;
}
books[index].borrowed_num--;
printf("Return book %s successfully.\n", name);
write_books(books, num_books);
}
// 统计图书信息
void summary_books(Book *books, int num_books) {
int total_num = 0;
int total_borrowed_num = 0;
for (int i = 0; i < num_books; i++) {
total_num += books[i].num;
total_borrowed_num += books[i].borrowed_num;
}
printf("Total number of books: %d\n", total_num);
printf("Total number of borrowed books: %d\n", total_borrowed_num);
}
int main() {
Book books[MAX_BOOKS];
int num_books = read_books(books);
// 借书示例
borrow_book(books, num_books, "计算机网络");
// 还书示例
return_book(books, num_books, "计算机网络");
// 统计图书信息
summary_books(books, num_books);
return 0;
}
```
在上面的代码中,`read_books()`函数用于读取图书信息文件,`write_books()`函数用于写入图书信息文件。`find_book()`函数用于查找图书,`borrow_book()`函数用于借书,`return_book()`函数用于还书,`summary_books()`函数用于统计图书信息。在`main()`函数中,我们演示了如何借书、还书和统计图书信息。
希望对你有所帮助!