#include <iostream>
#include<iomanip>
#include<string>
#include<fstream>
#include<stdlib.h>
using namespace std;
class Book
{
private:
string name; //书名
float price; //单价
public:
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()//构造函数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(const string Name,const string Writer,const string Isbn,const string Publisher,const float Price,const int Quantity);//构造函数
string get_writer(); //获取作者
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
};
//**********************************************************
BookE::BookE()//构造函数Book()
{
isbn="无";
writer="无";
publisher="无";
quantity=0;
};
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_writer() //获取作者
{
return writer;
};
void BookE::setWriter(const string newWriter) //更改作者
{
writer=newWriter;
};
class BookList
{
BookE bookList[10]; //生成Book类对象数组,即图书列表数组
public:
int bookNum; //存储图书列表中当前的图书数目
BookList()
{
bookNum=0;
}
void readFile()
{
float pr,qw;
char Temp[20],Temp1[20],Temp2[20],Temp3[20];
int xu;
ifstream infile;
cout<<"1.直接默认打开Booklist.txt。2.请输入您想要打开的文件名称(不带扩展名,默认文件名为file)"<<endl;
cout<<"请选择序号:"<<endl;
cin>>xu;
if(xu==1)
{
ifstream infile("Booklist.txt");
}
if(xu==2)
{
char fname[20];
cout<<"请输入您想要打开的文件名称(不带扩展名,默认文件名为file):";
cin>>fname;
ifstream infile(fname,ios::in);
}
//定义文件指针infile指向BookList.txt
if (!infile ) // 打开失败
{
cerr<<"cannot open Booklist.txt for output\n";
exit( -1 );
}
while((infile>>Temp)) //infile读入数据为空,则循环终止
{
infile>>pr;
infile>>Temp1;
infile>>Temp2;
infile>>Temp3;
infile>>qw;
insert(Temp,pr,Temp1,Temp2,Temp3,qw);
}
cout<<"从文件中读出已有图书如下:"<<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()<<setw(15)<<bookList[i]. get_price()<<setw(15)<<bookList[i].get_writer()<<setw(15)<<bookList[i]. get_isbn()<<setw(15)<<bookList[i].get_publisher()<<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].setPublisher(Publisher);
bookList[bookNum].setQuantity(Quantity);
bookNum++;
}
void showBook(int i) //输出一本图书
{
cout<<setiosflags(ios::left)<<setw(15)<<bookList[i].get_name()<<setw(15)<<bookList[i]. get_price()<<setw(15)<<bookList[i].get_writer()<<setw(15)<<bookList[i]. get_isbn()<<setw(15)<<bookList[i].get_publisher()<<setw(15)<<bookList[i].get_quantity()<<endl;
}
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_writer());
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)<<setw(15)<<"价格"<<setiosflags(ios::left)<<setw(15)<<"作者"<<setiosflags(ios::left)<<setw(15)<<"Isbn"<<setiosflags(ios::left)<<setw(15)<<"出版社"<<setiosflags(ios::left)<<setw(15)<<"数量"<<endl;
}
void showAll() //输出所有图书的书名
{
showBook_title();
for (int i=0; i<bookNum; i++)
showBook(i);
cout<<"共有图书"<<bookNum<<"本"<<endl;
}
friend ostream & operator << (ostream & cout1,BookE & book) ; //运算符重载方法,输出图书信息
};
ostream & operator << (ostream & cout1,BookE & book) //运算符重载方法,输出图书信息
{
cout1<<setiosflags(ios::left)<<setw(15)<<book.get_name()<<setw(15)<<book. get_price()<<setw(15)<<book.get_writer()<<setw(15)<<book. get_isbn()<<setw(15)<<book. get_publisher()<<setw(15)<<book. get_quantity()<<endl;
return cout1;
}
int main()
{
BookList bl;
float pr,qw;
char Temp[20],Temp1[20],Temp2[20],Temp3[20];
int f=1;
while (f==1)
{
//输出主菜单
cout<<"1.添加 2.查询 3.修改 4.删除 5.显示全部 6.导入文件 7.退出\n\n";
int xz;
cin>>xz;
if (xz==1)
{
system("cls");
//添加图书信息
cout<<"请输入新书的书名:";
cin>>Temp;
cout<<"请输入新书的作者:";
cin>>Temp1;
cout<<"请输入新书的Isbn:";
cin>>Temp2;
cout<<"请输入新书的出版社:";
cin>>Temp3;
cout<<"请输入新书的单价:";
cin>>pr;
cout<<"请输入新书的数量:";
cin>>qw;
bl.insert(Temp,pr,Temp1,Temp2,Temp3,qw);
bl. showBook_title();
bl.showBook(bl.bookNum-1);
}
if (xz==2)
{
system("cls");
//查找图书信息
cout<<"请要查找图书的书名:";
cin>>Temp;
int x=bl.search(Temp);
if( x>=0)
{
bl.showBook_title();
bl.showBook(x);
}
else
{
cout<<"未找到该书!"<<endl;
}
}
if (xz==3)
{
system("cls");
//修改图书信息
cout<<"请输入要修改图书的书名:";
cin>>Temp;
int y=bl.search(Temp);
if( y>=0)
{
cout<<"请输入该图书新的书名:";
cin>>Temp;
bl.update(y,Temp);
bl.showBook_title();
bl.showBook(y);
}
else
cout<<"未找到该书!"<<endl;
}
if (xz==4)
{
system("cls");//删除图书信息
cout<<"请输入要删除图书的书名:";
cin>>Temp;
int z=bl.search(Temp);
if( z>=0)
{
bl.deleted(z);
cout<<"共有图书本数:"<<bl.bookNum<<endl;
}
else
cout<<"未找到该书!"<<endl;
}
if (xz==5)
{
system("cls");//显示所有图书
bl.showAll();
}
if(xz==6)
{
system("cls");
bl.readFile();
}
if (xz==7)
{
bl.writeFile();
f=0;//不符合循环条件退出
}
}
return 0;
}
图书管理系统
最新推荐文章于 2024-03-13 12:51:37 发布