图书馆管理系统

/*模拟图书馆管理系统。每名读者只能借一本书,读者可借书或还书。管理员可对图书和读者信息进行录入、修改和删除。
图书信息至少应包括:编号、书名、数量。
读者信息至少应包括:编号、姓名、所借图书。
可根据图书名称或编号进行图书信息查询,可查询某本书现在被哪些读者借走。*/

 

/*创建两个txt文件,1、reader.txt   2、book.txt

1 凌霄 0
2 杨逗比 0
3 脚大神 0
4 胡笳 0

1 平凡的世界 10
2 瓦尔登湖 10
3 曼德拉传 10

*/


#include<stdio.h>
#include<string.h>

#define MAX 100

struct reader
{
int number;
char name[MAX];
int bookborrowed;
}
READER;
struct book
{
int num;
char name[MAX];
int amount;
}
BOOK;

int contents(void);//查询书目
int reader_search(void);//读者的查询
int admin_search(void);//管理员的查询
int enter_reader(void);//录入读者信息
int enter_book(void);//录入书籍信息
int conrrect_reader(void);//修改读者信息
int conrrect_book(void);//修改书籍信息
int delet_reader(void);//删除读者信息
int delet_book(void);//删除书籍信息
int swit(void);//管理员的选择
int borrow(int readernum,int booknum);//借书的函数
int bring(void);//还书的函数

int main()
{
char readerenter;
char pass[8],c;
printf("管理员请输入password,读者请回车\n");
gets(pass);
if(strcmp(pass,"password") == 0)
{
//管理员的诸项操作
swit();
}
else
{
//读者的诸项操作
printf("回车查询书目,输入“B”还书,其他输入查询详细信息\n");
readerenter = getchar();
if(readerenter == '\n')
{
contents();
}
else if(readerenter == 'B')
{
bring();
}
else
{
reader_search();
}
}
return 0;
}

int contents(void)//查询书目
{
char agree;
char line[MAX];
FILE *book;
if((book = fopen("book.txt","r")) == NULL)
printf("无书籍信息\n");
else
printf("编号 书名 数量\n");
while(1)
{
fgets(line,MAX,book);
if(feof(book))
break;
printf("%s",line);
}
fclose(book);
printf("借阅请输入“Y”\n");
agree = getchar();
if(agree == 'Y')
reader_search();
return 0;
}

int reader_search(void)//读者的查询
{
//输入编号或书名,查询书籍信息
FILE *book;
FILE *reader;
int choose = 0,booknum,readernum,key = 0;
char booknam[MAX],line[MAX],c;
printf("请输入:\n1.编号\n2.书名\n");
while(choose != 1 && choose != 2)
scanf("%d",&choose);
book = fopen("book.txt","r");
reader = fopen("reader.txt","r");
if(choose == 1)
{
printf("请输入书籍编号\n");
scanf("%d",&booknum);
while(!feof(book))
{
fscanf(book,"%d%20s%10d\n",&BOOK.num,&BOOK.name,&BOOK.amount);
if(booknum == BOOK.num )
{
printf("编号 书名 数量\n");
printf("%d%20s%10d\n",BOOK.num,BOOK.name,BOOK.amount);
key = 1;
break;
}
}
}
if(choose == 2)
{
printf("请输入书名\n");
scanf("%s",booknam);
while(!feof(book))
{
fscanf(book,"%d%20s%10d\n",&BOOK.num,&BOOK.name,&BOOK.amount);
if(strcmp(booknam,BOOK.name) == 0)
{
printf("编号 书名 数量\n");
printf("%d%20s%10d\n",BOOK.num,BOOK.name,BOOK.amount);
booknum = BOOK.num;
key = 1;
break;
}
}
}
if(key == 0)
{
printf("查无此书,请重新查询\n");
reader_search();
}
else
{
printf("查询完毕,输入“Y”重新查询,回车借阅\n");
getchar();
scanf("%c",&c);
if(c == 'Y')
reader_search();
else
{
printf("请输入你的读者编号\n");
scanf("%d",&readernum);
while(READER.number == readernum)
{
fscanf(reader,"%d%20s%d\n",&READER.number,&READER.name,&READER.bookborrowed);
}
if(READER.bookborrowed != 0)
{
printf("请先还书\n");
return 1;
}
else
{
printf("编号 姓名 借阅\n");
printf("%d%20s%10d\n",READER.number,READER.name,BOOK.num);
}
}
fclose(reader);
fclose(book);
borrow(readernum,booknum);
}
return 0;
}

