版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
简单的图书管理系统
book.h
#include<string>
#include<fstream>
using namespace std;
class Book
{
private:
string name; //书名
float price; //单价
public:
Book(); //构造函数
Book::Book(const string Name,const float newPrice);//重载构造函数
string get_name(); //获取书名
void setName(const string newName); //设置书名
float get_price(); //获取单价
void setPrice(const float newPrice);//更改单价
};
Book::Book()//构造函数
{
name="underDefine";
price=0;
}
Book::Book(const string Name,const float newPrice)//重载构造函数
{
name=Name;
price=newPrice;
}
//下面是Book类的成员函数
string Book::get_name() //获取书名
{
return name;
}
void Book::setName(const string newName) //设置书名
{
name=newName;
}
float Book::get_price() //获取单价
{
return price;
}
void Book::setPrice(const float newPrice)
{
price=newPrice;
}
//**********************************************************************************
//**********************************************************************************
class BookE:public Book
{
protected:
string isbn; //图书的isbn号
string publisher; //出版社名称
string writer; //作者姓名
int quantity; //数量
public:
BookE(); //构造函数
BookE::BookE(const string Name,const string Writer,const string Isbn,const string Publisher,const float Price,const int Quantity);//构造函数
string get_write(); //获取作者
void setWriter(const string newWriter);//更改作者
string get_publisher();//获取出版社
int get_quantity(); //获取数量
string get_isbn(); //获取isbn
void setPublisher(const string newPublisher);//更改出版社
void setQuantity(const int newQuantity); //更改数量
void setIsbn(const string newIsbn); //更改isbn
void display();
};
BookE::BookE() //构造函数BookE
{
};
BookE::BookE(const string Name,const string Writer,const string Isbn,const string Publisher,const float Price,const int Quantity):Book(Name,Price)//重载构造函数
{
isbn=Isbn;
writer=Writer;
publisher=Publisher;
quantity=Quantity;
}
string BookE::get_publisher() //获取作者
{
return publisher;
}
int BookE::get_quantity() //获取数量
{
return quantity;
}
string BookE::get_isbn() //获取isbn
{
return isbn;
}
void BookE::setPublisher(const string newPublisher)//更改出版社
{
publisher=newPublisher;
}
void BookE::setQuantity(const int newQuantity) //更改数量
{
quantity=newQuantity;
}
void BookE::setIsbn(const string newIsbn) //更改Isbn
{
isbn=newIsbn;
}
string BookE::get_write() //获取作者
{
return writer;
}
void BookE::setWriter(const string newWriter) //更改作者
{
writer=newWriter;
};
//**************************************************************************
//**************************************************************************
class BookList
{
BookE bookList[10]; //生成BookE类对象数组,即图书列表数组
public:
int bookNum; //储存图书列表中当前的图书数目
//void display();
BookList()
{
bookNum=0;
}
void readFile()
{
float pr,qw;
char Temp[20],Temp1[20],Temp2[20],Temp3[20];
ifstream infile("BookList.txt");
//定义文件指针infile指向BookList.txt
if(!infile) //打开失败
{
cerr<<"cannot open BookList.txt for output\n";
return;
}
while((infile>>Temp)) //infile读入数据为空,则循环终止
{
infile>>pr;
infile>>Temp1;
infile>>Temp2;
infile>>Temp3;
infile>>qw;
insert(Temp,pr, Temp1,Temp2,Temp3,qw);
}
cout<<"从BookList.txt中读出已有图书如下:"<<endl;
showAll(); //显示所有图书书名
infile.close();
}
void writeFile()
{
ofstream outfile("BookList.txt",ios::out); //输出文件打开
if(! outfile) //打开失败
{
cerr<<"cannot open BookList.txt for output\n";
exit(-1);
}
for( int i=0;i<bookNum;i++) //新书信息保存到文件中
{
outfile<<setiosflags(ios::left)<<setw(15)<<bookList[i].get_name();
outfile<<setw(15)<<bookList[i].get_price();
outfile<<setw(15)<<bookList[i].get_write();
outfile<<setw(15)<<bookList[i].get_isbn();
outfile<<setw(15)<<bookList[i].get_publisher();
outfile<<setw(15)<<bookList[i].get_quantity()<<endl;
}
outfile.close();
}
void insert(string newName,float newPrice,const string Writer,const string Isbn,const string Publisher,const int Quantity)//添加信息功能函数
{
bookList[bookNum].setName(newName);
bookList[bookNum].setPrice(newPrice);
bookList[bookNum].setWriter(Writer);
bookList[bookNum].setIsbn(Isbn);
bookList[bookNum].setQuantity(Quantity);
bookNum++;
}
void showBook(int i) //输出一本图书
{
cout<<setiosflags(ios::left)<<"书名:"<<bookList[i].get_name()<<setiosflags(ios::left)<<endl;
cout<<setw(15)<<"价格"<<bookList[i].get_price()<<endl;
cout<<setiosflags(ios::left)<<setw(5)<<"作者:"<<bookList[i].get_write()<<endl;
cout<<setiosflags(ios::left)<<setw(15)<<"Isbn:"<<bookList[i].get_isbn()<<endl;
cout<<setiosflags(ios::left)<<setw(15)<<"出版社:"<<bookList[i].get_publisher()<<endl;
cout<<setiosflags(ios::left)<<setw(15)<<"数量:"<<bookList[i].get_quantity()<<endl;
/*cout<<bookList[i].get_name()<<setw(15);
cout<<bookList[i].get_price()<<setw(15);
cout<<bookList[i].get_write()<<setw(15);
cout<<bookList[i].get_isbn()<<setw(15);
cout<<bookList[i].get_publisher()<<setw(15);
cout<<bookList[i].get_quantity()<<setw(15);*/
}
int search(string Name) //查找图书信息功能函数
{
int i;
for(i=0;i<bookNum;i++)
{
if(bookList[i].get_name()==Name)
return i;
}
if(i==bookNum)return -1;
}
void update(int i,string newName)//修改图书信息功能函数
{
bookList[i].setName(newName);
}
void deleted(int i) //删除图书信息功能函数
{
for(int j=i;j<(bookNum-1);j++)
{
bookList[j].setName(bookList[j+1].get_name());
bookList[j].setPrice(bookList[j+1].get_price());
bookList[j].setWriter(bookList[j+1].get_write());
bookList[j].setIsbn(bookList[j+1].get_isbn());
bookList[j].setPublisher(bookList[j+1].get_publisher());
bookList[j].setQuantity(bookList[j+1].get_quantity());
}
bookNum--;
}
void showBook_title() //输出列名
{
cout<<setiosflags(ios::left)<<setw(15)<<" "<<setiosflags(ios::left);
cout<<setw(15)<<" "<<setiosflags(ios::left)<<setw(15)<<" ";
cout<<setiosflags(ios::left)<<setw(15)<<" ";
cout<<setiosflags(ios::left)<<setw(15)<<" ";
cout<<setiosflags(ios::left)<<setw(15)<<" "<<endl;
}
void showAll() //输出所有图书的书名
{
showBook_title();
for(int i=0;i<bookNum;i++)
showBook(i);
cout<<"共有图书"<<bookNum<<"本"<<endl;
}
};
/*void BookE::display()
{
cout <<bookList[i].get_name();
}*/
/*ostream & operator << (ostream & coutl,BookE & book)//运算符重载方法,输出图书信息
{
cout<<setiosflags(ios::left)<<setw(15)<<book.get_name();
cout<<setw(15)<<book.get_price()<<setw(15)<<book.get_write();
cout<<setw(15)<<book.get_isbn()<<setw(15)<<book.get_publisher();
cout<<setw(15)<<book.get_quantity()<<endl;
return coutl;
}*/
main函数
#include<iostream>
#include<iomanip>
#include<fstream>
#include"book.h"
using namespace std;
int main()
{
BookList b1;
float pr,qw;
char Temp[20],Temp1[20],Temp2[20],Temp3[20];
int f=1;
b1.readFile();
while(f==1)
{
//输出主菜单
cout<<"1.添加2.查询3.修改4.删除5.显示全部6.退出\n\n";
int xz;
cin>>xz;
if(xz==1)
{
//添加图书信息
cout<<"请输入新书的书名:";
cin>>Temp;
cout<<"请输入新书的作者:";
cin>>Temp1;
cout<<"请输入新书的Isbn:";
cin>>Temp2;
cout<<"请输入新书的出版社:";
cin>>Temp3;
cout<<"请输入新书的单价:";
cin>>pr;
cout<<"请输入新书的数量:";
cin>>qw;
b1.insert(Temp,pr,Temp1,Temp2,Temp3,qw);
//b1.showBook(b1.bookNum-1);
}
if(xz==2)
{
//查找图书信息
cout<<"请要查找图书的书名:";
cin>>Temp;
int x=b1.search(Temp);
if(x>=0)
b1.showBook(x);
else
cout<<"未找到该书!"<<endl;
}
if(xz==3)
{
//修改图书信息
cout<<"请输入要修改图书的书名:";
cin>>Temp;
int y=b1.search(Temp);
if(y>=0)
{
cout<<"请输入该图书新的书名:";
cin>>Temp;
b1.update(y,Temp);
b1.showBook(y);
}
else
cout<<"未找到该书!"<<endl;
}
if(xz==4)
{
//删除图书信息
cout<<"请输入要删除图书的书名:";
cin>>Temp;
int z=b1.search(Temp);
if(z>=0)
{
b1.deleted(z);
cout<<"共有图书本数:"<<b1.bookNum<<endl;
}
else
cout<<"未找到该书!"<<endl;
}
if(xz=5)
{
//显示所有图书
b1.showAll();
}
if(xz==6)
{
b1.writeFile();
f=0;//不符合循环条件退出
}
}
return 0;
}
运行结果: