图书信息管理系统(C语言)
图书信息包括:登录号、书名、作者名、分类号、出版单位、出版时间、价格等。试设计一图书信息管理系统,使之能提供以下功能:
系统以菜单方式工作
1) 图书信息录入功能(图书信息用文件保存)--输入
2) 图书信息浏览功能--输出
3) 查询和排序功能:(至少一种查询方式)--算法
4) 按书名查询
5) 按作者名查询
6) 图书信息的删除与修改(可选项)
#include
#include
#include
#include
typedef struct book /*定义结构体*/
{
char bookname[100] ; /*书名*/
char writer[100]; /*作者*/
char classnumber[100]; /*出版号*/
char publisher[100]; /*出版单位*/
char time[100]; /*出版日期*/
char price[100]; /*价格*/
struct book *next; /*下个图书的信息*/
}Book;
int mima()
{
int i=0;
char user[10]="hut";
char Code[10]="416426";
char getuser[10];
char Getskey[10];
while(1)
{
printf("\t\t\t请输入账号:");
scanf("%s",getuser);
printf("\t\t\t请输入密码:");
while(1)
{
Getskey[i]=getch();
if(Getskey[i]=='\r')
break;
else if(Getskey[i]=='\b')
{
if(i==0)
continue;
printf("\b");
printf(" ");
printf("\b");
i--;
}
else
{
printf("*");
i++;
}
}
Getskey[6]='\0';
if(strcmp(user, getuser)==0&&strcmp(Code, Getskey)==0)
return 1;
else
{
printf("\n\n\t\t\t账号不存在或密码输入错误,请重新输入\n");
i = 0;
}
}
}
void showmenu1() /*显示管理员菜单*/
{
system("cls");
printf("\n\n\n\n\n");
printf("\t************************************************************\n");
printf("\t* *\n");
printf("\t* 欢迎来到图书信息管理系统 *\n");
printf("\t* *\n");
printf("\t* *\n");
printf("\t* 主菜单 *\n");
printf("\t* *\n");
printf("\t* 1.录入图书信息 *\n");
printf("\t* *\n");
printf("\t* 2.删除图书信息 *\n");
printf("\t* *\n");
printf("\t* 3.修改图书信息 *\n");
printf("\t* *\n");
printf("\t* 4.查询图书信息 *\n");
printf("\t* *\n");
printf("\t* 5.显示所有图书信息 *\n");
printf("\t* *\n");
printf("\t* 6.返回登录界面 *\n");
printf("\t* *\n");
printf("\t* 7.保存数据并退出系统 *\n");
printf("\t* *\n");
printf("\t************************************************************\n\n");
}
void showmenu2() /*显示游客菜单*/
{
system("cls");
printf("\n\n\n\n\n");
printf("\t************************************************************\n");
printf("\t* *\n");
printf("\t* 欢迎来到图书信息管理系统 *\n");
printf("\t* *\n");
printf("\t* *\n");
printf("\t* 主菜单 *\n");
printf("\t* *\n");
printf("\t* 1.查询图书信息 *\n");
printf("\t* *\n");
printf("\t* 2.显示所有图书信息 *\n");
printf("\t* *\n");
printf("\t* 3.返回登录界面 *\n");
printf("\t* *\n");
printf("\t* 4.退出系统 *\n");
printf("\t* *\n");
printf("\t************************************************************\n\n");
}
int savebookdata(Book *head) /*将数据保存到文件中*/
{
Book *p;
FILE*fp;
fp=fopen("D:\\zuoye.txt","w");
p=head->next;
do
{
fprintf(fp,"%s\t",p->bookname); /* 把这个结点的数据全部写入至文件内由于next指针不需要写入至文件,*/
fprintf(fp,"%s\t",p->writer); /*因此,数据大小就是sizeof(book)-sizeof(book*)总数据大小减去指针的大小 */
fprintf(fp,"%s\t",p->classnumber);
fprintf(fp,"%s\t",p->publisher);
fprintf(fp,"%s\t",p->time);
fprintf(fp,"%s",p->price);
fprintf(fp,"\n");
p=p->next; /*转换到下一个结点*/
}while(p!=NULL);
fclose(fp);
return 0;
}
void addnewbook(Book **head) /*录入图书信息*/
{
int i;
Book *p,*s;
system("cls");
if(*head == NULL) { /* 如果表头为空,先为它分配内存 */
*head = (book *)calloc(1, sizeof(book));
p = *head;
} else { /* 否则,就说明有内容,直接切换到尾结点 */
p = *head;
while(p->next)
p = p->next;
}
printf("\n\n\n");
printf("\t※※※※※※※※※※※※※※※※※※※※※※※※※※※※\n");
printf("\t\t\t请问添加几本书:");
scanf("%d",&i);
printf("\t--------------------------------------------------------\n");
while(i)
{
s = (Book *)calloc(1, sizeof(Book)); /*为这个结点分配内存 */
printf("\t\t\t请输入书名:");
scanf("%s",s->bookname);
printf("\t--------------------------------------------------------\n");
printf("\t\t\t请输入作者:");
scanf("%s",s->writer);
printf("\t--------------------------------------------------------\n");
printf("\t\t\t请输入出版号:");
scanf("%s",s->classnumber);
printf("\t--------------------------------------------------------\n");
printf("\t\t\t请输入出版单位:");
scanf("%s",s->publisher);
printf("\t--------------------------------------------------------\n");
printf("\t\t\t请输入出版日期:");
scanf("%s",s->time);
printf("\t--------------------------------------------------------\n");
printf("\t\t\t请输入价格:");
scanf("%s",s->price);
printf("\t--------------------------------------------------------\n");
printf("\n");
printf("\t--------------------------------------------------------\n");
i--;
p->next=s; /*保存输进去的这个结点*/
p=p->next; /*切换到下一个结点*/
p->next=NULL; /*最后一个结点为NULL*/
}
printf("\t\t\t图书添加成功!!!\n");
system("pause");
}
int readbookdata(Book **head) /*将数据从文件中读取*/
{
int n;
Book *s, *p;
FILE *fp;
fp=fopen("d:\\zuoye.txt","r");
/* 如果文件打开失败,就返回-1 */
if(NULL == fp) {
*head = NULL;
return -1;
}
*head = (Book *)calloc(1, sizeof(Book));
p = *head;
while (!feof(fp))
{
s = (Book *)calloc(1, sizeof(Book)); /* 为下个结点分配内存空间 */
fscanf(fp,"%s",s->bookname);
fscanf(fp,"%s",s->writer);
fscanf(fp,"%s",s->classnumber);
fscanf(fp,"%s",s->publisher);
fscanf(fp,"%s",s->time);
fscanf(fp,"%s",s->price);
p->next=s; /*保存该结点*/
p=p->next; /*切换到下一个结点*/
p->next=NULL; /*保证最后一个结点为NULL*/
}
fclose(fp);/* 关闭文件 */
return 0;
}
void deletebook(Book *head) /*删除图书信息*/
{
system("cls");
int flag=0;
char name[100],ch[10];
Book *p,*t;
/* 如果表头为空 */
if(head == NULL || head->next == NULL) {
printf("没有记录图书信息!\n");
system("pause");
return;
}
p=head;
printf("\n\n\n");
printf("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※\n");
printf("请输入要删除的书名;");
scanf("%s",&name);
printf("----------------------------------------------------------------------\n");
printf("书名\t\t作者名\t\t分类号\t出版单位\t出版时间\t价格\n");
printf("----------------------------------------------------------------------\n");
while(p->next)
{
t=p;
p=p->next; /*转换到下一个结点*/
if(strcmp(name,p->bookname)==0)
{
flag=1;
printf("%s\t%s\t%s\t%s\t%s\t%s\n",p->bookname,p->writer,p->classnumber,p->publisher,p->time,p->price);
break;
}
}
if(flag==1)
{
printf("是否删除该本图书信息(y/n)");
scanf("%s",ch);
printf("----------------------------------------------------------------------\n");
if(strcmp(ch,"Y")==0||strcmp(ch,"y")==0)
{
t->next=p->next;
printf("该书已删除!\n");
}
else
return;
}
else if(flag==0)
printf("没找到那本书!\n");
system("pause");
}
void modifybook(Book *head) /*修改图书信息*/
{
system("cls");
int flag=0;
char name[100],ch[10];
Book *p,*t;
/* 如果表头为空 */
if(head == NULL || head->next == NULL) {
printf("没有记录图书信息!\n");
system("pause");
return ;
}
p=head;
printf("\n\n\n");
printf("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※\n");
printf("请输入要修改的书名:");
scanf("%s",name);
printf("--------------------------------------------------------------------\n");
while(p->next)
{
p=p->next; /*切换到下一个结点*/
if(strcmp(name,p->bookname)==0)
{
flag=1;
t=p;
printf("书名\t\t作者名\t\t分类号\t出版单位\t出版时间\t价格\n");
printf("--------------------------------------------------------------------\n");
printf("%s\t%s\t%s\t%s\t%s\t%s\n",p->bookname,p->writer,p->classnumber,p->publisher,p->time,p->price);
}
}
if(flag==1)
{
printf("--------------------------------------------------------------------\n");
printf("\t\t是否修改该本图书信息(y/n):");
scanf("%s",ch);
printf("\t--------------------------------------------------------------------\n");
if(strcmp(ch,"Y")==0||strcmp(ch,"y")==0)
{
int c;
printf("\t\t【1】书名\n");
printf("--------------------------------------------------------------------\n");
printf("\t\t【2】作者\n");
printf("--------------------------------------------------------------------\n");
printf("\t\t【3】出版号\n");
printf("--------------------------------------------------------------------\n");
printf("\t\t【4】出版单位\n");
printf("--------------------------------------------------------------------\n");
printf("\t\t【5】出版日期\n");
printf("--------------------------------------------------------------------\n");
printf("\t\t【6】价格\n");
printf("--------------------------------------------------------------------\n");
printf("\t请输入你要修改的内容;");
scanf("%d",&c);
printf("--------------------------------------------------------------------\n");
p=t;
switch(c)
{
case 1:
printf("\t请输入书名:");
scanf("%s",p->bookname);
break;
case 2:
printf("\t请输入作者:");
scanf("%s",p->writer);
break;
case 3:
printf("\t请输入出版号:");
scanf("%s",p->classnumber);
break;
case 4:
printf("\t请输入出版单位:");
scanf("%s",p->publisher);
break;
case 5:
printf("\t请输入出版日期:");
scanf("%s",p->time);
break;
case 6:
printf("\t请输入价格:");
scanf("%s",p->price);
break;
}
printf("--------------------------------------------------------------------\n");
printf("\t恭喜大佬修改成功!!!\n");
}
}
else if(flag==0)
printf("对不起,该书库没有该书\n");
system("pause");
}
void searchbook(Book *head) /*查询图书信息*/
{
system("cls");
int choose,flag=0;
Book *p;
char b[100];
/* 如果表头为空 */
if(head == NULL || head->next == NULL) {
printf("没有记录图书信息!\n");
system("pause");
return ;
}
do
{
system("cls");
printf("\n\n\n");
printf("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※\n");
printf("\t\t1.按书名查找\n");
printf("----------------------------------------------------------------------\n");
printf("\t\t2.按作者名查找\n");
printf("----------------------------------------------------------------------\n");
printf("\t\t返回主菜单(其他数字)\n");
printf("----------------------------------------------------------------------\n");
printf("\t\t请输入您的选择:");
scanf("%d",&choose);
printf("----------------------------------------------------------------------\n");
p=head;
if(choose==1)
{
printf("输入所查书名:");
scanf("%s",b);
printf("----------------------------------------------------------------------\n\n");
while(p->next)
{
p=p->next; /*转换到下一个结点*/
if(strcmp(b,p->bookname)==0) /*判断是否找到并输出*/
{
flag=1;
printf("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※\n");
printf("书名\t\t作者名\t\t分类号\t出版单位\t出版时间\t价格\n");
printf("----------------------------------------------------------------------\n");
printf("%s\t%s\t%s\t%s\t%s\t%s\n",p->bookname,p->writer,p->classnumber,p->publisher,p->time,p->price);;
}
}
if(flag==0)
printf("不存在该信息\n");
}
else if(choose==2)
{
printf("输入所查作者名:");
scanf("%s",b);
printf("----------------------------------------------------------------------\n\n");
while(p->next)
{
p=p->next; /*转换到下一个结点*/
if(strcmp(b,p->writer)==0) /*判断是否找到并输出*/
{
flag=1;
printf("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※\n");
printf("书名\t\t作者名\t\t分类号\t出版单位\t出版时间\t价格\n");
printf("----------------------------------------------------------------------\n");
printf("%s\t%s\t%s\t%s\t%s\t%s\n",p->bookname,p->writer,p->classnumber,p->publisher,p->time,p->price);
}
}
if(flag==0)
printf("不存在该信息\n");
}
else
return;
system("pause");
}while(1);
}
int printfbook(Book *head) /*显示所有图书信息*/
{
Book *p;
system("cls");
/* 如果表头为空 */
if(head == NULL || head->next == NULL) {
printf("没有记录图书信息!\n");
system("pause");
return 0;
}
p=head->next;
printf("\n\n\n");
printf("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※\n");
printf(" 图书列表\n");
printf("----------------------------------------------------------------------\n");
printf("书名\t\t作者\t\tISBN\t出版单位\t出版时间\t价格\n");
printf("----------------------------------------------------------------------\n");
do
{
printf("%s\t%s\t%s\t%s\t%s\t%s\n",p->bookname,p->writer,p->classnumber,p->publisher,p->time,p->price);
p=p->next; /*转换到下一个结点*/
}while(p!=NULL);
system("pause");
}
int main(void)
{
system("cls");
Book *head=NULL;
readbookdata(&head);
system("color 8a");
int choice,z,c;
printf("\n\n\n");
printf("\t※※※※※※※※※※※※※※※※※※※※※※※※※※※※\n");
printf("\t\t\t欢迎光临图书信息管理系统\n");
printf("\t--------------------------------------------------------\n");
printf("\t\t\t【1】游客\n");
printf("\t\t\t【2】管理员\n");
printf("\t\t\t【3】退出系统\n");
printf("\t--------------------------------------------------------\n");
while(printf("\t\t\t请选择身份:")&&scanf("%d",&c)!=EOF)
{
if(c==1)
{
showmenu2();
while (printf("\t请输入选择项(1-3):")&&scanf("%d",&choice)!=EOF)
{
switch (choice)
{
case 1: /*查询图书信息*/
searchbook(head);
break;
case 2: /*显示所有图书信息*/
printfbook(head);
break;
case 3: /*返回登录界面*/
main();
break;
case 4:
exit(0);
default:
printf("\t输入错误!!!\n");
system("pause");
break;
}
showmenu2();
}
}
else if(c==2)
{
z=mima();
showmenu1();
while (printf("\t请输入选择项(1-6):")&&z==1&&scanf("%d",&choice)!=EOF)
{
switch (choice)
{
case 1: /*录入图书信息*/
addnewbook(&head);
break;
case 2: /*删除图书信息*/
deletebook(head);
break;
case 3: /*修改图书信息*/
modifybook(head);
break;
case 4: /*查询图书信息*/
searchbook(head);
break;
case 5: /*显示所有图书信息*/
printfbook(head);
break;
case 6: /*返回登录界面*/
main();
break;
case 7: /*保存数据并退出系统*/
savebookdata(head);
exit(0);
default:
printf("\t输入错误!!!\n");
system("pause");
break;
}
showmenu1();
}
}
else if(c==3)
exit(0);
else
{
printf("\t\t\t输入错误!!!\n\n");
}
}
return 0;
}