⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 manager.cpp

📁 本系统完成了要求的所有功能
💻 CPP
字号:
/* 程序名:  manager.cpp

   作者:  罗成行

   班级:03计算机1班

   学号:2003374119

   日期:  2005-4-25

   最好修改日期:  2005-5-5.

   这个程序用来处理图书馆的日常问题,如增减书目,图书的借阅与归还。
 
   为了要在Visual C++下正确地编译的这一个程序,你将会需要有如下的文件:
   btree.h      help.h      lib.dat     reader.dat

   测试环境:
      Microsoft Visual C++ 6.0
      Microsoft Visual C++ .NET
      windows xp;
*/

//#define DEBUG
//#define DEBUG_BTREE

#include <cstdlib>
#include <cstring>
#include <new>
#include <iostream>
#include <fstream>
#include <ctime>
#include "btree.h" //b树的操作函数.
#include "help.h"
using namespace std;

typedef struct Book{//存放图书信息
	int num;  //书号
	char name[30];//书名
	char author[30];//作者
	int Now_store;//现存量
	int Total_store;//总库存
}Book;

typedef struct reader{ //存放读者信息
	long int ID;//借阅证号
	int num;//所借阅的书的序号
	struct tm Re_data;//应当还书的日期.借书后一个月要归还.
}Reader;




/*	函数声明  */

void AddBookInfo(int rec,Book &REC,FILE *fp);//改变图书的现存量和总存量.
void input_BI(Book &REC);//输入图书信息.
int LoadFile(FILE *fp,BTree &T);//读取书库的图书信息,按书号建立B树索引
void ReaderInfo(FILE *fp2);//显示读者的信息
void AddBook(FILE *fp,BTree &T);//图书入库.
void ReturnBook(FILE *fp,FILE *fp2,BTree &T);//归还图书。
void BorrowBook(FILE *fp,FILE *fp2,BTree &T);//借阅图书
void DelBook(FILE *fp,BTree &T);//清除库存.
void SearchBook(FILE *fp,BTree T);//查找某本书,并显示该书的具体信息
void ShowBook(FILE *fp);//输出书库中所有图书的信息
void BookInfo(Book REC);//输出图书的具体信息.
void main_switch(char i,FILE *fp,BTree &,FILE *);//操作选择函数
void Menu();//菜单函数
void Menu_name();

/*-----------------------------------------*/

int rec=0; //全局变量,用来表示图书信息在文件中的相对于文件头的位置

void main(void){
	
#ifdef DEBUG_BTREE
	display();	
#else	
	int j;
	char a[100];
	
	char re_choose[]={"\nThe choice is invalid, please re- choose...\n"};
    BTree T=NULL;
	FILE *fp,*fp2;

#ifdef DEBUG
	if((fp=fopen("lib2.dat","rb+"))==NULL) 
		{
			printf("cannot open the file of library data!");
			exit(0);
		}
	if((fp2=fopen("reader2.dat","rb+"))==NULL) 
		{
			printf("cannot open the file of reader data!");
			exit(0);
		}
#else
	if((fp=fopen("lib.dat","rb+"))==NULL) 
		{
			printf("cannot open the file of library data!");
			exit(0);
		}
	if((fp2=fopen("reader.dat","rb+"))==NULL) 
		{
			printf("cannot open the file of reader data!");
			exit(0);
		}
#endif

	rec=LoadFile(fp,T);//读取之前文件中存放的信息
	if(!rec) rec-=rec;
	
    system("cls");
	Menu_name();
	system("pause");
	system("cls");

    while(1)
	{
		
		Menu();
		for(j=0;j<100;j++)
		{
			scanf("%c",&a[j]);
			if(a[j]=='\n') break;
		}   /*功能选择*/
		
        if(a[1]!='\n'){
			cout <<"\n"<<re_choose<<endl;
			system("pause");
			system("cls");
			continue;
		}
        else{
			if(a[0]=='0') break;
			main_switch(a[0],fp,T,fp2);
			
			
		}
	}//end while
	fclose(fp);
	fclose(fp2);
#endif
}


/*---------------------------------------------*/