int admin_search(void)//管理员的查询
{
//输入编号,查询书籍信息和借阅情况
FILE *book;
FILE *reader;
int choose = 0,booknum,readernum,key = 0;
char booknam[MAX],line[MAX],c;
printf("请输入:\n1.编号\n2.书名\n");
while(choose != 1 && choose != 2)
scanf("%d",&choose);
book = fopen("book.txt","r");
reader = fopen("reader.txt","r");
if(choose == 1)
{
printf("请输入书籍编号\n");
scanf("%d",&booknum);
while(!feof(book))
{
fscanf(book,"%d%20s%10d\n",&BOOK.num,&BOOK.name,&BOOK.amount);
if(booknum == BOOK.num )
{
printf("编号 书名 数量\n");
printf("%d%20s%10d\n",BOOK.num,BOOK.name,BOOK.amount);
key = 1;
break;
}
}
}
if(choose == 2)
{
printf("请输入书名\n");
scanf("%s",booknam);
while(!feof(book))
{
fscanf(book,"%d%20s%10d\n",&BOOK.num,&BOOK.name,&BOOK.amount);
if(strcmp(booknam,BOOK.name) == 0)
{
printf("编号 书名 数量\n");
printf("%d%20s%10d\n",BOOK.num,BOOK.name,BOOK.amount);
booknum = BOOK.num;
key = 1;
break;
}
}
}
if(key == 0)
{
printf("查无此书,请重新查询\n");
admin_search();
}
else
{
printf("这些人借了这本书\n");
while(!feof(reader))
{
fscanf(reader,"%d%20s%d\n",&READER.number,&READER.name,&READER.bookborrowed);
if(READER.bookborrowed == booknum)
{
printf("%d%20s%10d\n",READER.number,READER.name,BOOK.num);
}
}
}
swit();
return 0;
}

int enter_reader(void)//录入读者信息
{
FILE *reader;
char agree,temp[MAX];
int num;
reader = fopen("reader.txt","r+");
printf("请输入新增的读者信息(姓名 所借图书编号)\n");
scanf("%s %d",&READER.name,&READER.bookborrowed);
while(!feof(reader))
fscanf(reader,"%d%20s%d\n",&READER.number,temp,num);
fprintf(reader,"%d%20s%10d\n",READER.number+1,READER.name,READER.bookborrowed);
printf("回到选择操作面板请输入“Y”\n");
getchar();
agree = getchar();
if(agree == 89)
swit();
fclose(reader);
return 0;
}

int enter_book(void)//录入书籍信息
{
FILE *book;
char agree,temp[MAX];
int num;
book = fopen("book.txt","r+");
printf("请输入新增的书籍信息(书名 数目)\n");
scanf("%s %d",&BOOK.name,&BOOK.amount);
while(!feof(book))
fscanf(book,"%d%20s%d\n",&BOOK.num,temp,&num);
fprintf(book,"%d%20s%10d\n",BOOK.num+1,BOOK.name,BOOK.amount);
printf("回到选择操作面板请输入“Y”\n");
getchar();
agree = getchar();
if(agree == 89)
swit();
fclose(book);
return 0;
}

int conrrect_reader(void)//修改读者信息
{
int readernum,bookborrowed;
char readername[MAX];
FILE *reader = fopen("reader.txt","r");
FILE *rereader = fopen("rereader.txt","w");
printf("请输入要修改的读者的编号\n");
scanf("%d",&readernum);
printf("请输入该读者新的信息(姓名 所借图书的编号)\n");
scanf("%s %d",&readername,&bookborrowed);
while(!feof(reader))
{
fscanf(reader,"%d%20s%10d\n",&READER.number,&READER.name,&READER.bookborrowed);
if(readernum != READER.number)
fprintf(rereader,"%d%20s%10d\n",READER.number,READER.name,READER.bookborrowed);
else
fprintf(rereader,"%d%20s%10d\n",READER.number,readername,bookborrowed);
}
printf("操作成功!\n");
fclose(reader);
fclose(rereader);
remove("reader.txt");
rename("rereader.txt","reader.txt");
return 0;
}

int conrrect_book(void)//修改书籍信息
{
int booknum,bookamount;
char bookname[MAX];
FILE *book = fopen("book.txt","r");
FILE *rebook = fopen("rebook.txt","w");
printf("请输入要修改的书籍的编号\n");
scanf("%d",&booknum);
printf("请输入该书籍新的信息(书名 数量)\n");
scanf("%s %d",&bookname,&bookamount);
while(!feof(book))
{
fscanf(book,"%d%20s%10d\n",&BOOK.num,&BOOK.name,&BOOK.amount);
if(booknum != READER.number)
fprintf(rebook,"%d%20s%10d\n",BOOK.num,BOOK.name,BOOK.amount);
else
fprintf(rebook,"%d%20s%10d\n",BOOK.num,bookname,bookamount);
}
printf("操作成功!\n");
fclose(book);
fclose(rebook);
remove("book.txt");
rename("rebook.txt","book.txt");
return 0;
}

int delet_reader(void)//删除读者信息
{
int readernum;
FILE *reader = fopen("reader.txt","r");
FILE *rereader = fopen("rereader.txt","w");
printf("请输入要删除的读者的编号\n");
scanf("%d",&readernum);
while(!feof(reader))
{
fscanf(reader,"%d%20s%10d\n",&READER.number,&READER.name,&READER.bookborrowed);
if(readernum != READER.number)
fprintf(rereader,"%d%20s%10d\n",READER.number,READER.name,READER.bookborrowed);
else
;
}
printf("操作成功!\n");
fclose(reader);
fclose(rereader);
remove("reader.txt");
rename("rereader.txt","reader.txt");
return 0;
}

