C++图书管理系统代码 只要图书类~

2025-01-03 21:40:31
推荐回答(2个)
回答1:

#include
#include
#include
#include
using namespace std;

class Book{
public:

void addBook();
string bookInfo(bool is_borrowed = false) const;
void modifiedInfo();

inline bool findBook(const string &info, bool by_isbn = false) const{
if (by_isbn)
return m_isbn == info;
else
return m_author == info || m_isbn == info || m_name == info || m_publisher == info;
}

private:
string m_name, m_author, m_isbn, m_publisher;
};

class System{
public:

void findBookInfo() const;
Book *findBook() const;
void modifiedBookInfo();
void addBook();
void borrowBook();
void removeBook();
void popMenu();

inline void leftBookNum() const{
cout << "剩余图书:" << books.size() << endl << "已借出:" << borrowed_books.size() << endl;
cout << "总计:" << books.size() + borrowed_books.size() << endl;
};

private:
vector books, borrowed_books;
};

void Book::addBook(){
cout << "请输入图书名称:";
cin >> m_name;
cout << "请输入图书作者:";
cin >> m_author;
cout << "请输入图书编号:";
cin >> m_isbn;
cout << "请输入图书出版社:";
cin >> m_publisher;

cout << "添加成功!" << endl;
}

string Book::bookInfo(bool is_borrowed) const{
string info;
info = "书名:" + this->m_name + (is_borrowed ? "(已借出)\n" : "\n");
info += "作者:" + this->m_author + "\n编号:" + this->m_isbn + "\n出版社:" + this->m_publisher + "\n";
return info;
}

void Book::modifiedInfo(){
cout << "修改:\n1.书名\n2.作者\n3.编号\n4.出版社\n请选择:";
int n;
cin >> n;

switch(n){
case 1:{
cout << "请输入图书名称:";
cin >> this->m_name;
break;
}
case 2:{
cout << "请输入图书作者:";
cin >> this->m_author;
break;
}
case 3:{
cin >> "请输入图书编号:";
cin >> this->m_isbn;
break;
}
case 4:{
cout << "请输入图书出版社:";
cin >> this->m_publisher;
break;
}
default:{
cout << "选择错误!" << endl;
return;
}
}
cout << "修改成功!" << endl;
}

Book *System::findBook() const{
cout << "请输入书本编号:";
string isbn;
cin >> isbn;

Book *book = NULL;
for (size_t t = 0; t != books.size(); t++){
if (books.at(t)->findBook(isbn, true)){
book = books.at(t);
break;
}
}

if (book == NULL){
for (size_t t = 0; t != borrowed_books.size(); t++){
if (borrowed_books.at(t)->findBook(isbn, true)){
book = borrowed_books.at(t);
break;
}
}
}

return book;
}

void System::findBookInfo() const{
cout << "请输入图书名称/作者/编号/出版社 来搜索:";
string info;
cin >> info;

int count = 0;
size_t t = 0;
for (t = 0; t != books.size(); t++){
if (books.at(t)->findBook(info)){
cout << books.at(t)->bookInfo() << endl;
count++;
}
}

for (t = 0; t != borrowed_books.size(); t++){
if (borrowed_books.at(t)->findBook(info)){
cout << borrowed_books.at(t)->bookInfo(true) << endl;
count++;
}
}

cout << "共找到" << count << "条记录" << endl;
}

void System::addBook(){
Book *book = new Book;
book->addBook();
books.push_back(book);
}

void System::modifiedBookInfo(){
Book *book = findBook();

if(book == NULL)
cout << "无此书!" << endl;
else
book->modifiedInfo();
}

void System::removeBook(){
Book *book = findBook();
if(book == NULL){
cout << "无此书!" << endl;
return;
}

vector::iterator it = books.begin();
for(it = books.begin(); it != books.end(); it++){
if((*it) == book){
books.erase(it);
cout << "删除成功!" << endl;
return;
}
}
for(it = borrowed_books.begin(); it != borrowed_books.end(); it++){
if((*it) == book){
borrowed_books.erase(it);
cout << "删除成功!" << endl;
return;
}
}
cout << "删除失败!" << endl;
}

void System::borrowBook(){
Book *book = findBook();
if(book == NULL){
cout << "无此书!" << endl;
return;
}

for(vector::iterator it = books.begin(); it != books.end(); it++){
if((*it) == book){
books.erase(it);
break;
}
}
borrowed_books.push_back(book);

cout << "借书成功!" << endl;
}

void System::popMenu(){
while(true){
const string menu = "================================\n图书管理系统\n1.图书录入\n2.图书信息修改\n3.图书删除\n4.图书查询\n5.借书\n6.输出书本总量\n7.退出\n请选择:";
cout << menu;
int n;
cin >> n;

switch(n){
case 1:{
addBook();
break;
}
case 2:{
modifiedBookInfo();
break;
}
case 3:{
removeBook();
break;
}
case 4:{
findBookInfo();
break;
}
case 5:{
borrowBook();
break;
}
case 6:{
leftBookNum();
break;
}
case 7:{
exit(0);
}
default:{
cout << "选择错误" << endl;
cin.sync();
cin.clear();
break;
}
}
}
}