void main_switch(char i,FILE *fp,BTree &T,FILE *fp2)
  //操作选择函数
{
  
	char re_choose[]={"\nThe choice is invalid, please re- choose...\n"};
    switch(i)
			{
			case '1' ://图书借阅
				system("cls");
				BorrowBook(fp,fp2,T);
				system("pause");		
				system("cls");
				getchar();
				break;
			case '2' ://归还图书
				system("cls");
				ReturnBook(fp,fp2,T);
				getchar();
				system("pause");
				system("cls");
				break;
			case '3' ://增加图书到库存里
				system("cls");
				AddBook(fp,T);
				system("pause");
				system("cls");
				getchar();
				break;
			case '4'://从库存里清除没用的图书
				system("cls");
				DelBook(fp,T);
				getchar();
				system("pause");
				system("cls");
				break;

			case '5'://查看某本书的资料
				system("cls");
				SearchBook(fp,T);
				getchar();
				system("pause");
				system("cls");
				break;

			case '6'://查看读者的借阅情况
				system("cls");
				cout<<"\nThe borrow of reader reads the circumstance:"<<endl;
				ReaderInfo(fp2);
				system("pause");
				system("cls");
				break;
			case '7'://查看库存里所有图书的信息
				system("cls");
				cout<<"\nThe information of all books is as follows:"<<endl;
				ShowBook(fp);
				system("pause");
				system("cls");
				break;
			case '8'://查看B树的状态
				system("cls");
				showtree(T,1);
				cout<<"\n\n";
				system("pause");
				system("cls");
				break;
			case '9': 
				system("cls");
				Help();
				system("pause");
				system("cls");
				break;
			default  :
				cout <<re_choose<<endl;
				system("pause");
				system("cls");
		
			}//end switch
}


void Menu()
     //菜单函数

{
	char Input[]={"\tPlease input number: "};
	cout <<"\n\n\t\t"<<"=============LIBRARY=============="<<endl;
	cout <<"\n\t\t"<<"Please choose a function:"<<endl;
	cout <<"\n\t\t"<<"1.Borrow to read the book."<<endl;
	cout <<"\t\t2.Return the book." << endl;
	cout <<"\t\t3.Add book in store." << endl;
    cout <<"\t\t4.Clearance store."<<endl;
	cout <<"\t\t5.Check the book."<<endl;
	cout <<"\t\t6.Look into reader's circumstance."<<endl;
	cout <<"\t\t7.Look into the book circumstance."<<endl;
	cout <<"\t\t8.Show the B tree appearance."<<endl;
	cout <<"\t\t9.Help."<<endl;
	cout <<"\t\t0.Exit.\n"<<endl;
	cout <<"\t\t===============================\n"<<endl;
	cout << Input ;
}//Menu();


void Menu_name()
     //作者信息
{   
	printf("\n\n\n\n\n\n\n");
	printf("              *************************************************\n");
	printf("                           图书管理系统\n\n");
	printf("                           制作:罗成行\n");
	printf("                           班级:03计算机(1)班\n");
	printf("                           学号: 2003374119\n");
	printf("                           指导老师: 孙爱东\n");
	printf("              **************************************************\n");
	printf("\n\n\n\t\t");
}



/*---------------------------------------------*/

void AddBookInfo(int rec,Book &REC,FILE *fp)
    //改变图书的现存量和总存量.
{

	int j=REC.Now_store;//j存放增加的数目
	
	fseek(fp,rec,0);
	fread(&REC,sizeof(Book),1,fp);
	int k=REC.Total_store;
	REC.Now_store+=j;
	REC.Total_store+=j;
	fseek(fp,rec,0);
	fwrite(&REC,sizeof(Book),1,fp);//覆盖原来的图书信息

}



void input_BI(Book &REC)
  //输入图书信息.
{
	printf("\nPlease input the book information");
	printf("\nBook number:");
	scanf("%d%*c",&REC.num);
	printf("\nBook name:");
	gets(REC.name);
	printf("\nauthor:");
	gets(REC.author);
	printf("\nDeposit the quantity:");
	scanf("%d",&REC.Now_store);
}


int LoadFile(FILE *fp,BTree &T)
   //读取书库的图书信息,按书号建立B树索引
{
	int rec=0;
	int flag=-2;//文件尾端的判断标志,用来防止尾端数据
				//重读的情况的发生
	Book REC;
	Result R;
	REC.num=flag;
	while(!feof(fp))
	{
		fread(&REC,sizeof(Book),1,fp);
		if(flag==REC.num) break;
		if(REC.num==-1) {
			rec+=sizeof(Book);
			continue;
		}
		R=SearchBTree(T,REC.num,0);
		InsertBTree(T,REC.num,R.pt,R.i,rec);
        rec+=sizeof(Book);
		flag=REC.num;
	}
	return(rec);
}



