源码链接 [link] (https://download.csdn.net/download/the_only_god/11249549)
设计并编写一个 C++风格应用程序,模拟一个图书管理系统应用程序,支持系统用户的图书借阅、图书管理、用户管理等功能。图书借阅管理主要包括图书借阅、图书归还、借阅信息查看等功能。图书管理主要包括图书的增加、删除、修改、查看、统计等功能。用户管理主要包括用户注册、登录、修改密码、修改个人信息、设置用户类型等功能。
#include
#include
#include
#include
#include
using namespace std;
/***************
/*****class*****
/*****book******
****************/
class Book
{
protected:
string title,num,author,pub;
int state;
double price;
public:
static int countBook;
void setTitle(string s,Book &b);//设置成员变量值的函数:被设置的值、被设置的对象
void setNum(string s,Book &b);
void setAuthor(string s,Book &b);
void setPub(string s,Book &b);
void setPrice(double d,Book &b);
void setState(int i,Book &b);
string getTitle(const Book &b);//返回成员变量值的函数:要返回值的对象
string getNum(const Book &b);
string getAuthor(const Book &b);
string getPub(const Book &b);
double getPrice(const Book &b);
int getState(const Book &b);
friend ostream &operator <<(ostream &out,const Book &b);
int searchBook(Book *b);//从书库中找书
friend istream &operator >>(istream &t,Book &b);
void deleteBook(Book *b);//删除图书
void alterBook(Book &b);//修改图书信息
void deleteBook();
void addBook();
void displayBook();
};
int Book::countBook=0;
void Book::setTitle(string s,Book &b)
{
b.title=s;
}
void Book::setNum(string s,Book &b)
{
b.num=s;
}
void Book::setAuthor(string s,Book &b)
{
b.author=s;
}
void Book::setPub(string s,Book &b)
{
b.pub=s;
}
void Book::setPrice(double d,Book &b)
{
b.price=d;
}
void Book::setState(int i,Book &b)
{
b.state=i;
}
string Book::getTitle(const Book &b)
{
return b.title;
}
string Book::getNum(const Book &b)
{
return b.num;
}
string Book::getAuthor(const Book &b)
{
return b.author;
}
string Book::getPub(const Book &b)
{
return b.pub;
}
double Book::getPrice(const Book &b)
{
return b.price;
}
int Book::getState(const Book &b)
{
return b.state;
}
void Book::deleteBook(Book *b)
{
Book B;
string s;
cout<<"请输入要删除书的书名或编号:";
cin>>s;
for(int i=0;i<countBook;i++)
if((s==B.getTitle(b[i]))||(s==B.getNum(b[i])))
{
cout<<b[i];
for(int j=0;j<countBook-1;j++)
b[j]=b[j+1];
countBook--;
cout<<"删除成功"<<endl;
break;
}
cout<<"未找到此书"<<endl;
}
int Book::searchBook(Book *b)
{
int flag=-1;
string s;
cout<<"请输入要查找书的书名或编号:";
cin>>s;
Book B;
for(int i=0;i<countBook;i++)
if((s==B.getTitle(b[i]))||(s==B.getNum(b[i])))
{
flag=i;
cout<<b[i];
break;
}
return flag;
}
void Book::displayBook()
{
cout<<title<<"\t"<<num<<"\t"<<author<<"\t"<<pub<<"\t"<<price<<"\t";
if(state==0)
cout<<"在馆"<<endl;
else
cout<<"已借出"<<endl;
}
void Book::addBook()
{
cout<<"请输入图书编号:";
cin>>num;
cout<<"请输入书名:";
cin>>title;
cout<<"请输入作者:";
cin>>author;
cout<<"请输入出版社:";
cin>>pub;
cout<<"请输入单价:";
cin>>price;
state = 0;
countBook++;
cout<<"添加成功"<<endl;
}
void Book::alterBook(Book &b)
{
if(b.state==1)
cout<<"图书已杰出,无法修改!"<<endl;
else
{
cout<<"请输入修改后编号:";
cin>>b.num;
cout<<"请输入修改后书名:";
cin>>b.title;
cout<<"请输入修改后作者:";
cin>>b.author;
cout<<"请输入修改后出版社:";
cin>>b.pub;
cout<<"请输入修改后单价:";
cin>>b.price;
cout<<"修改成功!"<<endl;
}
}
/************************
/*********class user*****
*************************/
class User
{
public:
string name;
User();
User(string n);
static int countUser;
void add(string s);
string getName();
};
int User::countUser=0;
User::User()
{}
User::User(string n )
{
name=n;
countUser++;
}
void User::add(string s)
{
name=s;
countUser++;
}
string User::getName()
{
return name;
}
//读者类
class Reader:public User
{
Book book[10];
struct tm *borrowTime[10];
public:
static int countReader;
int readerState;
friend class systemAdministrator;
friend class log;
int countBook,authorityState;
Reader();
Reader(string n );
void setBorrowTime(const time_t *t);
void addReader(string n);
void borrowBook(int flag,Book *book);//传入要借的书名(编号)、书库;
void returnBook(Book *book);//传入要归还的书名(编号)、书库;
void display();
friend ostream &operator<<(ostream&out,Reader &r);
friend istream &operator>>(istream&in,Reader &r);
};
int Reader::countReader=0;
Reader::Reader()
{
countBook=0;
}
Reader::Reader(string s):User(s)
{
authorityState=0;
countBook=0;
countReader++;
}
void Reader::addReader(string n)
{
authorityState=0;
name = n;
countReader++;
}
void Reader::setBorrowTime(const time_t *t)
{
borrowTime[countBook-1] = localtime(t);
}
void Reader::borrowBook(int flag,Book *book)
{
Book B;
if(B.getState(book[flag])==1)
cout<<"图书已借出!"<<endl;
else
{
this->book[countBook++]=book[flag];
B.setState(1,book[flag]);
cout<<"借阅成功"<<endl;
}
}
void Reader::returnBook(Book *b)
{
Book B;
string s;
cout<<"请输入要归还的图书名或编号:";
cin>>s;
int i;
for(i=0;i<countBook;i++)
if((s==B.getTitle(book[i]))||(s==B.getNum(book[i])))
{
cout<<book[i];
break;
}
if( i==countBook)
cout<<"未借阅此书!"<<endl;
else
{
cout<<"您确定要归还此书?"<<endl;
cout<<"1-Yes\t2-No"<<endl;
int x;
cin>>x;
switch(x)
{
case 1:
{
for(int j=i;j<countBook-1;j++)
book[j]=book[j+1];
--countBook;
break;
}
case 2:
break;
default:;
}
}
}
void Reader::display()
{
Book B;
char s[20];
cout<<"已借阅书目:"<<endl;
cout<<"书名\t"<<"编号\t"<<"作者\t"<<"出版社\t"<<"价格\t"<<"借阅时间\t"<<"应还时间"<<endl;
if(countBook == 0)
cout<<"暂无"<<endl;
else
for(int i=0;i<countBook;i++)
{
cout<<B.getTitle(book[i])<<"\t"<<B.getNum(book[i])<<"\t"<<B.getAuthor(book[i])<<"\t"<<B.getPub(book[i])<<"\t"<<B.getPrice(book[i])<<"\t";
strftime(s,20,"%Y-%m-%d",borrowTime[i]);
cout<<s<<"\t";
time_t t = mktime(borrowTime[i]);
if(readerState==1)
t += 3600*24*30;
else
t += 3600*24*60;
struct tm *tempTm = localtime(&t);
strftime(s,20,"%Y-%m-%d",tempTm);
cout<<s<<endl;
}
}
//图书管理员类
class Librarian:public User,public Book
{
public:
static int countLibrarian;
friend class systemAdministrator;
friend class log;
//创建对象函数
Librarian();
Librarian(string n);
void addLibrarian(string n);
//图书增、删、查、改、统计从Book中继承来
friend ostream &operator<<(ostream&out,Librarian &r);
friend istream &operator>>(istream&in,Librarian &r);
};
int Librarian::countLibrarian=0;
Librarian::Librarian()
{}
Librarian::Librarian(string n):User(n)
{
countLibrarian++;
}
void Librarian::addLibrarian(string n)
{
name = n;
countLibrarian++;
}
//系统管理员类
class systemAdministrator:public User
{
public:
int printReader(Reader*);
//权限管理
void setAuthorityReader(Reader *r);
};
int systemAdministrator::printReader(Reader *r)
{
if(Reader::countReader==0)
{
cout<<"暂无读者"<<endl;
return 0;
}
for(int i=0;i<Reader::countReader;i++)
{
cout<<"读者:"<<r[i].getName()<<endl;
r[i].display();
cout<<endl;
}
return 1;
}
void systemAdministrator::setAuthorityReader(Reader *r)
{
string s;
cout<<"请输入要操作的读者名:";
cin>>s;
int i;
for( i=0;i<Reader::countReader;i++)
if(r[i].getName()==s)
break;
if(i==Reader::countReader)
cout<<"未找到此读者"<<endl;
else
{
int flag;
cout<<"*****设置读者权限*****"<<endl;
cout<<"1.禁止读者借书"<<endl;
cout<<"2.允许读者借书"<<endl;
cout<<"3.禁止读者登录"<<endl;
cout<<"4.允许读者登录"<<endl;
cout<<"Your choice:";
cin>>flag;
r[i].authorityState = flag;
}
}
class Students:public Reader
{
time_t borowTime;
time_t returnTime;
};
/*****************************
/******class log**************
******************************/
extern Reader reader[10];
extern Librarian librarian[10];
extern Book book[10];
class Log
{
protected:
string id,password;
static int countReader;
static int countLibrarian;
public:
int count;//记录改log的在reader或librarian的位置
int state;//判断登录权限
int readerState;
static int countLog;
void setId(string s);
void setPassword(string s);
string getId();
string getPassword();
static int menu(int flag);
void logIn();
static int registerIn(Log *l);
friend ostream &operator <<(ostream &out,Log &l);
friend istream &operator >>(istream &in,Log &l);
};
int Log::countLibrarian=0;
int Log::countReader=0;
int Log::countLog=0;
void Log::setId(string s)
{
id = s;
}
void Log::setPassword(string s)
{
password=s;
}
string Log::getId()
{
return id;
}
string Log::getPassword()
{
return password;
}
int Log::menu(int i)
{
int flag;
if(i==1)
{
cout<<"**********注册**********"<<endl;
cout<<"1.读者注册入口"<<endl;
cout<<"2.图书管理员注册入口"<<endl;
}
else
{
cout<<"**********登录**********"<<endl;
cout<<"1.读者登录入口"<<endl;
cout<<"2.图书管理员登录入口"<<endl;
}
cout<<"Your choice:";
cin>>flag;
return flag;
}
void Log::logIn()
{
int flag=menu(1);
system("cls");
cout<<"请输入账号:";
cin>>id;
cout<<"请输入密码:";
cin>>password;
countLog++;
if(flag == 1)
{
cout<<"注册类型:1.学生\t2.教师"<<endl;
cin>>reader[countReader].readerState;
reader[countReader].name=id;
count=countReader;
Reader::countReader++;
state=1;
}
else if(flag == 2)
{
count=countLibrarian;
librarian[countLibrarian++].addLibrarian(id);
state=2;
}
cout<<"注册成功"<<endl;
system("pause");
}
int Log::registerIn(Log *l)
{
int flag = menu(2);
string x,y;
cout<<"请输入账号:";
cin>>x;
cout<<"请输入密码:";
cin>>y;
for(int i=0;i<countLog;i++)
if((x==l[i].id)&&(y==l[i].password)&&(flag==l[i].state))
{
if((l[i].state==1)&&(reader[l[i].count].authorityState==3))
{
cout<<"您已被禁止登陆!"<<endl;
system("pause");
return -1;
}
cout<<"登录成功"<<endl;
system("pause");
return i;
}
cout<<"登录失败"<<endl;
system("pause");
return -1;
}
Log log[10];
Reader reader[10];
Librarian librarian[10];
Book book[10];
int countBook=Book::countBook;
int menu();
int readerMenu(int t,string s);
int librarianMenu(string s);
void showBookLibrary();
void readFile();
void writeFile();
/*****************
/***main**********
******************/
int main()
{
readFile();
systemAdministrator king;
int flagMenu = menu();
system("cls");
for(;flagMenu!=0;)
{
switch(flagMenu)
{
case 1://注册
log[Log::countLog].logIn();
break;
case 2:
{
int flag = Log::registerIn(log);
if(flag == -1)
break;
if(log[flag].state == 1)//以读者身份登录
{
int readerFlag = log[flag].count;
system("cls");
int readerChoice = readerMenu(reader[readerFlag].readerState,reader[readerFlag].getName());
for(;readerChoice!=4;)
{
switch(readerChoice)
{
case 1://借书
{
Book B;
if(reader[readerFlag].authorityState==1)
{
cout<<"您已被禁止借书!"<<endl;
break;
}
int bookNum=B.searchBook(book);
if(bookNum == -1)
{
cout<<"未找到此书"<<endl;
break;
}
reader[readerFlag].borrowBook(bookNum,book);
time_t tempTime = time(NULL);
reader[readerFlag].setBorrowTime(&tempTime);
break;
}
case 2://还书
reader[readerFlag].returnBook(book);
break;
case 3://查看借阅信息
reader[readerFlag].display();
break;
}
system("pause");
system("cls");
readerChoice=readerMenu(reader[readerFlag].readerState,reader[readerFlag].getName());
}
}
else if(log[flag].state==2)//以图书管理员身份登录
{
system("cls");
Book B;
int librarianFlag = log[flag].count;
int choice=librarianMenu(librarian[librarianFlag].getName());
for(;choice!=5;)
{
switch(choice)
{
case 1://增添图书
book[Book::countBook].addBook();
break;
case 2://删除图书
librarian[librarianFlag].deleteBook(book);
break;
case 3://查询图书信息
showBookLibrary();
break;
case 4://修改图书信息
librarian[librarianFlag].alterBook(book[B.searchBook(book)]);
break;
}
system("pause");
system("cls");
choice=librarianMenu(librarian[librarianFlag].getName());
}
}
break;
}
case 3://系统管理员入口
{
string s;
cout<<"请输入管理者通行码:"<<endl;
cin>>s;
if(s!="000000")
{
cout<<"密码错误!"<<endl;
system("pause");
break;
}
cout<<"读者信息"<<endl;
int flag=king.printReader(reader);
if(flag!=0)
king.setAuthorityReader(reader);
system("pause");
break;
}
}
system("cls");
flagMenu = menu();
system("cls");
}
writeFile();
return 0;
}
int menu()
{
int i;
cout<<"1.注册"<<endl;
cout<<"2.登陆"<<endl;
cout<<"3.系统管理员入口(管理员密码000000)"<<endl;
cout<<"0.退出"<<endl;
cout<<"Your choice:";
cin>>i;
return i;
}
int readerMenu(int t,string s)
{
int flag;
if(t == 1)
cout<<"学生:";
else
cout<<"教师:";
cout<<s<<endl;
cout<<"1.借书"<<endl;
cout<<"2.还书"<<endl;
cout<<"3.查看借阅信息"<<endl;
cout<<"4.返回"<<endl;
cout<<"Your choice:";
cin>>flag;
return flag;
}
int librarianMenu(string s)
{
int flag;
cout<<"用户名:"<<s<<endl;
cout<<"1.增添图书"<<endl;
cout<<"2.删除图书"<<endl;
cout<<"3.查询图书信息"<<endl;
cout<<"4.修改图书信息"<<endl;
cout<<"5.返回"<<endl;
cout<<"Your choice:";
cin>>flag;
return flag;
}
void showBookLibrary()
{
Book B;
cout<<"**********Library**********"<<endl;
cout<<"书名\t"<<"编号\t"<<"作者\t"<<"出版社\t"<<"价格\t"<<"借阅状态"<<endl;
for(int i=0;i<countBook;i++)
book[i].displayBook();
}
istream &operator >>(istream &in,Book &b)
{
in>>b.num;
in>>b.title;
in>>b.author;
in>>b.pub;
in>>b.price;
in>>b.state;
return in;
}
ostream &operator <<(ostream &out,const Book &b)
{
out<<b.title<<" "<<b.num<<" "<<b.author<<" "<<b.pub<<" "<<b.price<<" "<<b.state<<endl;
return out;
}
void readFile()
{
int i;
ifstream fin("log.txt",ios::in);
if(fin!=NULL)
{
fin>>Log::countLog;
for(i=0;i<Log::countLog;i++)
fin>>log[i];
}
fin.close();
//reader.txt
fin.open("reader.txt",ios::in);
if(fin!=NULL)
{
fin>>Reader::countReader;
for(i=0;i<Reader::countReader;i++)
fin>>reader[i];
}
fin.close();
//librarian.txt
fin.open("librarian.txt",ios::in);
if(fin!=NULL)
{
fin>>Librarian::countLibrarian;
for(i=0;i<Librarian::countLibrarian;i++)
fin>>librarian[i];
}
fin.close();
//book.txt
fin.open("book.txt",ios::in);
if(fin!=NULL)
{
fin>>countBook;
for(i=0;i<countBook;i++)
fin>>book[i];
}
fin.close();
}
void writeFile()
{
int i;
ofstream fout;
//log.txt
fout.open("log.txt",ios::trunc);
fout<<Log::countLog<<endl;
for(i=0;i<Log::countLog;i++)
fout<<log[i];
fout.close();
//reader.txt
fout.open("reader.txt",ios::trunc);
fout<<Reader::countReader<<endl;
for(i=0;i<Reader::countReader;i++)
fout<<reader[i];
fout.close();
//librarian.txt
fout.open("librarian.txt",ios::trunc);
fout<<Librarian::countLibrarian<<endl;
for(i=0;i<Librarian::countLibrarian;i++)
fout<<librarian[i];
fout.close();
//book.txt
fout.open("book.txt",ios::trunc);
if(fout!=NULL)
{
fout<<countBook<<endl;
for(i=0;i<countBook;i++)
fout<<book[i];
}
fout.close();
cout<<"存档成功!"<<endl;
}
ostream &operator <<(ostream &out,Log &l)
{
out<<l.id<<" ";
out<<l.password<<" ";
out<<l.count<<" ";
out<<l.countLog<<" ";
out<<l.readerState<<" ";
out<<l.state<<endl;
return out;
}
istream &operator >>(istream &in,Log &l)
{
in>>l.id;
in>>l.password;
in>>l.count;
in>>l.countLog;
in>>l.readerState;
in>>l.state;
return in;
}
ostream &operator<<(ostream&out,Reader &r)
{
out<<r.name<<" "<<r.readerState<<endl;
out<<r.countBook<<endl;
for(int i=0;i<countBook;i++)
out<<r.book[i];
return out;
}
istream &operator>>(istream&in,Reader &r)
{
in>>r.name;
in>>r.readerState;
in>>r.countBook;
for(int i=0;i<countBook;i++)
in>>r.book[i];
return in;
}
ostream &operator<<(ostream&out,Librarian &r)
{
out<<r.name<<endl;
return out;
}
istream &operator>>(istream&in,Librarian &r)
{
in>>r.name;
return in;
}