图书信息管理系统添加功能模快函数
排序(价格,借书,还书
有源码
#include
#include
#include
#include
usingnamespacestd;
//Book.h
classBook//1
{
private:
stringname;
floatprice;
public:
Book();
Book(conststringName,constfloatnewPrice);
stringget_name();
voidsetName(conststringnewName);
floatget_price();
voidsetPrice(constfloatnewPrice);
};
//Book.app
Book::Book()
{
name="underDefine";
price=0;
}
Book::Book(conststringName,constfloatnewPrice)
{
name=Name;
price=newPrice;
}
stringBook::get_name()//2
{
returnname;
}
voidBook::setName(conststringnewName)
{
name=newName;
}
floatBook::get_price()
{
returnprice;
}
voidBook::setPrice(constfloatnewPrice)
{
price=newPrice;
}
classBookE:publicBook
{ //3
//BookE.h
protected:
stringisbn;
stringauthor;
stringpublisher;
intquantity;
public:
BookE();
BookE(conststringName,constfloatPrice,conststringIsbn,conststringAuthor,
conststringPublisher,constintQuantity);
stringget_isbn();
voidsetIsbn(conststringnewIsbn);
stringget_author();
voidsetAuthor(conststringnewAuthor);
stringget_publisher();
voidsetPublisher(conststringnewPublisher);
intget_quantity();
voidsetQuantity(constintnewQuantity);
friendostream&operator<<(ostream&coutl,BookE&book);
};
//BookE.app
BookE::BookE(){};
BookE::BookE(conststringName,constfloatPrice,conststringIsbn,conststringAuthor,
conststringPublisher,constintQuantity)
:Book(Name,Price)
{
isbn=Isbn;
author=Author;
publisher=Publisher;
quantity=Quantity;
}
stringBookE::get_isbn()//get
{
returnisbn;
}
stringBookE::get_author()
{
returnauthor;
}
stringBookE::get_publisher()
{
returnpublisher;
}
intBookE::get_quantity()
{
returnquantity;
}
voidBookE::setIsbn(conststringnewIsbn)//set
{
isbn=newIsbn;
}
voidBookE::setAuthor(conststringnewAuthor)
{
author=newAuthor;
}
voidBookE::setPublisher(conststringnewPublisher)
{
publisher=newPublisher;
}
voidBookE::setQuantity(constintnewQuantity)
{
quantity=newQuantity;
}
ostream&operator<<(ostream&coutl,BookE&book)
{
coutl<< setiosflags(ios::left)<< setw(15)<< book.get_name()<< setw(15)<<
book.get_author()
<<setw(15)<<book.get_isbn()<<setw(15)<<book.get_publisher()<<setw(15)
<<book.get_price()<<book.get_quantity()<<endl;
return coutl;
}
classBookList
{
//BookList.h
BookEbookList[10];
public:
intbookNum;
BookList()//1
{
bookNum=0;
}
voidreadFile()//2
{
floatprice;
intquantity;
//charTemp[20],Temp1[20],Temp2[20],Temp3[20];
stringtemp,isbn,publisher,author;
ifstreaminfile("Booklist.txt",ios::app);
if(!infile)
{
cerr<<"cannotopenBooklist.txtforinput!";
return;
}
while((infile>>temp))
{
infile>>price;
infile>>temp;
infile>>isbn;
infile>>publisher;
infile>>author;
infile>>quantity;
insert(temp,price,isbn,publisher,author,quantity);
}
cout<<"从 BookList.txt中读出已有图书如下:"<<endl;
showAll();
infile.close();
}
voidwriteFile()//3
{
ofstreamoutfile("Booklist.txt",ios::out);
if(!outfile)
{
cerr<<"cannotopenBooklist.txtforoutput\n";
exit(-1);
}
for(inti=0;i<bookNum;i++)
{
outfile<<setw(15)<<bookList[i].get_name();
outfile<<setw(15)<<bookList[i].get_price();
outfile<<setw(15)<<bookList[i].get_author();
outfile<<setw(15)<<bookList[i].get_isbn();
outfile<<setw(15)<<bookList[i].get_publisher();
outfile<<setw(15)<<bookList[i].get_quantity()<<endl;
}
outfile.close();
}
voidinsert(stringnewName,floatnewPrice,conststringnewIsbn,
const string newAuthor, const string newPublisher, const int
newQuantity)
{
bookList[bookNum].setName(newName);
bookList[bookNum].setPrice(newPrice);
bookList[bookNum].setIsbn(newIsbn);
bookList[bookNum].setAuthor(newAuthor);
bookList[bookNum].setPublisher(newPublisher);
bookList[bookNum].setQuantity(newQuantity);
bookNum++;
}
voidshowBook(inti)//5
{
cout<<bookList[i];
}
intsearch(stringName)//6
{
inti;
for(i=0;i<bookNum;i++)
{
if(bookList[i].get_name()==Name)
returni;
}
return-1;
}
voidupdate(inti,stringnewName,floatnewPrice,stringnewIsbn,
stringnewAuthor,stringnewPublisher,intnewQuantity)//7
{
bookList[i].setName(newName);
bookList[i].setPrice(newPrice);
bookList[i].setIsbn(newIsbn);
bookList[i].setPublisher(newPublisher);
bookList[i].setAuthor(newAuthor);
bookList[i].setQuantity(newQuantity);
}
voiddeleted(inti)//8
{
intj;
for(intj=i;j<(bookNum-1);j++)
{
bookList[j].setName(bookList[j+1].get_name());
bookList[j].setPrice(bookList[j+1].get_price());
bookList[j].setIsbn(bookList[j+1].get_isbn());
bookList[j].setAuthor(bookList[j+1].get_author());
bookList[j].setPublisher(bookList[j+1].get_publisher());
bookList[j].setQuantity(bookList[j+1].get_quantity());
}
bookNum--;
}
voidshowBook_title()//9
{
cout<<setw(15)<<"书名"<<setw(15)<<"价格"<<setw(15)<<"isbn"<<setw(15)
<<"出版社"<<setw(15)<<"作者"<<setw(15)<<"数量"<<endl;
}
voidshowAll()//10
{
showBook_title();
for(inti=0;i<bookNum;i++)
{
showBook(i);
}
cout<<"共有图书"<<bookNum<<"本"<<endl;
}
};
intmain()
{
BookListbl;
floatprice;
intquantity;
stringtemp,isbn,publisher,author;
intf=1;
bl.readFile();
while(f==1)
{
cout<<"--------------------------------------------------\n";
cout<<"1.添加 2.查询 3.修改 4.删除 5.显示全部 6.退出\n";
cout<<"--------------------------------------------------\n"<<endl;
intxz;
cin>>xz;
if(xz==1)
{
cout<<"请输入新书的书名:";
cin>>temp;
cout<<"请输入新书的作者:";
cin>>author;
cout<<"请输入新书的 Isbn:";
cin>>isbn;
cout<<"请输入新书的出版社:";
cin>>publisher;
cout<<"请输入新书的单价:";
cin>>price;
cout<<"请输入新书的数量:";
cin>>quantity;
bl.insert(temp,price,isbn,author,publisher,quantity);
bl.showBook(bl.bookNum-1);
}
if(xz==2)
{
cout<<"请要查找图书的书名:";
cin>>temp;
intx=bl.search(temp);
if(x>=0)
{
bl.showBook(x);
}
else
{
cout<<"未找到该书!"<<endl;
}
}
if(xz==3)
{
cout<<"请输入修改图书的书名:";
cin>>temp;
inty=bl.search(temp);
if(y>=0)
{
cout<<"请输入该图书新的书名:";
cin>>temp;
cout<<"请输入图书修改后的作者:";
cin>>author;
cout<<"请输入修改后的 ISBN:";
cin>>isbn;
cout<<"请输入修改后的出版社:";
cin>>publisher;
cout<<"请输入修改后的单价:";
cin>>price;
cout<<"请输入修改后的数量:";
cin>>quantity;
bl.update(y,temp,price,author,isbn,publisher,quantity);
bl.showBook(y);
}
else
{
cout<<"未找到该书!"<<endl;
}
}
if(xz==4)
{
cout<<"请输入要删除的图书的书名:";
cin>>temp;
cout<<"请输入要删除的图书的作者:";
cin>>author;
cout<<"请输入要删除的图书的 ISBN:";
cin>>isbn;
cout<<"请输入要删除的图书出版社:";
cin>>publisher;
cout<<"请输入要删除的图书单价:";
cin>>price;
cout<<"请输入要删除的图书数量:";
cin>>quantity;
intz=bl.search(temp);
if(z>=0)
{
bl.deleted(z);
cout<<"共有图书本数:"<<bl.bookNum<<endl;
}
else
{
cout<<"未找到该书!"<<endl;
}
}
if(xz==5)
{
bl.showAll();
}
if(xz==6)
{
bl.writeFile();
f=0;
}
}
}