用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - c++代码库

图书管理系统

2014-07-04 作者: 云代码会员举报

[c++]代码库

#include<iostream.h>
#include<fstream.h>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
#define max 3     
typedef struct Books{
	char book_name[15];
	int  total_num;
	int  borrow_num;
	char book_num[15];
	Books *next;
}books_node;
typedef struct Readers{
	char reader_name[15];
	char reader_no[15];
	int  reader_borrow_num;
	struct Reader_borrow_books{
		char borrow_books_name[15];
		char borrow_books_no[15];
	}reader_borrow_book[max];
	Readers *next;
}readers_node;
class books  {                                                                               
protected:

	books_node *broot;
public:
	books();
	savebooks();
	search_book();
	print_books();
	virtual add_book()=0;
	virtual delet_book()=0;
};
class manager:public books          
{
	
protected:
	readers_node *rroot;
public:
	manager(readers_node*);
	savereaders();
	add_book();
	delet_book();
	search_reader();
	add_reader();
	delet_reader();
	print_readers();
};
class reader:public books{
private:readers_node* personnel;
public:
	reader(readers_node*);
	add_book();
	reader_borrow();
	delet_book();
	reader_return();
	print_me();
	save_readers(readers_node*);
};
books::books()
{
	books_node *r;
	broot=new books_node;
	r=new books_node;
	r->next=NULL;
	broot=r;
	ifstream inFile;
	inFile.open("c:\\books.dat",ios::binary);
	if(inFile.is_open()==-1)cout<<"con't open books.dat"<<endl;
	else
	{
	        int i=0;
			while(1)
			{	
				i++;
				books_node *node;
				node=new books_node;
				inFile.read( (char*)node,sizeof(books_node));
				if(inFile.eof())   break;
				node->next=r->next;
				r->next=node;	
			}
			inFile.close();
			if(i<2)cout<<"暂时没有图书数据"<<endl;
	}
}
books::savebooks()
{
	books_node *L;
	L=new books_node;
	L=broot->next;
	
	ofstream outFile;
	outFile.open("c:\\books.dat",ios::binary);
	while(L)
	{
		outFile.write((char*)L,sizeof(books_node));
		L=L->next;
	}
	outFile.close();	
}
books::search_book()
{
	char searchedbook[15];
	cout<<"输入要查找的书的名子:";
	cin>>searchedbook;
	books_node *r;
	r=new books_node;
	r=broot->next;
	while(r)
	{
		if((strcmp(searchedbook,r->book_name))==0)
		{
			cout<<"书名:"<<r->book_name<<"      书的总量:"<<r->total_num<<"       借出:"<<r->borrow_num<<"    书的编号:"<<r->book_num<<endl;break;
			
		}
		r=r->next;
		
	}
	if(r==NULL)cout<<"对不起,没有此书"<<endl;	
}
books::print_books()
{
	books_node *r;
    r=new books_node;
	r=broot->next;
	
	while(r)
	{
		cout<<"书名:" <<r->book_name<<" 书的总量:"<<r->total_num<<"  借出:"<< r->borrow_num <<" 书的编号:"<<r->book_num<<endl;

		r=r->next;
		
	}
	delete r;
}
manager::add_book()
{
	books_node *L;
	L=new books_node;
	L=broot;
	while(1)
	{
		books_node *r;
		r=new books_node;
		r->next=NULL;
		cout<<"输入书名:";
		cin>>r->book_name;
		
		cout<<"输入书的编号:";
		cin>>r->book_num;	
		
		r->borrow_num=0;	
		cout<<"输入总数:";
		cin>>r->total_num;
		r->next=L->next;
		L->next=r;
		cout<<"add more?(n终止,其它键继续)";
		char sign;
		cin>>sign;
		if(sign=='n'||sign=='N')
			break;
	}
	broot=L;
	savebooks();	
}
manager::delet_book()
{
	char deleted[15];
	cout<<"输入要删除的图书名字:";
	cin>>deleted;
    books_node *node,*L;
	node=new books_node;
	L=new books_node;
	node=broot;
	while(node->next)
	{
		if((strcmp(deleted,node->next->book_name))==0)break;			
		node=node->next;
	}
	if(node->next==NULL) 
	{
		cout<<"sorry!There is no the book!"<<endl;
	}
	else
	{
		if(node->next->borrow_num==0)
		{
		node->next=node->next->next;
		savebooks();
		}
		else
			cout<<"对不起,暂时不能删除,因为还有没有归还的书"<<endl;
	}
}
manager::manager(readers_node*node)
{ 
	rroot=new readers_node;
	rroot=node;
}
manager::savereaders()
{
	readers_node *L;
	L=new readers_node;
	L=rroot->next;
	
	ofstream outFile;
	outFile.open("c:\\Readers.dat",ios::binary);
	
	while(L)
	{
		outFile.write((char*)L,sizeof(readers_node));
		L=L->next;
	}
	outFile.close();
}
manager::add_reader()
{
	readers_node *L;
	L=new readers_node;
	L=rroot;
	int sign=0;
	
	while(1)
	{
		readers_node *r;
		r=new readers_node;
		r->next=NULL;
		
		cout<<"输入姓名:";
		cin>>r->reader_name;
		
		cout<<"输入读者的编号:";
		cin>>r->reader_no;	
		
		r->reader_borrow_num=0;
		
		
		r->next=L->next;
		L->next=r;
		cout<<"add more?(n终止,其它键继续)";
		char sign;
		cin>>sign;
		if(sign=='n'||sign=='N')
			break;
	}
	rroot=L;
	savereaders();	
}
manager::delet_reader()
{
	char deleted[15];
	cout<<"输入要删除读者的名字:";
	cin>>deleted;
    readers_node *node,*L;
	node=new readers_node;
	L=new readers_node;
	node=rroot;
	while(node->next)
	{
		if((strcmp(deleted,node->next->reader_name))==0)break;			
		node=node->next;
	}
	if(node->next==NULL) 
	{
		cout<<"sorry!There is no the reader!"<<endl;
	}
	else
	{
		node->next=node->next->next;
		
		savereaders();
	}	
}
manager::search_reader()
{
    readers_node *r;
	r=new readers_node;
	r=rroot->next;
	char reader[15];
	cout<<"输入要查找读者的姓名:";
	cin>>reader;
	while(r)
	{
		if((strcmp(reader,r->reader_name))==0)
		{
			cout<<"姓名:"<<r->reader_name<<"    编号:"<<r->reader_no<<"        借书总数:"<<r->reader_borrow_num<<endl;
			for(int i=0;i<r->reader_borrow_num;i++)
			{
				cout<<"书名:"<<r->reader_borrow_book[i].borrow_books_name<<"            "<<"书的编号:"<<r->reader_borrow_book[i].borrow_books_no;
			}
			break;
		}
		else r=r->next;
	}
	if(!r)cout<<"没有此人"<<endl;
	
}
manager::print_readers()
{
	readers_node *printL;
	printL=new readers_node;
	printL=rroot->next;
	
	while(printL)
	{
		cout<<"姓名        编号         借书总数"<<endl;
		cout<<printL->reader_name<<"        "<<printL->reader_no<<"        "<<printL->reader_borrow_num<<endl;
		for(int i=0;i<printL->reader_borrow_num;i++)
		{
			cout<<"书名:"<<printL->reader_borrow_book[i].borrow_books_name<<"            "<<"书的编号:"<<printL->reader_borrow_book[i].borrow_books_no;
		}
		printL=printL->next;
	}
	
    delete printL;
}
reader::reader(readers_node*node)
{
	(personnel)=new readers_node;
	personnel=node;
}
reader::add_book()
{
	books_node*node;
	node=new books_node;
	node=broot->next;
	char returned[15];
	cout<<"输入要还书的名子:";
	cin>>returned;
    for(int i=0;i<personnel->reader_borrow_num;i++)
	{
		if((strcmp(returned,personnel->reader_borrow_book[i].borrow_books_name))==0)break;
	}
	if(i==personnel->reader_borrow_num+1)
	{
		printf("不好意思,你没有此书");system("pause");
	}
    else{
		
		while(node)
		{
			if((strcmp(node->book_name,returned)==0))break;
			else node=node->next;
			
		}
		if(node)
		{
			
			personnel->reader_borrow_num--;
			node->borrow_num--;
			
			savebooks();
		}
		else cout<<"对不起,没有你要还的书或者所还书被删除"<<endl;
	}
}
reader::delet_book()       
{
	if(personnel->reader_borrow_num<max)
	{
	books_node*node;
	
	node=new books_node;
	node=broot->next;

	char borrowed[15];
	cout<<"输入要借书的名子:";
	cin>>borrowed;
	while(node)
	{
		if((strcmp(node->book_name,borrowed)==0))break;
		else node=node->next;

	}
	if(node&&(node->total_num-node->borrow_num)>0)
	{
	strcpy(personnel->reader_borrow_book[personnel->reader_borrow_num].borrow_books_name,node->book_name);
	strcpy(personnel->reader_borrow_book[personnel->reader_borrow_num++].borrow_books_no,node->book_num);
	node->borrow_num++;

	savebooks();
	}
	else cout<<"对不起,没有你要借的书或者所要借的书已经借完!"<<endl;

	}
	else cout<<"对不起,你借书的数量已经超过最大借数量"<<endl;
}
reader::print_me()
{
        
		cout<<"姓名"<<personnel->reader_name<<"   编号:"<<personnel->reader_no<<"   借书总数:"<<personnel->reader_borrow_num<<endl;

			for(int i=0;i<personnel->reader_borrow_num;i++)
			{
				printf("书名:%s,书的编号:%s",personnel->reader_borrow_book[i].borrow_books_name,personnel->reader_borrow_book[i].borrow_books_no);
			}
}
void Pause()
{
	getch();
	fflush(stdin);
}
reader::reader_borrow()
{
	delet_book();
}
reader::reader_return()
{
	add_book();
}
reader::save_readers(readers_node *L)
{
	readers_node *root;
	root=new readers_node;

	root=L->next;
	ofstream outFile;
	outFile.open("c:\\Readers.dat",ios::binary);
	while(root)
	{	
		outFile.write((char*)root,sizeof(readers_node));
		root=root->next;
	}
	outFile.close();
}
readers_node* read_readers()
{
	readers_node *r;
	r=new readers_node;
	r->next=NULL;
	int flag=0;
	ifstream inFile;
	inFile.open("c:\\Readers.dat",ios::binary);
	if(inFile.is_open()==-1) 
		
	{
		cout<<"con't open readers.dat"<<endl;return r;
	}
	else
	{
		while(1)
		{
			flag++;
			readers_node *node;
			node=new readers_node;
			inFile.read((char*)node,sizeof(readers_node));
			if(inFile.eof()) break;
			node->next=r->next;
			r->next=node;
		}	
		inFile.close();
		if(flag<2)
		{
			cout<<"暂无读者 "<<endl; return r;
		}
		else			
			return r;		
	}	
}
password()
{	
	system("cls");
	char a[]="111111",b[10],c;int n=0,k;
	cerr<<"\n\n\n\n\t\t\t请输入密码,你将拥有3次机会:\n";

	while(1)
	{
		n++;
		k=0;
		cerr<<"\t\t\t输入密码:";

		while(1)
		{
			c=getch();
			if(c==13)break;
			b[k]=c;k++;
			cerr<<"*";
		}
		b[k]='\0';
		cout<<"\n";
		if(n>2)
		{
			printf("你已经超过输入次数,系统将退出\n");
			getch();
		}
		if(strcmp(a,b)==0)break;
		else cout<<"密码错误,请重新输入\n";
		cerr<<"\n";
	}
}
void manager_menu_1(manager M)
{
	int N;
	while(1)
	{
		cout<<"┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n"; 
		cout<<"┃    图书管图书管理系统                                    ┃\n"; 
		cout<<"┃        菜单                                              ┃\n"; 
		cout<<"┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫\n"; 
		cout<<"┃①增加图书                                                ┃\n"; 
		cout<<"┃ ②.删除图书                                              ┃\n";
		cout<<"┃ ③.查找图书                                              ┃\n";
		cout<<"┃ ④.输出所有                                              ┃\n"; 
		cout<<"┃   退出请按0                                              ┃\n";
		cout<<"┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n"; 
		cout<<"请输入您的选择(0--4):( )\b\b";
		cin>>N;
		switch (N)
		{
		case 1:  M.add_book();getch();break;
		case 2:	 M.delet_book();getch();break;
		case 3:  M.search_book();getch();break;
		case 4:  M.print_books();getch();break;
		case 0:  return;
		}
		system("cls");	
	}	
	
}
void manager_menu_2(manager M)
{
		int N;
	while(1)
	{
		cout<<"┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n"; 
		cout<<"┃    图书管图书管理系统                                    ┃\n"; 
		cout<<"┃         菜单                                             ┃\n"; 
		cout<<"┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫\n"; 
		cout<<"┃①增加读者                                                ┃\n"; 
		cout<<"┃ ②删除读者                                               ┃\n";
		cout<<"┃③查找读者                                                ┃\n";
		cout<<"┃④输出所有                                                ┃\n"; 
		cout<<"┃   退出请按0                                              ┃\n";
		cout<<"┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n"; 
		cout<<"请输入您的选择(0--4):( )\b\b";
		cin>>N;
		switch (N)
		{
		case 1:  M.add_reader();getch();break;
		case 2:	 M.delet_reader();getch();break;
		case 3:  M.search_reader();getch();break;
		case 4:  M.print_readers();getch();break;
		case 0:  return;
		}
		system("cls");
		
	}	
}
void manager_menu(readers_node*L)
{
	system("cls");
	password();
	manager Manager(L);
	int N;
while(1)
	{
		cout<<"┏━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n"; 
		cout<<"┃   图书管图书管理系统                                 ┃\n"; 
		cout<<"┃      菜单                                            ┃\n"; 
		cout<<"┣━━━━━━━━━━━━━━━━━━━━━━━━━━━┫\n"; 
		cout<<"┃①图书维护                                            ┃\n"; 
		cout<<"┃②读者维护                                            ┃\n"; 
		cout<<"┃退出请按0                                             ┃\n";
		cout<<"┗━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n"; 
		cout<<"请输入您的选择(0--2):( )\b\b";
		cin>>N;
		switch (N)
		{
		case 1: system("cls"); manager_menu_1(Manager);	break;
		case 2:	 system("cls"); manager_menu_2(Manager);break;
		case 0:  return;
		}
		system("cls");
	}	
}
reader_menu(readers_node*L)
{
	char name[15];
	cout<<"输入用户名:";
	cin>>name;
	readers_node* node;
	node=new readers_node;
	node=L->next;
	
	while(node)
	{
		if((strcmp(name,node->reader_name))==0)
			break;
		else 
			node=node->next;
	}
	if(node)
	{
		reader Reader(node);
		int N;
		while(1)
		{
			cout<<"┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n"; 
			cout<<"┃图书管图书管理系统                                          ┃\n"; 
			cout<<"┃      菜单                                                  ┃\n"; 
			cout<<"┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫\n"; 
			cout<<"┃①借书                                                      ┃\n"; 
			cout<<"┃ ②还书                                                     ┃\n";
			cout<<"┃③查找书                                                    ┃\n"; 
			cout<<"┃ ④.输出所有                                                ┃\n"; 
            cout<<"┃ ⑤自身信息                                                 ┃\n"; 
			cout<<"┃退出请按0                                                   ┃\n";
			cout<<"┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n"; 
			cout<<"请输入您的选择(0--5):( )\b\b";
			cin>>N;
			switch (N)
			{
			case 1: system("cls") ;Reader.reader_borrow();Reader.save_readers(L);break;
			case 2:	system("cls"); Reader.reader_return();Reader.save_readers(L);break;
			case 3: system("cls");Reader.search_book();system("pls");break;
			case 4: system("cls");Reader.print_books();getch();break;
			case 5: system("cls");Reader.print_me(); Pause();break;
			case 0:  return;
			}
			system("cls");	
			
		}
	}
	else
	{
		printf("没有这个用户!按任意键继续");
		getch();
		return;
	}	
}
main()
{
readers_node* Reader_root;
Reader_root=new readers_node;
Reader_root=read_readers();
int N;
while(1)
	{		system("cls");
		cout<<"┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n"; 
		cout<<"┃     图书管图书管理系统                                   ┃\n"; 
		cout<<"┃          菜单                                            ┃\n"; 
		cout<<"┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫\n"; 
		cout<<"┃①图书管理员                                              ┃\n"; 
		cout<<"┃ ②.读  者                                                ┃\n"; 
		cout<<"┃推出请按0                                                 ┃\n";
		cout<<"┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n"; 
		cout<<"请输入您的选择(0--2):( )\b\b";
		cin>>N;
		switch (N)
		{
		case 1:  manager_menu(Reader_root);break;
		case 2:	 reader_menu(Reader_root);break;
		case 0:exit(0);break;
		}
	}
}


网友评论    (发表评论)

共4 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...