int main(){
System *sys = new System;
sys->popMenu();
return 0;
}

回答2:

/*****************************************************************************************/
#include
#include
#include
#include //输入/输出文件流类
using namespace std;
const int Maxr=100;//最多的读者
const int Maxb=100;//最多的图书
const int Maxbor=5;//每位读者最多借五本书
//读者类,实现对读者的信息的描述

class Reader
{
private:
int tag; //删除标记 1:已删 0:未删
int no; //读者编号
char name[10]; //读者姓名
int borbook[Maxbor];//所借图书
public:
Reader() {}
char *getname() {return name;} //获取姓名
int gettag() {return tag;} //获取删除标记
int getno() {return no;} //获取读者编号
void setname(char na[]) //设置姓名
{
strcpy(name,na);

}
void delbook(){ tag=1; }//设置删除标记 1:已删 0:未删
void addreader(int n,char *na)//增加读者
{
tag=0;
no=n;
strcpy(name,na);
for(int i=0;i borbook[i]=0;
}
void borrowbook(int bookid)//借书操作
{
for(int i=0;i {
if (borbook[i]==0)
{
borbook[i]=bookid;
return;

}
}

}
int retbook(int bookid)//还书操作
{
for(int i=0;i {
if(borbook[i]==bookid)
{
borbook[i]=0;
return 1;

}
}
return 0;
}
void disp()//读出读者信息
{
cout << setw(5) << no < for(int i=0;i if(borbook[i]!=0)
cout << borbook[i] << "|";
cout << "]"<
}
};

//读者类库,实现建立读者的个人资料
class RDatabase
{
private:
int top; //读者记录指针
Reader read[Maxr];//读者记录
public:
RDatabase() //构造函数,将reader.txt读到read[]中
{
Reader s;
top=-1;
fstream file("reader.txt",ios::in);//打开一个输入文件
while (1)
{
file.read((char *)&s,sizeof(s));
if (!file)break;
top++;
read[top]=s;
}
file.close(); //关闭 reader.txt
}
void clear()//删除所有读者信息
{
top=-1;
}
int addreader(int n,char *na)//添加读者时先查找是否存在
{
Reader *p=query(n);
if (p==NULL)
{
top++;
read[top].addreader(n,na);
return 1;
}
return 0;

}
Reader *query(int readerid)//按编号查找
{
for (int i=0;i<=top;i++)
if (read[i].getno()==readerid &&
read[i].gettag()==0)
{
return &read[i];
}
return NULL;
}
void disp() //输出所有读者信息
{
for (int i=0;i<=top;i++)
read[i].disp();
}
void readerdata();//读者库维护
~RDatabase() //析构函数,将read[]写到reader.txt文件中
{
fstream file("reader.txt",ios::out);
for (int i=0;i<=top;i++)
if (read[i].gettag()==0)
file.write((char *)&read[i],sizeof(read[i]));
file.close();

}
};
void RDatabase::readerdata()
{

char choice;
char rname[20];
int readerid;
Reader *r;
while (choice!='0')
{
cout <<"\n\n\t\t\t读 者 维 护\n\n\n\t\t 1 新 增\n\n\t\t 2 更 改\n\n\t\t 3 删 除\n\n\t\t 4 查 找\n\n\t\t 5 显 示\n\n\t\t 6 全 删\n\n\t\t 0 退 出"< cin >> choice;
switch (choice)
{
case '1':
cout << "输入读者编号:";
cin >> readerid;
cout << "输入读者姓名:";
cin >> rname;
addreader (readerid,rname);
break;
case '2':
cout << "输入读者编号:";
cin >> readerid;
r=query(readerid);
if (r==NULL)
{
cout << " 该读者不存在 "< break;
}
cout << "输入新的姓名:";
cin >> rname;
r->setname(rname);
break;
case '3':
cout << " 输入读者编号:";
cin >> readerid;
r=query(readerid);
if (r==NULL)
{
cout <<" 该读者不存在" << endl;
break;
}
r->delbook();
break;
case '4':
cout << "读入读者编号:";
cin >> readerid;
r=query(readerid);
if (r==NULL)
{
cout <<"该读者不存在"<< endl;
break;
}
r->disp();
break;
case '5':
disp();
break;
case '6':
clear();
break;
default:cout<<"输入错误,请从新输入:";break;
}
}
}

//图书类,实现对图书的描述,图书的编号,书名,借出,还入等
class Book
{
private:
int tag;//删除标记 1:已删 0:未删
int no;//图书编号
char name[20];//书名
int onshelf;//是否再架 1:再架 2:已借
public:
Book(){}
char *getname() { return name; }//获取姓名
int getno(){ return no; }//获取图书编号
int gettag(){ return tag; }//获取删除标记
void setname(char na[])//设置书名
{
strcpy(name,na);
}
void delbook(){ tag=1;}//删除图书
void addbook(int n,char *na)//增加图书
{
tag=0;
no=n;
strcpy(name,na);
onshelf=1;
}
int borrowbook()//借书操作
{
if (onshelf==1)
{
onshelf=0;
return 1;
}
return 0;
}
void retbook()//还书操作
{
onshelf=1;
}
void disp()//输出图书
{
cout << setw(6) << no << setw(18) << name << setw(10)
<<(onshelf==1? "在架":"已借") < }
};

//图书库类,实现对图书的维护,查找,删除等
class BDatabase
{
private:
int top; //图书记录指针
Book book[Maxb]; //图书记录
public:
BDatabase()//构造函数,将book.txt读到book[]中
{
Book b;
top=-1;
fstream file("book.txt",ios::in);
while (1)
{
file.read((char *)&b,sizeof(b));
if (!file) break;
top++;
book[top]=b;
}
file.close();
}
void clear()//全删
{
top=-1;
}
int addbook(int n,char *na)//增加图书
{
Book *p=query(n);
if (NULL==p)
{
top++;
book[top].addbook(n,na);
return 1;
}
return 0;
}
Book *query(int bookid)//查找图书
{
for (int i=0;i<=top;i++)
if (book[i].getno()==bookid &&book[i].gettag()==0)
{
return &book[i];
}
return NULL;
}
void bookdata();//图书库维护
void disp()
{
for (int i=0;i<=top;i++)
if (book[i].gettag()==0)
book[i].disp();
}
~BDatabase()//析构函数,将book[]写到book.txt文件中
{
fstream file("book.txt",ios::out);
for (int i=0;i<=top;i++)
if (book[i].gettag()==0)
file.write((char *)&book[i],sizeof(book[i]));
file.close();
}
};
void BDatabase::bookdata()
{
char choice;
char bname[40];
int bookid;
Book *b;
while (choice!='0')
{
cout <<"\n\n\n\t\t\t图 书 维 护 "< cout<<"\t\t1 新 增\n \t\t2 更 改\n\t\t3 删 除\n\t\t4 查 找\n\t\t5 显 示\n\t\t6 全 删\n\t\t0 退 出"< cin >> choice;
switch (choice)
{
case '1':
cout << "输入图书编号:"< cin >> bookid;
cout << "输入图书书名:"< cin >> bname;
addbook(bookid,bname);
break;
case '2':
cout << "输入图书编号:"< cin >> bookid;
b=query(bookid);
if (b==NULL)
{
cout << " 该图书不存在 "< break;
}
cout << "输入新的书名:"< cin >> bname;
b->setname(bname);
break;
case '3':
cout <<" 读入图书编号:"< cin >> bookid;
b=query(bookid);
if (b==NULL)
{
cout <<" 该图书不存在" << endl;
break;
}
b->delbook();
break;
case '4':
cout << " 读入图书编号:"< cin >> bookid;
b=query(bookid);
if (b==NULL)
{
cout <<" 该图书不存在"<< endl;
break;
}
b->disp();
break;
case '5':
disp();
break;
case '6':
clear();
break;
default:cout<<"输入错误,请从新输入:";
}
}
}

//main() 函数的实现,程序的主界面的引导

void main()
{
char choice;
int bookid,readerid;
RDatabase ReaderDB;
Reader *r;
BDatabase BookDB;
Book *b;
while(choice!='0')
{
cout <
cout <<"\t\t\t1 借 书\n\n\t\t\t2 还 书 \n\n\t\t\t3 图 书 维 护\n\n\t\t\t4 读 者 维 护\n\n\t\t\t0 离 开"< cin >> choice;
switch (choice)
{
case '1':
cout <<" 借书 读者编号:";
cin >>readerid;
cout <<" 图书编号: ";
cin >>bookid;
r=ReaderDB.query(readerid);//按编号查找
if (NULL==r)
{
cout <<" 不存在该读者,不能借书"<< endl;
break;
}
b=BookDB.query(bookid);
if (b==NULL)
{
cout <<" 不存在该图书,不能借书"<< endl;
break;
}
if (b->borrowbook()==0)
{
cout << " 该图书已借出,不能借书"<< endl;
break;
}
r->borrowbook(b->getno());
break;
case '2':
cout<<"还书\n 读者编号:";
cin >>readerid;
cout << " 图书编号:";
cin >>bookid;
r=ReaderDB.query(readerid);
if (r==NULL)
{
cout <<" 不存在该读者,不能还书" << endl;
break;
}
b=BookDB.query(bookid);
if (b==NULL)
{
cout <<" 不存在该图书,不能还书" < break;
}
b->retbook();
r->retbook(b->getno());
break;
case '3':
BookDB.bookdata();
break;
case '4':
ReaderDB.readerdata();
break;
default:cout<<"输入错误,请从新输入:";

}
}
}