图书管理系统源代码.docx
《图书管理系统源代码.docx》由会员分享,可在线阅读,更多相关《图书管理系统源代码.docx(37页珍藏版)》请在冰豆网上搜索。
图书管理系统源代码
#include"stdio.h"
#include"stdlib.h"
#include"string.h"
#defineMENU_ADMIN_COUNT2
#defineMENU_SEARCH_BOOK_COUNT6
#defineMENU_USER_COUNT7
#defineBOOK_FILE"books.dat"
#defineUSER_FILE"user.dat"
#defineMAX_BOOK_NAME20
#defineMAX_PUBLISHER20
#defineMAX_DATE10
#defineMAX_AUTHOR20
#defineMAX_ISBN20
typedefstruct_book_info
{charbook_name[MAX_BOOK_NAME];
charauthor[MAX_AUTHOR];
charpublisher[MAX_PUBLISHER];
charpub_date[MAX_DATE];
charISBN[MAX_ISBN];
intpages;
}book_info;
typedefstruct_book
{book_infobi;
struct_book*next;
}book;
#defineMAX_USERNAME10
#defineMAX_PASSWORD10
typedefenum_USER_TYPE{
ADMIN=0,
USER
}USER_TYPE;
typedefstruct_user_info
{charusername[MAX_USERNAME];
charpassword[MAX_PASSWORD];
USER_TYPEuser_type;
}user_info;
typedefstruct_user
{user_infoui;
struct_user*next;
}user;
voidinit_user();
voidload_users();
USER_TYPElogin();
voidadd_user();
voidsearch_user();
voidsave_users();
voidclear_users();
voidsave_users_to_file();
user*get_last_user();
user*get_previous_user(user*p);
user*find_user(char*name);
voidshow_user(user_info*info);
voidinput_user(user_info*info);
voiddelete_user(user*p);
voidupdate_user(user*p);
voidinit_book();
voidload_books();
voidadd_book();
voidview_book();
voiddelete_book();
voidsave_books();
voidclear_books();
voidsearch_book_by_name();
voidsearch_book_by_author();
voidsearch_book_by_publisher();
voidsearch_book_by_pubdate();
voidsearch_book_by_isbn();
intfindstr(char*source,char*str);
voidsave_books_to_file();
book*get_last_book();
book*get_previous_book(book*p);
voidinput_book(book_info*info);
voidshow_book(book_info*info);
voidshow_admin_menu();
voidshow_search_book_menu();
voidshow_user_menu();
voidadmin_exit();
voiduser_exit();
book*first_book=NULL;
user*first_user=NULL;
charmenu_title[]=
"=========================================\n"
"|图书管理系统|\n"
"+---------------------------------------+\n";
charmenu_admin[]=
"||\n"
"|
(1)图书管理|\n"
"|
(2)用户管理|\n"
"|(3)退出系统|\n"
"+---------------------------------------+\n";
charadmin_menu1[]=
"-----------------------------------------\n"
"|图书管理|\n"
"----------------------------------------|\n"
"|<1>新增图书 |\n"
"|<2>浏览图书|\n"
"|<3>查找图书|\n"
"|<4>删除图书|\n"
"|<5>保存图书 |\n"
"|<0>返回主菜单|\n"
"-----------------------------------------\n";
charadmin_menu2[]=
"----------------------------------------\n"
"用户管理|\n"
"----------------------------------------|\n"
"|<1>新增用户|\n"
"|<2>查找用户|\n"
"|<3>保存用户|\n"
"|<0>返回主菜单|\n"
"|---------------------------------------|\n";
void(*menu1_fun[])()=
{
add_book,
view_book,
show_search_book_menu,
delete_book,
save_books,
};
void(*menu2_func[])()=
{
add_user,
search_user,
save_users,
};
charmenu_admin_search_book[]=
"|查找图书:
|\n"
"|<1>按书名查找|\n"
"|<2>按作者查找|\n"
"|<3>按出版社查找|\n"
"|<4>按出版日期查找|\n"
"|<5>按国际标准书号(ISBN)查找|\n"
"|<0>返回主菜单|\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,
};
charmenu_user[]=
"|<1>浏览图书|\n"
"|<2>按书名查找图书|\n"
"|<3>按作者查找图书|\n"
"|<4>按出版社查找图书|\n"
"|<5>按出版日期查找图书|\n"
"|<6>按国际标准书号(ISBN)查找图书|\n"
"|<0>退出系统|\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
};
voidadd_book()
{chartry_again='Y';
book*p=NULL;
book*new_book=NULL;
while(try_again=='Y'||try_again=='y')
{new_book=(book*)malloc(sizeof(book));
memset(new_book,0,sizeof(book));
new_book->next=NULL;
printf(">新增图书...\n");
input_book(&(new_book->bi));
p=get_last_book();
if(p==NULL)
{first_book=new_book;
}
else
{p->next=new_book;
}
printf(">继续添加图书吗?
(yorn):
");
getchar();
try_again=getchar();
}
}
voidview_book()
{book*p=NULL;
charinput='Y';
intcount=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(">显示下一页吗?
(yorn):
");
getchar();
input=getchar();
if(input!
='y'&&input!
='Y')
{break;
}
}
p=p->next;
}
printf(">再次浏览图书吗?
(yorn):
");
getchar();
input=getchar();
}
}
voidsearch_book_by_name()
{book*p=NULL;
chars[MAX_BOOK_NAME]={0};
charinput='Y';
intcount=0;
inti=0;
printf(">查找图书...\n");
while(input=='Y'||input=='y')
{count=0;
p=first_book;
memset(s,0,MAX_BOOK_NAME);
printf(">请输入书名(最大长度为%d):
",MAX_BOOK_NAME);
scanf("%s",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。
继续查找吗?
(yorn):
",s);
getchar();
input=getchar();
continue;
}
printf(">共找到%d本图书...\n",count);
printf(">继续查找吗?
(yorn):
");
getchar();
input=getchar();
}
}
voidsearch_book_by_author()
{book*p=NULL;
chars[MAX_AUTHOR]={0};
charinput='Y';
intcount=0;
inti=0;
printf(">查找图书...\n");
while(input=='Y'||input=='y')
{count=0;
p=first_book;
memset(s,0,MAX_AUTHOR);
printf(">请输入作者(最大长度为%d):
",MAX_AUTHOR);
scanf("%s",s);
while(p!
=NULL)
{if(findstr(p->bi.author,s)!
=-1)
{show_book(&(p->bi));
count++;
}
p=p->next;
}
if(count==0)
{printf(">没有找到作者为%s的图书。
继续查找吗?
(yorn):
",s);
getchar();
input=getchar();
continue;
}
printf(">共找到%d本图书...\n",count);
printf(">继续查找吗?
(yorn):
");
getchar();
input=getchar();
}
}
voidsearch_book_by_publisher()
{book*p=NULL;
chars[MAX_PUBLISHER]={0};
charinput='Y';
intcount=0;
printf(">查找图书...\n");
while(input=='Y'||input=='y')
{count=0;
p=first_book;
memset(s,0,MAX_AUTHOR);
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的图书。
继续查找吗?
(yorn):
",s);
getchar();
input=getchar();
continue;
}
printf(">共找到%d本图书...\n",count);
printf(">继续查找吗?
(yorn):
");
getchar();
input=getchar();
}
}
voidsearch_book_by_pubdate()
{book*p=NULL;
chars[MAX_DATE]={0};
charinput='Y';
intcount=0;
inti=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的图书。
继续查找吗?
(yorn):
",s);
getchar();
input=getchar();
continue;
}
printf(">共找到%d本图书...\n",count);
printf(">继续查找吗?
(yorn):
");
getchar();
input=getchar();
}}
voidsearch_book_by_isbn()
{charinput='Y';
charisbn[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;
result=NULL;
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));
}
else
{printf(">没有找到ISBN为%s的图书。
\n",isbn);
}
printf(">继续查找吗?
(yorn)");
getchar();
input=getchar();
}
}
voiddelete_book()
{charinput='Y';
charisbn[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;
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(">确认删除吗?
(yorn)");
getchar();
input=getchar();
if(input=='y'||input=='Y')
{if(p==NULL)
{
get_previous_book(p)->next=p->next;
free(p);
}}
else
{
get_previous_book(p)->next=p->next;
}
}
else
{printf(">没有找到ISBN为%s的图书。
\n",isbn);
}
printf(">继续删除其他图书吗?
(yorn)");
getchar();
input=getchar();
}
}
voidsave_books()
{save_books_to_file();
printf(">保存成功!
按任意键返回...");
getchar();
getchar();
}
voidsave_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);
}
voidinit_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);
}
voidload_books()
{book*b=NULL;
book*last=NULL;
FILE*fp=NULL;
intcount=0;
b=(book*)malloc(sizeof(book));
memset(b,0,sizeof(book));
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;
}
else
{last=get_last_book();
last->next=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);
}
voidclear_books()
{book*p=NULL;
while(first_book!
=NULL)
{if(first_book->next!
=NULL)
{p=first_book;
first_book=first_book->next;
free(p);
p=NULL;
}
else
{free(first_book);
first_book=NULL;
}
}
}
intfindstr(char*source,char*str)
{intpos=-1;
inti=0;
intj=0;
intm=strlen(source);
intn=strlen(str);
if(m==0||n==0||m{returnpos;
}
if(m==n)
{if(strcmp