c语言设计一个图书管理系统,其中包括管理员添加,修改,删除书籍和用户登录,用户借阅,用户还书,用户评论,显示图书数量,评论等信息的代码
时间: 2023-07-27 19:18:07 浏览: 47
以下是一个简单的图书管理系统的示例代码,仅供参考:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义图书结构体
typedef struct book {
char name[50]; // 书名
char author[20]; // 作者
int num; // 数量
float price; // 价格
} Book;
// 定义用户结构体
typedef struct user {
char name[20]; // 用户名
char password[20]; // 密码
} User;
// 全局变量
int book_count = 0; // 图书数量
Book books[100]; // 图书数组
int user_count = 0; // 用户数量
User users[10]; // 用户数组
// 函数声明
void add_book();
void modify_book();
void delete_book();
void login();
void borrow_book();
void return_book();
void comment();
void display_books();
void display_comments();
int main() {
int choice;
while (1) {
printf("\n=== 图书管理系统 ===\n");
printf("1. 管理员添加图书\n");
printf("2. 管理员修改图书\n");
printf("3. 管理员删除图书\n");
printf("4. 用户登录\n");
printf("5. 用户借阅图书\n");
printf("6. 用户还书\n");
printf("7. 用户评论\n");
printf("8. 显示图书数量和评论\n");
printf("0. 退出系统\n");
printf("请输入您的选择:");
scanf("%d", &choice);
switch (choice) {
case 1:
add_book();
break;
case 2:
modify_book();
break;
case 3:
delete_book();
break;
case 4:
login();
break;
case 5:
borrow_book();
break;
case 6:
return_book();
break;
case 7:
comment();
break;
case 8:
display_books();
display_comments();
break;
case 0:
printf("再见!\n");
exit(0);
default:
printf("无效的选择,请重新输入!\n");
break;
}
}
}
// 添加图书
void add_book() {
if (book_count >= 100) {
printf("图书数量已达上限,无法添加!\n");
return;
}
Book book;
printf("请输入书名:");
scanf("%s", book.name);
printf("请输入作者:");
scanf("%s", book.author);
printf("请输入数量:");
scanf("%d", &book.num);
printf("请输入价格:");
scanf("%f", &book.price);
books[book_count++] = book;
printf("添加图书成功!\n");
}
// 修改图书
void modify_book() {
char name[50];
printf("请输入要修改的书名:");
scanf("%s", name);
for (int i = 0; i < book_count; i++) {
if (strcmp(books[i].name, name) == 0) {
printf("请输入新的书名:");
scanf("%s", books[i].name);
printf("请输入新的作者:");
scanf("%s", books[i].author);
printf("请输入新的数量:");
scanf("%d", &books[i].num);
printf("请输入新的价格:");
scanf("%f", &books[i].price);
printf("修改图书成功!\n");
return;
}
}
printf("未找到该图书!\n");
}
// 删除图书
void delete_book() {
char name[50];
printf("请输入要删除的书名:");
scanf("%s", name);
for (int i = 0; i < book_count; i++) {
if (strcmp(books[i].name, name) == 0) {
for (int j = i; j < book_count - 1; j++) {
books[j] = books[j + 1];
}
book_count--;
printf("删除图书成功!\n");
return;
}
}
printf("未找到该图书!\n");
}
// 用户登录
void login() {
char name[20];
char password[20];
printf("请输入用户名:");
scanf("%s", name);
printf("请输入密码:");
scanf("%s", password);
for (int i = 0; i < user_count; i++) {
if (strcmp(users[i].name, name) == 0 && strcmp(users[i].password, password) == 0) {
printf("登录成功,欢迎您,%s!\n", name);
return;
}
}
printf("用户名或密码错误!\n");
}
// 用户借阅图书
void borrow_book() {
char name[50];
printf("请输入要借阅的书名:");
scanf("%s", name);
for (int i = 0; i < book_count; i++) {
if (strcmp(books[i].name, name) == 0) {
if (books[i].num <= 0) {
printf("该图书已借完!\n");
return;
}
books[i].num--;
printf("借阅图书成功!\n");
return;
}
}
printf("未找到该图书!\n");
}
// 用户还书
void return_book() {
char name[50];
printf("请输入要归还的书名:");
scanf("%s", name);
for (int i = 0; i < book_count; i++) {
if (strcmp(books[i].name, name) == 0) {
books[i].num++;
printf("还书成功!\n");
return;
}
}
printf("未找到该图书!\n");
}
// 用户评论
void comment() {
char name[50];
char comment[100];
printf("请输入要评论的书名:");
scanf("%s", name);
for (int i = 0; i < book_count; i++) {
if (strcmp(books[i].name, name) == 0) {
printf("请输入您的评论:");
scanf("%s", comment);
// 将评论追加到文件末尾
FILE *fp = fopen("comments.txt", "a");
fprintf(fp, "%s: %s\n", name, comment);
fclose(fp);
printf("评论成功!\n");
return;
}
}
printf("未找到该图书!\n");
}
// 显示图书数量
void display_books() {
printf("当前图书数量:%d\n", book_count);
}
// 显示评论
void display_comments() {
FILE *fp = fopen("comments.txt", "r");
if (fp == NULL) {
printf("暂无评论!\n");
return;
}
printf("当前评论:\n");
char line[100];
while (fgets(line, 100, fp) != NULL) {
printf("%s", line);
}
fclose(fp);
}
```
说明:
- 图书和用户信息都存储在数组中,没有使用数据库。
- 添加、修改、删除图书需要管理员权限,需要输入管理员用户名和密码。
- 用户登录后才能进行借阅、还书、评论等操作。
- 评论信息存储在文件中,每次添加评论都会将评论追加到文件末尾。
- 图书数量和评论信息都可以在系统中进行查看。