int delet_book(void)//删除书籍信息
{
int booknum;
FILE *book = fopen("book.txt","r");
FILE *rebook = fopen("rebook.txt","w");
printf("请输入要删除的书籍的编号\n");
scanf("%d",&booknum);
while(!feof(book))
{
fscanf(book,"%d%20s%10d\n",&BOOK.num,&BOOK.name,&BOOK.amount);
if(booknum != BOOK.num)
fprintf(rebook,"%d%20s%10d\n",BOOK.num,BOOK.name,BOOK.amount);
else
;
}
printf("操作成功!\n");
fclose(book);
fclose(rebook);
remove("book.txt");
rename("rebook.txt","book.txt");
return 0;
}

int swit(void){
int handle = 0;
printf("请输入您要进行的操作的编号:\n1.查询书籍信息\n2.录入读者信息\n3.录入书籍信息\n4.修改读者信息\n5.修改书籍信息\n6.删除读者信息\n7.删除书籍信息\n8.退出\n");
scanf("%d",&handle);
getchar();
switch(handle)
{
case 1:admin_search();
break;
case 2:enter_reader();
break;
case 3:enter_book();
break;
case 4:conrrect_reader();
break;
case 5:conrrect_book();
break;
case 6:delet_reader();
break;
case 7:delet_book();
break;
case 8:
return 0;
default:printf("输入错误,请重新输入:\n");
swit();
break;
};
return 0;
}

int borrow(int readernum,int booknum)
{
int flag = 1;
FILE *reader = fopen("reader.txt","r");
FILE *rereader = fopen("rereader.txt","w");
FILE *book = fopen("book.txt","r");
FILE *rebook = fopen("rebook.txt","w");
while(!feof(book))
{
fscanf(book,"%d%20s%10d\n",&BOOK.num,&BOOK.name,&BOOK.amount);
if(booknum != BOOK.num)
fprintf(rebook,"%d%20s%10d\n",BOOK.num,BOOK.name,BOOK.amount);
else if(BOOK.amount == 0)
{
printf("书已被借光\n");
flag = 0;
fprintf(rebook,"%d%20s%10d\n",BOOK.num,BOOK.name,BOOK.amount);
}
else
fprintf(rebook,"%d%20s%10d\n",BOOK.num,BOOK.name,BOOK.amount-1);
}
while(!feof(reader))
{
fscanf(reader,"%d%20s%10d\n",&READER.number,&READER.name,&READER.bookborrowed);
if(readernum != READER.number)
fprintf(rereader,"%d%20s%10d\n",READER.number,READER.name,READER.bookborrowed);
else if(!flag)
fprintf(rereader,"%d%20s%10d\n",READER.number,READER.name,READER.bookborrowed);
else
fprintf(rereader,"%d%20s%10d\n",READER.number,READER.name,booknum);

}
if(flag)
printf("借阅成功!\n");
fclose(reader);
fclose(rereader);
remove("reader.txt");
rename("rereader.txt","reader.txt");
fclose(book);
fclose(rebook);
remove("book.txt");
rename("rebook.txt","book.txt");
}

int bring(void)
{
int readernum,booknum,flag = 1;
printf("请输入你的读者编号\n");
scanf("%d",&readernum);
FILE *reader = fopen("reader.txt","r");
FILE *rereader = fopen("rereader.txt","w");
FILE *book = fopen("book.txt","r");
FILE *rebook = fopen("rebook.txt","w");
while(!feof(reader))
{
fscanf(reader,"%d%20s%10d\n",&READER.number,&READER.name,&READER.bookborrowed);
if(readernum != READER.number)
fprintf(rereader,"%d%20s%10d\n",READER.number,READER.name,READER.bookborrowed);
else if(READER.bookborrowed == 0)
{
printf("您不需要还书\n");
fprintf(rereader,"%d%20s%10d\n",READER.number,READER.name,READER.bookborrowed);
flag = 0;
}
else
{
fprintf(rereader,"%d%20s%10d\n",READER.number,READER.name,0);
booknum = READER.bookborrowed;
}
}
while(!feof(book))
{
fscanf(book,"%d%20s%10d\n",&BOOK.num,&BOOK.name,&BOOK.amount);
if(booknum != BOOK.num)
fprintf(rebook,"%d%20s%10d\n",BOOK.num,BOOK.name,BOOK.amount);
else if(!flag)
fprintf(rebook,"%d%20s%10d\n",BOOK.num,BOOK.name,BOOK.amount);
else
fprintf(rebook,"%d%20s%10d\n",BOOK.num,BOOK.name,BOOK.amount+1);
}
if(flag)
printf("还书成功!\n");
fclose(reader);
fclose(rereader);
remove("reader.txt");
rename("rereader.txt","reader.txt");
fclose(book);
fclose(rebook);
remove("book.txt");
rename("rebook.txt","book.txt");
}

posted @ 2015-05-17 12:21  xbhlp  阅读(818)  评论(0编辑  收藏  举报