void ReaderInfo(FILE *fp2){
	//显示读者的信息
	Reader person3;
	int flag2=0;
    int flag3=-2;
	int flag1=-2;
	person3.ID=flag3;
	rewind(fp2);
	while(!feof(fp2)){
		fread(&person3,sizeof(Reader),1,fp2);
		//借阅证号和借的图书的编号均相同则表示已到文件尾端,返回.
	    if((flag3==person3.ID) && (flag1==person3.num)) return;
		if(person3.ID<=-1){
			flag2+=sizeof(Reader);
			continue;
		}
		cout<<"\nID:"<<person3.ID<<endl;
		cout<<"book number:"<<person3.num<<endl;
		cout<<"return time:";
		cout<<person3.Re_data.tm_year;
		cout<<"-"<<person3.Re_data.tm_mon;
		cout<<"-"<<person3.Re_data.tm_mday<<endl;
		flag2+=sizeof(Reader);
		flag3=person3.ID;
		flag1=person3.num;
	}
	//空文件时提示没有读者资料.
	if(flag2==0) cout<<"\nNo reader's data.\n"<<endl;
}



void AddBook(FILE *fp,BTree &T){
	//图书入库.
	//当书库中已有该图书,改变其现存量和总存量.
    //当书库中没有该图书,则增加该图书信息到书库里
	Book REC;
	Result R;
	cout<<"\n----------ADD BOOK----------";
	input_BI(REC);
	R=SearchBTree(T,REC.num,0);
	if(R.tag==1) AddBookInfo((R.pt)->recptr[R.i],REC,fp);
	else 
	{
		REC.Total_store=REC.Now_store;
		InsertBTree(T,REC.num,R.pt,R.i,rec);
		fseek(fp,0,SEEK_END);  //找到文件末尾的位置
		fwrite(&REC,sizeof(Book),1,fp);//入库
		rec+=sizeof(Book);
		showtree(T,1);
	}
}



void ReturnBook(FILE *fp,FILE *fp2,BTree &T){
	//归还图书。
	//查找该读者是否借阅了该图书,是就归还成功,不是就返回.
	Reader person2,person;
	Book REC;
	Result R;
	int flag;
	cout<<"\n----------RETURN BOOK----------";
	cout<<"\ninput you ID please:";
	cin>>person.ID;
	cout<<"\nPlease input the number of the book which you want to return:";
	int id4;
	cin>>id4;
	rewind(fp2);
	while(!feof(fp2)){
		//查找该读者是否借阅了该图书,是就归还成功,不是就返回.
		fread(&person2,sizeof(Reader),1,fp2);
		if(person2.ID==-1)continue;
		if((person2.ID==person.ID) && (person2.num==id4)){
			flag=1;
			break;
		}
	}
	if(flag==1){
		R=SearchBTree(T,id4,0);
		if(R.tag==0) {
			//读者虽然借阅了该图书,但该书以被清库,读者不需再还书.
			cout<<"\nThis book has been destory!You need not return back,you lucky!\n"<<endl;
			return;
		}
		//读取系统本地时间作为实际归还日期
		time_t loctime;
		struct tm *ptr;
		loctime = time(NULL);
		ptr = localtime(&loctime);
		cout<<"\nThe book has been return successful!"<<endl;
		cout<<"\nYour return's time is :";
		cout<<asctime(ptr)<<endl;
		//更新书库中的资料
		int rec5=R.pt->recptr[R.i];
		fseek(fp,rec5,SEEK_SET);
		fread(&REC,sizeof(Book),1,fp);
		REC.Now_store++;
		fseek(fp,rec5,SEEK_SET);
		fwrite(&REC,sizeof(Book),1,fp);
		cout<<"After return,this book's information as follow:";
		BookInfo(REC);//输出图书的具体信息
		person2.ID=-1;
		fseek(fp2,-sizeof(Reader),SEEK_CUR);
		fwrite(&person2,sizeof(Reader),1,fp2);
	}
	else
		cout<<"\nno this user or you had not borrow this book!"<<endl;
}



