C语言实现图书管理系统课程设计
本文实例为大家分享了C语言实现图书管理系统的具体代码,供大家参考,具体内容如下
设计要求
设计图书管理系统
要求如下:
1.对书名,作者,编号,出版单位,出版日期,类别,存放位置进行添加、删除、修改
2.查询相关信息
3.保存所有信息
实现代码
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <coNIO.h>
typedef struct book
{
char bookname[100] ;
char writer[100];
char classnumber[100];
char publisher[100];
char time[100];
char type[100];
char place[100];
struct book *next;
}Book;
int mima()
{
int i=0;
char user[10]="aa";
char Code[10]="123456";
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 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");
}
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 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");
}
int savebookdata(Book *head)
{
Book *p;
FILE*fp;
fp=fopen("D:\\java\\zuoye.txt","w");
p=head->next;
do
{
fprintf(fp,"%s\t",p->bookname);
fprintf(fp,"%s\t",p->writer);
fprintf(fp,"%s\t",p->classnumber);
fprintf(fp,"%s\t",p->publisher);
fprintf(fp,"%s\t",p->time);
fprintf(fp,"%s\t",p->type);
fprintf(fp,"%s\t\n",p->place);
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->type);
printf("\t--------------------------------------------------------\n");
printf("\t\t\t请输入存放位置:");
scanf("%s",s->place);
printf("\t--------------------------------------------------------\n");
printf("\n");
printf("\t--------------------------------------------------------\n");
i--;
p->next=s;
p=p->next;
p->next=NULL;
}
printf("\t\t\t图书添加成功!!!\n");
system("pause");
}
int readbookdata(Book **head)
{
int n;
Book *s, *p;
FILE *fp;
fp=fopen("d:\\java\\zuoye.txt","r");
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",p->type);
fscanf(fp,"%s\n",p->place);
p->next=s;
p=p->next;
p->next=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类别\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\t%s\n",p->bookname,p->writer,p->classnumber,p->publisher,p->time,p->type,p->place);
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类别\t存放位置\n");
printf("--------------------------------------------------------------------\n");
printf("%s\t%s\t%s\t%s\t%s\t%s\t%s\n\n",p->bookname,p->writer,p->classnumber,p->publisher,p->time,p->type,p->place);
}
}
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\t【7】存放位置\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->type);
break;
case 7:
printf("\t请输入存放位置:");
scanf("%s",p->place);
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\t3.按编号查找\n");
printf("----------------------------------------------------------------------\n");
printf("\t\t4.按出版单位查找\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存放位置\n");
printf("----------------------------------------------------------------------\n");
printf("%s\t%s\t%s\t%s\t%s\t%s\t%s\n",p->bookname,p->writer,p->classnumber,p->publisher,p->time,p->type,p->place);
}
}
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存放位置\n");
printf("----------------------------------------------------------------------\n");
printf("%s\t%s\t%s\t%s\t%s\t%s\t%s\n",p->bookname,p->writer,p->classnumber,p->publisher,p->time,p->type,p->place);
}
}
if(flag==0)
printf("不存在该信息\n");
}
else if(choose==3)
{
printf("输入所查编号:");
scanf("%s",b);
printf("----------------------------------------------------------------------\n\n");
while(p->next)
{
p=p->next;
if(strcmp(b,p->classnumber)==0)
{
flag=1;
printf("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※\n");
printf("书名\t作者名\t编号\t出版单位\t出版时间\t类别\t存放位置\n");
printf("----------------------------------------------------------------------\n");
printf("%s\t%s\t%s\t%s\t%s\t%s\t%s\n",p->bookname,p->writer,p->classnumber,p->publisher,p->time,p->type,p->place);
}
}
if(flag==0)
printf("不存在该信息\n");
}
else if(choose==4)
{
printf("输入所查出版单位:");
scanf("%s",b);
printf("----------------------------------------------------------------------\n\n");
while(p->next)
{
p=p->next;
if(strcmp(b,p->publisher)==0)
{
flag=1;
printf("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※\n");
printf("书名\t作者名\t编号\t出版单位\t出版时间\t类别\t存放位置\n");
printf("----------------------------------------------------------------------\n");
printf("%s\t%s\t%s\t%s\t%s%s\t%s\n",p->bookname,p->writer,p->classnumber,p->publisher,p->time,p->type,p->place);
}
}
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出版单位\t出版时间\t类别\t存放位置\n");
printf("----------------------------------------------------------------------------------\n");
do
{
printf("%s\t%s\t%s\t%s\t%s\t%s\t%s\n\n",p->bookname,p->writer,p->classnumber,p->publisher,p->time,p->type,p->place);
p=p->next;
}while(p!=NULL);
system("pause");
}
int main(void)
{
system("cls");
Book *head=NULL;
readbookdata(&head);
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;
}
运行结果
这里提供的是管理员身份
这里提供游客身份(供选择的功能相对管理员少)
后面大家就可以自行输入对应功能前的序号,实现此系统的功能
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
相关文章