不要问我为什么是图书管理系统2.。。。
因为有图书管理系统1呀。。。
咦,,,
这句话为何似曾相识。。。
依旧。。。我的风格。。。
我只是源代码的创造者。。。
完整代码
void show_admin_menu(); /*显示管理员操作的菜单*/
void show_search_book_menu(); /*显示管理员查询图书的菜单*/
void show_user_menu(); /*显示普通用户操作的菜单*/
void admin_exit(); /*管理员退出系统*/
void user_exit(); /*用户退出系统*/
#define MAX_BOOK_NAME 20 /*书名最大长度*/
#define MAX_PUBLISHER 20 /*出版社最大长度*/
#define MAX_DATE 10 /*出版日期最大长度*/
#define MAX_AUTHOR 20 /*作者最大长度*/
#define MAX_ISBN 20 /*ISBN最大长度*/
typedef struct _book_info
{
char book_name[MAX_BOOK_NAME]; /*书名*/
char author[MAX_AUTHOR]; /*作者*/
char publisher[MAX_PUBLISHER]; /*出版社*/
char pub_date[MAX_DATE]; /*出版日期*/
char ISBN[MAX_ISBN]; /*ISBN*/
int pages; /*页数*/
} book_info;
typedef struct _book
{
book_info bi; /*图书基本信息*/
struct _book* next; /*指向下一本图书的指针*/
} book;
void init_book(); /*图书模块初始化*/
void load_books(); /*从图书文件中加载图书信息*/
void add_book(); /*新增图书*/
void view_book(); /*浏览所有图书*/
void delete_book(); /*删除图书*/
void save_books(); /*调用将图书信息保存到文件函数,给出提示信息*/
void clear_books(); /*从内存中清除图书链表信息*/
void search_book_by_name(); /*按书名查询图书*/
void search_book_by_author(); /*按作者查询图书*/
void search_book_by_publisher(); /*按出版社查询图书*/
void search_book_by_pubdate(); /*按出版日期查询图书*/
void search_book_by_isbn(); /*按ISBN查询图书*/
int findstr(char* source,char* str); /*在字符串source中查找字符串str,如果没有找到,返回-1,找到则返回str的起始位置*/
void save_books_to_file(); /*将图书信息保存到文件*/
book* get_last_book(); /*得到图书链表的最后一节点*/
book* get_previous_book(book* p); /*取得图书节点P的前一个节点*/
void input_book(book_info* info); /*输入一本图书信息*/
void show_book(book_info* info); /*显示图书信息*/
#define MAX_USERNAME 10 /*用户名最大长度*/
#define MAX_PASSWORD 10 /*密码最大长度*/
typedef enum _USER_TYPE
{
ADMIN = 0,
USER
}USER_TYPE;
typedef struct _user_info
{
char username[MAX_USERNAME]; /*用户名*/
char password[MAX_PASSWORD]; /*密码*/
USER_TYPE user_type; /*用户类型,0为管理员,1为普通用户*/
}user_info;
typedef struct _user
{
user_info ui; /*用户基本信息*/
struct _user* next; /*指向下一个用户的指针变量*/
}user;
void init_user(); /*用户模块初始化*/
void load_users(); /*从用户文件中加载用户信息*/
USER_TYPE login(); /*用户登录,返回用户类型*/
void add_user(); /*新增一个用户*/
void search_user(); /*查找一个*/
void save_users(); /*调用将用户信息保存到文件函数,给出提示信息*/
void clear_users(); /*从类存中清除用户链表信息*/
void save_users_to_file(); /*将用户信息保存到文件*/
user* get_last_user(); /*得到用户链表的最后一个节点*/
user* get_previous_user(user* p); /*得到用户节点P的前一个节点*/
user* find_user(char* name); /*从用户链表中按用户名查找一个用户信息*/
void show_user(user_info* info); /*显示一个用户信息*/
void input_user(user_info* info); /*输入一个用户信息*/
void delete_user(user* p); /*从链表中删除一个用户信息*/
void update_user(user* p); /*更新一个用户信息*/
#include<stdlib.h>
#include<stdio.h>
//#include"book.h"
//#include"user.h"
//#include"management.h"
#define MENU_ADMIN_COUNT 9 /*管理员操作主菜单的选项个数*/
#define MENU_SEARCH_BOOK_COUNT 6 /*管理员查询菜单的选项个数*/
#define MENU_USER_COUNT 7 /*普通用户操作主菜单的选项个数*/
char menu_title[] = "========================================\n"
"| 图书管理系统 |\n"
"+--------------------------------------+\n";
char menu_admin[] = "| |\n"
"|图书管理: |\n"
"| <1> 新增图书 |\n"
"| <2> 浏览图书 |\n"
"| <3> 查找图书 |\n"
"| <4> 删除图书 |\n"
"| <5> 保存图书 |\n"
"| |\n"
"|用户管理 |\n"
"| <6> 新增用户 |\n"
"| <7> 查找用户 |\n"
"| <8> 保存用户 |\n"
"| |\n"
"| <9> 退出系统 |\n"
"|+-------------------------------------+|\n";
void (* admin_func[])() =
{
add_book,
view_book,
show_search_book_menu,
delete_book,
save_books,
add_user,
search_user,
save_users,
admin_exit
};
char menu_admin_search_book[] = "| 查找图书: |\n"
"| <1>按书名查找 |\n"
"| <2>按作者查找 |\n"
"| <3>按出版社查找 |\n"
"| <4>按出版日期查找 |\n"
"| <5>按国际标准书号(ISBN)查找 |\n"
"| <6>返回主菜单 |\n"
"+--------------------------------------------+\n";
void (* admin_search_book_func[])() =
{
search_book_by_name,
search_book_by_author,
search_book_by_publisher,
search_book_by_pubdate,
search_book_by_isbn,
};
char menu_user[] = "| <1>浏览图书 |\n"
"| <2>按书名查找 |\n"
"| <3>按作者查找 |\n"
"| <4>按出版社查找 |\n"
"| <5>按出版日期查找 |\n"
"| <6>按国际标准书号(ISBN)查找 |\n"
"| <7>退出程序 |\n"
"+----------------------------------------------------------+\n";
void (* user_func[])() =
{
view_book,
search_book_by_name,
search_book_by_author,
search_book_by_publisher,
search_book_by_pubdate,
search_book_by_isbn,
user_exit
};
void show_admin_menu()
{
int selected = 0;
while(selected < 1 || selected > MENU_ADMIN_COUNT)
{
system("cls");
printf(menu_title);
printf(menu_admin);
printf(">请选择要进行的操作:");
scanf("%d",&selected);
if(selected < 1 || selected > MENU_ADMIN_COUNT)
{
printf(">输入错误!请选择[%d - %d]之间的数字!按任意键重试...",1,MENU_ADMIN_COUNT);
getchar ();
getchar ();
}
else
{
admin_func[selected - 1]();
}
selected = 0;
}
}
void show_search_book_menu()
{
int selected = 0;
while(selected < 1 || selected > MENU_SEARCH_BOOK_COUNT)
{
system("cls");
printf(menu_title);
printf(menu_admin_search_book);
printf(">请选择要进行的操作:");
scanf("%d", &selected);
/*用户输入6,退出该菜单项*/
if(selected == MENU_SEARCH_BOOK_COUNT)
{
break;
}
if(selected < 0 || selected > MENU_SEARCH_BOOK_COUNT)
{
printf(">输入错误!请选择[%d - %d]之间的数字!按任意键重试...",1,MENU_ADMIN_COUNT);
getchar ();
getchar ();
}
else
{
admin_search_book_func[selected - 1]();
}
selected = 0;
}
}
void show_user_menu()
{
int selected = 0;
while(selected < 1 || selected > MENU_USER_COUNT)
{
system("cls");
printf(menu_title);
printf(menu_user);
printf(">请选择要进行的操作:");
scanf("%d", &selected);
if(selected < 1 || selected > MENU_USER_COUNT)
{
printf(">输入错误!请选择[%d - %d]之间的数字!按任意键重试...",1,MENU_ADMIN_COUNT);
getchar ();
getchar ();
}
else
{
user_func[selected - 1]();
}
selected = 0;
}
}
void admin_exit()
{
char sure = 'N';
printf(">确定要退出吗?(y or n)");
getchar();
sure = getchar();
if(sure == 'y' ||sure == 'Y')
{
save_users_to_file(); /*将用户链表中的数据保存到文件*/
clear_users(); /*清空用户列表*/
save_books_to_file(); /*将图书链表中的数据保存到文件*/
clear_books(); /*清空图书链表*/
exit(0);
}
}
void user_exit()
{
char sure = 'N';
printf(">确定要退出吗?(y or n)");
getchar();
if (sure == 'y' || sure == 'Y')
{
clear_users(); /*清空用户链表*/
clear_books(); /*清空图书链表*/
exit (0);
}
}
int main()
{
char input = 'N';
init_user();
load_users();
init_book();
load_books();
printf("图书管理系统登陆...\n");
if (login() == ADMIN)
{
show_user_menu();
}
else
{
show_admin_menu();
}
}
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#define BOOK_FILE "books.dat"
#define USER_FILE "user.dat"
/*
#include "book.h"
#include "management.h"
#include "user.h"
*/
book* first_book = NULL;/*book结构体链表的头节点*/
user* first_user = NULL;
void add_book()
{
char try_again = 'Y';
book* p = NULL;
book* new_book = NULL;
while(try_again == 'Y' || try_again == 'y')
{
new_book = (book*)malloc(sizeof(book));/*创建一个 new_book*/
memset(new_book, 0, sizeof(book));/*初始化new_book*/
new_book->next = NULL;
printf(">新增图书...\n");
input_book(&(new_book->bi));/*调用函数 input_book()为 new_book 赋值*/
p = get_last_book();/*调用 get_last_book(), 取得链表中最后一个节点,赋值给p*/
if (p == NULL)/*如果链表为空*/
{
first_book = new_book;/*将new_book赋值给头节点*/
}
else
{
p->next = new_book;/*将p连到最后一个节点后*/
}
printf(">继续添加图书吗?(y or n):");
getchar();
try_again = getchar();
}
}
void view_book()
{
book*p = NULL;
char input = 'Y';
int count = 0;
while (input == 'y' || input == 'Y')
{
count = 0;
p = first_book;
printf("+-----------------------------------------+\n");
printf("| 书名 | 作者 |\n");
printf("+-----------------------------------------+\n");
while (p != NULL)
{
printf("|%20s|%20s|\n", p->bi.book_name, p->bi.author);
printf("+-----------------------------------------+\n");
count++;
if (count == 5)
{
count == 0;
printf(">显示下一页吗?(y or n):");
getchar();
input = getchar();
if (input != 'y' && input != 'y')
{
break;
}
}
p = p->next;
}
printf(">再次浏览图书吗?(y or n):");
getchar();
input = getchar();
}
}
void search_book_by_name()
{
book*p = NULL;
char s[MAX_BOOK_NAME] = {0};/*书名*/
char input = 'Y';
int count = 0;
int i = 0;
printf(">查找图书...\n");
while (input == 'Y' || input == 'y')
{
count = 0;
p = first_book; /* p指向第一个节点 */
memset(s, 0, MAX_BOOK_NAME); /*清空s */
printf(">请输入书名 (最大长度为 %d):", MAX_BOOK_NAME);
scanf("%s", s);
/*p不为空时,调用findstr函数查找书名中是否包含输入的字符串 s*/
/*这里实现了模糊查询 */
while(p != NULL)
{
if (findstr(p->bi.book_name, s) != -1)
{
show_book(&(p->bi));/*显示查到的图书信息*/
count++;
}
p = p->next;
}
if (count == 0)
{
printf(">没有找到图书 %s。继续找吗?(y or n):", s);
getchar();
input = getchar();
continue;
}
printf(">共找到%d本图书...\n", count);
printf(">继续查找吗?(y or n):");
getchar();
input = getchar();
}
}
void search_book_by_author()
{
book* p = NULL;
char s[MAX_AUTHOR] = {0}; /*作者信息*/
char input = 'Y';
int count = 0;
int i = 0;
printf(">查找图书...\n");
while (input == 'Y' || input == 'y')
{
count = 0;
p = first_book; /* p指向第一个节点 */
memset(s, 0, MAX_AUTHOR);
printf(">请输入作者(最大长度为 %d):", MAX_AUTHOR);
scanf("%s", s);
/* p不为空时,调用findstr函数查找书名是否包含输入的字符串 s*/
/*这里实现了模糊查询 */
while(p !=NULL)
{
if (findstr(p->bi.author, s) != -1)
{
show_book(&(p->bi));
count++;
}
p = p->next;
}
if (count == 0)
{
printf(">没有找到作者为 %s 的图书。继续查找吗?(y or n):", s);
getchar();
input = getchar();
continue;
}
printf(">共找到 %d 本书...\n, count");
printf(">继续查找吗?(y or n):");
getchar();
input = getchar();
}
}
void search_book_by_publisher()
{
book* p = NULL;
char s[MAX_PUBLISHER] = {0}; /*出版社信息*/
char input = 'Y';
int count = 0;
int i = 0;
printf(">查找图书...\n");
while (input == 'Y' || input == 'y')
{
count = 0;
p = first_book;
memset(s, 0, MAX_PUBLISHER);
printf(">请输入出版社(最大长度为 %d):", MAX_PUBLISHER);
scanf("%s", s);
while(p !=NULL)
{
if (findstr(p->bi.publisher, s) != -1)
{
show_book(&(p->bi));
count++;
}
p = p->next;
}
if (count == 0)
{
printf(">没有找到出版社为 %s 的图书。继续找吗?(y or n):", s);
getchar();
input = getchar();
continue;
}
printf(">共找到 %d 本图书...\n",count);
printf(">继续查找吗?(y or n):");
getchar();
input = getchar();
}
}
void search_book_by_pubdate()
{
book* p = NULL;
char s[MAX_DATE] = {0}; /*出版日期*/
char input = 'Y';
int count = 0;
int i = 0;
printf(">查找图书...\n");
while (input == 'Y' || input == 'y')
{
count = 0;
p = first_book;
memset(s, 0, MAX_DATE);
printf(">请输入出版日期 (最大长度为 %d):", MAX_DATE);
scanf("%s", s);
while(p != NULL)
{
if (findstr(p->bi.pub_date, s) != -1)
{
show_book(&(p->bi));
count++;
}
p = p->next;
}
if (count == 0)
{
printf(">没有找到出版日期为 %s 的图书。继续查找吗?(y or n):", s);
getchar();
input = getchar();
continue;
}
printf(">共找到 %d 本图书...\n", count);
printf(">继续查找吗?(y or n):");
input = getchar();
}
}
void search_book_by_isbn()
{
char input = 'Y';
char isbn[MAX_ISBN] = {0};
book* p = NULL;
book* result = NULL;
while(input == 'Y' || input == 'y')
{
printf(">查找图书...\n");
printf(">请输入ISBN (最大长度为 %d):", MAX_ISBN);
scanf("%s", isbn);
p = first_book; /*p指向第一个节点*/
result = NULL;
/*在图书链表中查找输入的ISBN是否存在*/
while (p !=NULL)
{
if(strcmp(p->bi.ISBN, isbn) == 0)
{
result = p;
break;
}
p = p->next;
}
if(result != NULL)
{
printf(">查找到图书...\n");
show_book(&(result->bi)); /*调用show_book显示查到的图书信息*/
}
else
{
printf(">没有找到ISBN为 %s 的图书。\n", isbn);
}
printf(">继续查找吗?(y or n):");
getchar();
input = getchar();
}
}
void delete_book()
{
char input ='Y';
char isbn[MAX_ISBN] ={0};
book* p =NULL;
book *result;
while(input == 'Y' || input == 'y')
{
printf(">删除图书...\n");
printf(">请输入ISBN(最大长度为 %d):",MAX_ISBN);
scanf("%s",isbn);
p = first_book;
result = NULL;
while (p !=NULL)
{
if(strcmp(p->bi.ISBN,isbn) == 0)
{
result = p;
break;
}
p=p->next;
}
if (result !=NULL)
{
show_book(&(result->bi));
printf(">确认删除吗?(y or n)");
getchar();
input = getchar();
if( input == 'y' || input == 'Y')
{
get_previous_book(p)->next = p->next;
free(p);
}
}
else
{
printf(">没有找到ISBN %s 的图书。\n",isbn);
}
printf(">继续删除其他图书吗?(y or n)");
getchar();
input = getchar();
}
}
void save_books()
{
save_books_to_file();
printf(">保存成功!按任意键返回...");
getchar();
getchar();
}
void save_books_to_file()
{
FILE* fp = fopen(BOOK_FILE, "wb");
book* p = first_book;
while(p !=NULL)
{
fwrite(&(p->bi),sizeof(book_info),1,fp);
fseek(fp, 0, SEEK_END);
p = p->next;
}
fclose(fp);
}
void init_book()
{
FILE* fp = NULL;
fp = fopen(BOOK_FILE, "r");
if (fp == NULL) /*如果文件不存在*/
{
fp = fopen(BOOK_FILE,"w");/*创建文件*/
if (fp == NULL)
{
printf("不能创建文件,按任意键退出...");
getchar();
exit(0);
}
}
fclose(fp);
}
void load_books()
{
book* b = NULL;
book* last = NULL;
FILE* fp = NULL;
int count = 0;
b = (book*)malloc(sizeof(book));
memset(b, 0, sizeof(book));/*初始化b*/
b->next = NULL;
fp = fopen(BOOK_FILE, "rb");/*打开图文件*/
/*从文件中逐个读出图书信息*/
while(fread(&(b->bi),sizeof(book_info), 1,fp) == 1)
{
if (first_book == NULL)/*如果读取的是一个节点,即第一本书*/
{
first_book = b;/*将头节点指向b*/
}
else
{
last = get_last_book();/*否则找到链表中的最后一个图书节点,即最后一本书*/
last->next = b;/*将b连接到最后一个节点后*/
}
count++;
fseek(fp, count * sizeof(book_info),SEEK_SET);/*将文件指针指到下一本书*/
b = (book*)malloc(sizeof(book));
memset(b, 0, sizeof(book));
b->next = NULL;
}
free(b);
b = NULL;
fclose(fp);
}
void clear_books()
{
book* p = NULL;
while (first_book != NULL)/*链表不为空*/
{
if (first_book->next!=NULL)/*如果链表中有2本以上的书*/
{
p = first_book;
first_book = first_book->next;/*头节点向后移动一位*/
free(p);/*释放原来的头节点*/
p = NULL;
}
else/*清除链表中最后一本书*/
{
free(first_book);
first_book = NULL;
}
}
}
/*在source中逐个字符进行查找,看str是否存在,存在返回存在的位置,否则返回-1*/
int findstr(char* source, char* str)
{
int pos = -1;
int i = 0;
int j = 0;
int m = strlen(source);
int n = strlen(str);
/*str长度为0,或source长度为0,或者str长度比sopurce长的情况*/
if (m ==0 || n==0 ||m<n)
{
return pos;
}
/*str和source长度相等的情况*/
if (m == n)
{
if (strcmp(source, str) == 0)
{
return 0;
}
else{return -1;}
}
for (i = 0 ; i < (m - n); i++)
{
pos = i;
for (j = 0; j < n; j++)
{
if (source[i+j] != str[j])
{
pos = -1;
break;
}
if (pos != -1)
{
break;
}
}
}
return pos;
}
book* get_last_book()
{
book* p = first_book;
if (p == NULL)
{
return p;
}
while((p !=NULL) && (p->next != NULL))
{
p = p->next;
}
return p;
}
void input_book(book_info*info)
{
printf(">请输入书名 (最大长度为 %d):", MAX_BOOK_NAME);
scanf("%s", info->book_name);
printf(">请输入作者 (最大长度为 %d):", MAX_AUTHOR);
scanf("%s", info->author);
printf(">请输入出版社 (最大长度为 %d):", MAX_PUBLISHER);
scanf("%s", info->publisher);
printf(">请输入出版日期 (最大长度为 %d):", MAX_DATE);
scanf("%s", info->pub_date);
printf(">请输入 ISBN (最大长度为 %d):", MAX_ISBN);
scanf("%s", info->ISBN);
printf(">请输入页数:");
scanf("%s", &(info->pages));
}
void show_book(book_info* info)
{
printf("------------------------------\n");
printf("书名:%s\n", info->book_name);
printf("作者:%s\n", info->author);
printf("出版社:%s\n", info->publisher);
printf("出版日期:%s\n", info->pub_date);
printf("..............................\n");
printf("ISBN:%s\n", info->ISBN);
printf("页数:%s\n", info->pages);
printf("\n");
}
book* get_previous_book(book* p)
{
book* previous = first_book;
while(previous != NULL)
{
if (previous->next == p)
{
break;
}
previous = previous->next;
}
return previous;
}
void init_user()
{
FILE* fp = NULL;
user_info default_admin;
strcpy(default_admin.username,"admin");
strcpy(default_admin.password,"123");
default_admin.user_type = ADMIN;
fp = fopen(USER_FILE,"r");
if (fp == NULL)
{
fp = fopen(USER_FILE,"wb");
fwrite(&default_admin,sizeof(user_info),1,fp);
}
fclose(fp);
}
void load_users()
{
user* u = NULL;
user* last = NULL;
FILE* fp = NULL;
int count = 0;
u = (user*)malloc(sizeof(user));
memset(u, 0, sizeof(user));
u->next = NULL;
fp = fopen(USER_FILE,"rb");
fseek(fp, 0, SEEK_SET);
while(fread(&(u->ui), sizeof(user_info), 1, fp) == 1)
{
if (first_user == NULL)
{
first_user = u;
}
else
{
last = get_last_user();
last->next = u;
}
count++;
fseek(fp, count * sizeof(user_info), SEEK_SET);
u = (user*)malloc(sizeof(user));
memset(u, 0, sizeof(user));
u->next = NULL;
}
free(u);
u = NULL;
fclose(fp);
}
USER_TYPE login()
{
char username[MAX_USERNAME] = {0};
char password[MAX_PASSWORD] = {0};
char try_again = 'Y';
user* p = NULL;
while(try_again == 'y' || try_again == 'Y')
{
printf("请输入用户名:");
scanf("%s", username);
printf("请输入密码:");
scanf("%s", password);
p = find_user(username);
if (p == NULL)
{
printf("用户名输入错误,请重试!");
}
else if (strcmp(p->ui.password, password) != 0)
{
printf("密码输入错误,请重试!");
}
else
{
return p->ui.user_type;
}
printf(">重新输入吗??(y or n):");
getchar();
try_again = getchar();
}
exit(0);
}
void add_user(){}
void search_user(){}
void save_users(){}
void save_users_to_file(){}
void clear_users()
{
user* p = NULL;
while (first_user != NULL)
{
if (first_user->next != NULL)
{
p = first_user;
first_user = first_user->next;
free(p);
p = NULL;
}
else
{
free(first_user);
first_user = NULL;
}
}
}
user* get_last_user()
{
user* p = first_user;
while((p != NULL) && (p->next != NULL))
{
p = p->next;
}
return p;
}
user* get_previous_user(user* p)
{
user* previous = first_user;
while(previous != NULL)
{
if (previous->next == p)
{
break;
}
previous =previous->next;
}
return previous;
}
void show_user(user_info* info)
{
printf("------------------------------\n");
printf("用户名:%s\n", info->username);
printf("密码:%s\n", info->password);
printf("用户类型:%s\n", info->user_type == ADMIN ? "admin" : "user");
printf("\n");
}
void input_user(user_info* info)
{
printf(">请输入用户名(最大长度为 %d):", MAX_USERNAME);
scanf("%S", info->username);
printf(">请输人密码(最大长度为 %d):", MAX_PASSWORD);
scanf("%s", info->password);
printf(">请输人用户类型 (%d 是管理员, %d 是普通用户)", ADMIN, USER);
scanf("%d", &(info->user_type));
}
user* find_user(char* name)
{
user* p = first_user;
int is_found = 0;
while (p != NULL)
{
if (strcmp(p->ui.username, name) == 0)
{
is_found = 1;
break;
}
p = p->next;
}
if (is_found)
{
return p;
}
return NULL;
}
哦。。。有一个uaer.dat和books.dat的。。。文件哦。。。
差点忘记了- -||