void BorrowBook(FILE *fp,FILE *fp2,BTree &T){
	//借阅图书
	//登记读者的证号和应归还的日期.
	Book REC;
	Result R;
	Reader person;
	cout<<"\n----------BORROW BOOK----------";
	cout<<"\nInput your ID please:";
	cin>>person.ID;
    cout<<"\nWhich book you want to borrow ?:";			
	int id;
	cin>>id;
	person.num=id;
	R=SearchBTree(T,id,0);
	if(R.tag==0) printf("\nIt have no this book record in the stack!\n");
	else
	{
		int rec2=R.pt->recptr[R.i];
		fseek(fp,rec2,SEEK_SET);
		fread(&REC,sizeof(Book),1,fp);
		if(REC.Now_store!=0){
			REC.Now_store--;
			fseek(fp,rec2,SEEK_SET);
			fwrite(&REC,sizeof(Book),1,fp);
			cout<<"\nThe following is the information of the book after you borrow:"<<endl;
			BookInfo(REC);	
			//读取本地时间,按一个月为一个租借期,存放读者的信息.
			tm *bo_time;						
			time_t t;
			t= time(NULL);
			bo_time = localtime(&t);
			person.Re_data.tm_mon=bo_time->tm_mon+2;
			person.Re_data.tm_year=bo_time->tm_year+1900;
			person.Re_data.tm_mday=bo_time->tm_mday;						
			fseek(fp2,0,SEEK_END);
			fwrite(&person,sizeof(Reader),1,fp2);
			cout<<"\nYou must return this book before this date:";						
			cout<<person.Re_data.tm_year;					
			cout<<"-"<<person.Re_data.tm_mon;
			cout<<"-"<<person.Re_data.tm_mday<<"\n"<<endl;
		}
		else printf("\nAt present stock is 0, please wait for after other people return again to borrow to read.");

	}
}


void DelBook(FILE *fp,BTree &T){
	//清除库存.
	//如果该书被删除了,则屏蔽书库中该书的信息,
	//并删除该书在B树的索引。
	Book REC;
	Result R;
	cout<<"\n----------DELETE BOOK----------";
	cout << "\nThe book number of the book that you want to delete is: ";
	int id2;	
	cin >> id2;
	R=SearchBTree(T,id2,0);
	if(R.tag==0) printf("\nIt have no this book record in the stack!\n\n");
	else
	{
		int rec3=R.pt->recptr[R.i];
		fseek(fp,rec3,SEEK_SET);
		fread(&REC,sizeof(Book),1,fp);
		cout<<"\nYou want that the information of the book for delete is as follows:"<<endl;
		BookInfo(REC);
		//确认删除.
		cout<<"\nDelete ? (y/n):";
		char cmd;
		cin >> cmd;
		if(cmd=='n'){
			return;
		}
		else{
			//清除库存
			REC.num=-1;
			fseek(fp,rec3,SEEK_SET);
			fwrite(&REC,sizeof(Book),1,fp);
			DelBTree(T,id2);
			cout << "\nSucceed clearance the stock of that book!"<<endl;
			showtree(T,1);
		}
	}
}


void SearchBook(FILE *fp,BTree T){
	//查找某本书,并显示该书的具体信息
	Book REC;
	Result R;
	cout << "\nThe book number of the book that you want to check is: ";		
	int id3;
	cin >> id3;
	R=SearchBTree(T,id3,0);
	if(R.tag==0) printf("\nIt have no this book record in the stack!\n");
	else
	{
		int rec4=R.pt->recptr[R.i];
		fseek(fp,rec4,SEEK_SET);
		fread(&REC,sizeof(Book),1,fp);
		cout<<"\nYou want that the information of the book for check is as follows:"<<endl;
		BookInfo(REC);//输出信息
	}
}


void ShowBook(FILE *fp){
	//输出书库中所有图书的信息
	int flag=-2;
	Book REC;
	REC.num=flag;
	rewind(fp);
	while(!feof(fp))
	{
		fread(&REC,sizeof(Book),1,fp);
		if(flag==REC.num) break;
		if(REC.num==-1) continue;
#ifndef DEBUG
		BookInfo(REC);
#else
		cout<<"\nBook number: "<<REC.num<<endl;
#endif
		flag=REC.num;
	}
}


void BookInfo(Book REC){
	//输出图书的具体信息.
	cout<<"\nBook number: "<<REC.num<<endl;
	cout<<"Book name: "<<REC.name<<endl;
	cout<<"Author: "<<REC.author<<endl;
	cout<<"Existing quantity: "<<REC.Now_store<<endl;
	cout<<"Total save the quantity: "<<REC.Total_store<<endl;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -