额。。。最近好多人要这个东东。。不明白是什么意思。。。我自己写了一个,你看看。。。
用C++ 的 模板写的。。。
不理解的地方咱继续交流。。。。
//book_class.h 代码如下:
#include <iostream>
#include <string>
#include <list>
#include <fstream>
#include <algorithm>
#include <iterator>
using namespace std;
typedef struct book_class
{
string book_number;
string book_name;
string book_public_infor;
string book_type; //图书类型;
string book_pbulic_time;//出版时间;
}book;
class equal1
{
public:
string str;
equal1(string str1):str(str1)
{}
bool operator () (book & book1)
{
if(book1.book_name==str || book1.book_number==str || book1.book_public_infor==str || book1.book_type==str)
return true;
return false;
}
};
class book_manage
{
public:
list<book> book_store_list;
public:
book_manage(){}
void sort();
void show();
void insert();
void erase();
void inquire();
void save(){};
void show(list<book>::iterator itr);
friend bool operator < (const book &book1,const book &book2)
{
return book1.book_name<book2.book_name;
}
friend ostream & operator << ( ostream &out,const book &book1)
{
out<<book1.book_name<<" "<<book1.book_number<<" "
<<book1.book_pbulic_time<<" "<<book1.book_public_infor<<" "
<<book1.book_type<<endl;
return out;
}
};
//book_class.cpp 实现文件内容如下:
#include "book_class.h"
void book_manage::sort()
{
int number;
if(this->book_store_list.size()==0)
{
cout<<"no book in the store!"<<endl;
return ;
}
loop: cout<<"input the information you want the book sort by:"<<endl;
cout<<"you can input:"<<endl;
cout<<"1:book_name"<<endl;
cout<<"2:book_num"<<endl;
cout<<"3:book_public_time."<<endl;
cout<<endl;
cin>>number;
cout<<" input the infor:"<<endl;
switch(number)
{
case 1:
this->book_store_list.sort();
cout<<"the information of book had been sort by book's name!"<<endl;
break;
case 2:
break;
case 3:
break;
default:
cout<<"Ǹʲô"<<endl;
goto loop;
}
cout<<" this is the new sort!"<<endl;
}
void book_manage::show()
{
if(this->book_store_list.size()==0)
cout<<"no book in the store!"<<endl;
else
{
ostream_iterator<book> os(cout," ");
copy(this->book_store_list.begin(),this->book_store_list.end(), os);
}
system("pause");
return ;
}
void book_manage::insert()
{
book_class book1;
cout<<"input the new book's information:"<<endl;
cout<<"eg: 001 ߵѧ 2001-9-10 "<<endl;
cin>>book1.book_number>>book1.book_name>>book1.book_public_infor>>book1.book_pbulic_time>>book1.book_type;
this->book_store_list.push_back(book1);
cout<<"insert successfully!"<<endl;
system("pause");
}
void book_manage::erase()
{
string book_name;
cin>>book_name;
list<book>::iterator itr=find_if(this->book_store_list.begin(),this->book_store_list.end(),equal1(book_name));
if(itr==this->book_store_list.end())
cout<<"can't find the book!"<<endl;
else
this->book_store_list.erase(itr);
}
void book_manage::inquire()
{
string book_infor;
bool find_flag=false;
cin>>book_infor;
list<book>::iterator itr=find_if(this->book_store_list.begin(),this->book_store_list.end(),equal1 (book_infor));
if(itr==this->book_store_list.end())
{
cout<<"can't find the information you want!"<<endl;
return;
}
while(itr!=this->book_store_list.end())
{
this->show(itr);
++itr;
itr=find_if(itr,this->book_store_list.end(),equal1 (book_infor));
}
}
void book_manage::show(list<book>::iterator itr)
{
cout<<"you may need it!"<<endl;
cout<<(*itr).book_name<<" "<<(*itr).book_number<<" "
<<(*itr).book_pbulic_time<<" "<<(*itr).book_public_infor<<" "
<<(*itr).book_type<<endl;
}
//main.cpp 主文件如下:
#include "book_class.h"
using namespace std;
int main()
{
//ifstream infile("book.txt");
// book_manage store(infile);
book_manage store;
loop: cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ "<<endl<<endl<<endl;
cout<<"\t welcome to sxl.book_manage 1.0"<<endl<<endl;
cout<<"\t\tthe is the menu!"<<endl<<endl;
cout<<"\t\t1: show the information of book !"<<endl;
cout<<"\t\t2: sort the book's information !"<<endl;
cout<<"\t\t3: insert some book!"<<endl;
cout<<"\t\t4: erase some book!"<<endl;
cout<<"\t\t5: inquire book!"<<endl;
cout<<"\t\t6: exit the system!"<<endl<<endl;
int chose;
cout<<"input your chose:";
cin>>chose;
switch(chose)
{
case 1:
system("cls");
store.show();
cout<<"press any key to continue!"<<endl;
system("cls");
goto loop;
case 2:
system("cls");
store.sort();
store.show();
system("cls");
goto loop;
case 3:
store.insert();
store.show();
system("cls");
goto loop;
case 4:
cout<<"input the book's name or num you want to erase:";
store.erase();
store.show();
system("cls");
goto loop;
case 5:
cout<<" you may input the book's number,or book's name or book's pulbic_type:";
cout<<" input the book's infor:"<<endl;
store.inquire();
system("pause");
system("cls");
goto loop;
case 6:
store.save();
return 0;
default:
cout<<" Ǹʲô?"<<endl;
system("pause");
cout<<"press any key to chose again!"<<endl;
system("cls");
goto loop;
}
return 0;
}
我的博客里边 有一些 关于 C 和C++ 的东东。。。你感兴趣的话看看吧。。呵呵。。祝你好运
http://hi.baidu.com/sx_liang/blog