如何用C++实现简单图书馆管理系统

如何用C++实现简单图书馆管理系统

这篇文章主要介绍“如何用C++实现简单图书馆管理系统”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“如何用C++实现简单图书馆管理系统”文章能帮助大家解决问题。

功能如下:

如何用C++实现简单图书馆管理系统

1,添加书籍
2,删除书籍(可删除还没外借的书籍)
3,读者借书
4,读者还书
5,按书籍登入号查看信息(每一本书的书籍登入号唯一,如有5本相同书名作者的书,那就有5个不同的书籍登入号)
6,查询所有图书信息(可以直接查看到同本书在图书馆中的剩余和借出情况)
7,查看指定读者的借书详情
8,注册新读者
9,查看所有书籍信息(可以详细到每个登入号和此登入号书籍的借阅情况:如谁借的,借阅日期)

采用客户端和服务器模式,服务器端接收客户端,一旦接收到便启用线程,然后相应来自此客户端的不同请求

server端

serverHead.h

#ifndefSERVERHEAD#defineSERVERHEAD#include<iostream>#include<string>#include<cstdlib>#include<list>#include<iterator>#include<fstream>#include<cstring>#include<thread>#include<WINSOCK2.H>#include<mysql.h>#pragmacomment(lib,"ws2_32.lib")structinfoData{intflag;//各种命令intborFlag;//借阅情况1表示借出,0表示未出借charenterName[20];//书籍登入号,一本份charbookName[50];//书籍名charbookNum[20];//书编号charbookWriter[50];//书作者intremainBook;//最初设置的库存charreaderName[50];//借书者charreaderNum[50];//借书证charborrowTime[30];//借书时间intremain;//库中还剩此书数量intlend;//从库中借出的此书数量};classBook_mysql{public:Book_mysql();boolconnectAndCreate();//链接创建数据库和表boolselectWhetherEmpty();//检测是否为空表booltestTheBookWhetherExist(constinfoData&Data);//判断书籍存在boolinsertTo(infoData&Data);//插入书籍intdeleteFrom(infoData&Data);//删除相应书籍boolreaderBorrowBook(infoData&Data);//读者借书intreaderReturnBook(infoData&Data);//还书boolwhetherSurpass(infoData&Data);//判断是否超过借书上限(上限一本书最多借两本)boolselectBookByEntername(infoData&Data);//通过书籍登入号查询书籍voiddisplayInformation(infoData&Data,constSOCKET&sClient);//打印(笼统)voidshowTheReaderBook(infoData&Data,SOCKET&sClient);//打印指定读者借书情况voidDISPLAY(infoData&Data,SOCKET&sClient);//打印(详细)private:charuser[30];//登陆名字charpswd[10];//密码charhost[20];//表示本地数据库unsignedintport;//端口MYSQLmyCont;MYSQL_RES*result;MYSQL_ROWsql_row;intres;};classReader_mysql{public:Reader_mysql();boolconnectAndCreate();booltestTheReaderWhetherExist(infoData&Data);//检测该读者是否已经存在voidinsertTo(infoData&Data);//注册boolreaderBorrowBook();//借书boolreaderReturnBook();//还书private:charuser[30];//登陆名字charpswd[10];//密码charhost[20];//表示本地数据库unsignedintport;MYSQLmyCont;MYSQL_RES*result;MYSQL_ROWsql_row;intres;};classserverSocket{public:serverSocket(intport);~serverSocket();SOCKETserverAccpetSocket();//阻塞acceptprivate:WSADATAwsd;SOCKETsServer;//客户端套接字用来监听std::list<SOCKET>listScli;//客户端套接字用链表来接收,接送一个放一个SOCKETtemp;//用来存放中间值SOCKADDR_INaddrServ;//服务器地址};voidaddBook(infoData&Data,constSOCKET&sClient,Book_mysql&book_mysql);voiddeleteBook(infoData&Data,constSOCKET&sClient,Book_mysql&book_mysql);voidborrowBook(infoData&Data,constSOCKET&sClient,Book_mysql&book_mysql,Reader_mysql&reader_mysql);voidreturnBook(infoData&Data,SOCKET&sClient,Book_mysql&book_mysql,Reader_mysql&reader_mysql);voidsearchBook(infoData&Data,constSOCKET&sClient,Book_mysql&book_mysql);voiddisplayAllBook(infoData&Data,SOCKET&sClient,Book_mysql&book_mysql);voidDISPLAY(infoData&Data,SOCKET&sClient,Book_mysql&book_mysql);voidshowTheReaderBook(infoData&Data,SOCKET&sClient,Book_mysql&book_mysql,Reader_mysql&reader_mysql);voidsetNewReader(infoData&Data,constSOCKET&sClient,Reader_mysql&reader_mysql);voidclientSocketThreadFunction(SOCKETsClient);#endif

套接字类的实现

serverSocket.cpp

#include"serverHead.h"serverSocket::serverSocket(intport){//初始化套结字动态库if(WSAStartup(MAKEWORD(2,2),&wsd)!=0){std::cout<<"WSAStartupfailed!"<<std::endl;return;}//开始创建服务端socket//创建套接字AF_INET:ipv4SOCK_STREAM:使用tcpsServer=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);if(INVALID_SOCKET==sServer){std::cout<<"socketfailed!"<<std::endl;return;}//服务器套接字地址addrServ.sin_family=AF_INET;//IPv4addrServ.sin_port=htons(port);//设置端口建议大于1024addrServ.sin_addr.s_addr=INADDR_ANY;//表示接受任何客户端的请求//绑定套接字绑定服务端socket和端口intret=bind(sServer,(LPSOCKADDR)&addrServ,sizeof(SOCKADDR_IN));if(SOCKET_ERROR==ret){std::cout<<"bindfailed!"<<std::endl;return;}//开始监听ret=listen(sServer,10);if(SOCKET_ERROR==ret){std::cout<<"listenfailed!"<<std::endl;return;}}serverSocket::~serverSocket(){closesocket(sServer);//关闭套接字WSACleanup();//释放套接字资源;}SOCKETserverSocket::serverAccpetSocket(){//接受客户端请求sockaddr_inaddrClient;intaddrClientlen=sizeof(addrClient);temp=accept(sServer,(sockaddrFAR*)&addrClient,&addrClientlen);if(INVALID_SOCKET==temp){std::cout<<"acceptfailed!"<<std::endl;return-1;}listScli.push_front(temp);returntemp;}

MySQL中书籍表的实现

bookMysql.cpp

#include"serverHead.h"Book_mysql::Book_mysql(){strcpy_s(user,"root");strcpy_s(pswd,"123456");strcpy_s(host,"localhost");port=3306;mysql_init(&myCont);if(mysql_real_connect(&myCont,host,user,pswd,"mysql",port,NULL,0))//先链接自带的数据库,以便后面创建专属数据库{res=mysql_query(&myCont,"createdatabaseifnotexistszhanghsun");mysql_query(&myCont,"SETNAMESGBK");if(res){std::cout<<"创建库失败"<<std::endl;system("pause");exit(-1);;}res=mysql_query(&myCont,"usezhanghsun");if(res){std::cout<<"use失败"<<std::endl;system("pause");exit(-1);}charorder[1024];sprintf_s(order,"createtableIFNOTEXISTSm_book(书籍登入号char(10)notnull,书名char(20)defaultnull,书编号char(20)defaultnull,书作者char(50)defaultnull,借阅intdefault0,读者名char(20)default'%s',读者借书号char(20)default'%s',借阅时间char(20)default'%s')","无","无","无");res=mysql_query(&myCont,order);if(res){std::cout<<"创建表失败"<<std::endl;system("pause");exit(-1);}}else{std::cout<<"链接失败"<<std::endl;system("pause");exit(-1);}}//判断指定书籍是否存在boolBook_mysql::testTheBookWhetherExist(constinfoData&Data){charorder[1024];//检测是否存在该书sprintf_s(order,"SELECT*FROMm_bookWHERE书名='%s'and书作者='%s'",Data.bookName,Data.bookWriter);res=mysql_query(&myCont,order);if(!res){result=mysql_store_result(&myCont);if(result){if(sql_row=mysql_fetch_row(result))//如果查询不为空{return1;//已有该书}}}else{std::cout<<"查询失败"<<std::endl;system("pause");exit(-1);}return0;//没有该书}//插入书籍操作boolBook_mysql::insertTo(infoData&Data){charorder[1024];sprintf_s(order,"insertintom_book(书籍登入号,书名,书编号,书作者)values('%s','%s','%s','%s')",Data.enterName,Data.bookName,Data.bookNum,Data.bookWriter);res=mysql_query(&myCont,order);if(res){std::cout<<"插入失败"<<std::endl;system("pause");exit(-1);}return1;}//判断表是否为空boolBook_mysql::selectWhetherEmpty(){charorder[1024];sprintf_s(order,"SELECT*FROMm_book");res=mysql_query(&myCont,order);if(!res){result=mysql_store_result(&myCont);if(result){if(sql_row=mysql_fetch_row(result))//获取具体的数据{return0;//表不为空}}}else{std::cout<<"查询failed"<<std::endl;system("pause");exit(-1);}return1;//表为空}//判断该读者要借书籍是否超出上限boolBook_mysql::whetherSurpass(infoData&Data){charorder[1024];intnum=0;sprintf_s(order,"select读者名,读者借书号fromm_bookwhere书名='%s'and书作者='%s'",Data.bookName,Data.bookWriter);res=mysql_query(&myCont,order);if(!res){result=mysql_store_result(&myCont);if(result){while(sql_row=mysql_fetch_row(result))//获取具体的数据{if(strcmp(Data.readerName,sql_row[0])==0&&strcmp(Data.readerNum,sql_row[1])==0){++num;}if(num>=2){return1;//已达上限}}}}else{std::cout<<"查询failed"<<std::endl;system("pause");exit(-1);}return0;//没超}//删除书籍intBook_mysql::deleteFrom(infoData&Data){charorder[1024];intflag=0;sprintf_s(order,"SELECT*FROMm_bookWHERE书名='%s'AND书作者='%s'AND借阅='%d'",Data.bookName,Data.bookWriter,0);res=mysql_query(&myCont,order);if(!res){result=mysql_store_result(&myCont);if(result){while(sql_row=mysql_fetch_row(result))//获取具体的数据{flag=1;std::cout<<"删除登入号:"<<sql_row[0]<<std::endl;//根据得到的登入号一一删除sprintf_s(order,"DELETEFROMm_bookWHERE书籍登入号='%s'",sql_row[0]);res=mysql_query(&myCont,order);if(res){std::cout<<"删除failed"<<std::endl;system("pause");exit(-1);}}if(flag==0){return-1;//全借出}elseif(flag==1){return1;//删除了相关书籍}}}else{std::cout<<"查询failed"<<std::endl;system("pause");exit(-1);}}//读者借阅书籍boolBook_mysql::readerBorrowBook(infoData&Data){charorder[1024];sprintf_s(order,"SELECT书籍登入号FROMm_bookWHERE书名='%s'AND书作者='%s'AND借阅='%d'",Data.bookName,Data.bookWriter,0);res=mysql_query(&myCont,order);if(!res){result=mysql_store_result(&myCont);if(result){if(sql_row=mysql_fetch_row(result))//获取具体的数据{std::cout<<"借的书的登入号:"<<sql_row[0]<<std::endl;sprintf_s(order,"UPDATEm_bookSET借阅='%d',读者名='%s',读者借书号='%s',借阅时间=NOW()WHERE书籍登入号='%s'",1,Data.readerName,Data.readerNum,sql_row[0]);res=mysql_query(&myCont,order);if(res){std::cout<<"更新failed"<<std::endl;system("pause");exit(-1);}}else{return0;//书全借光}}}else{std::cout<<"查询failed"<<std::endl;system("pause");exit(-1);}return1;//借书成功}//还书intBook_mysql::readerReturnBook(infoData&Data){charorder[1024];sprintf_s(order,"SELECT*FROMm_bookWHERE书籍登入号='%s'",Data.enterName);res=mysql_query(&myCont,order);if(!res){result=mysql_store_result(&myCont);if(result){if(sql_row=mysql_fetch_row(result))//获取具体的数据{std::cout<<"登入号:"<<sql_row[0]<<std::endl;sprintf_s(order,"UPDATEm_bookSET借阅=0,读者名='%s',读者借书号='%s',借阅时间='%s'WHERE书籍登入号='%s'","无","无","无",Data.enterName);res=mysql_query(&myCont,order);if(res){std::cout<<"更新failed"<<std::endl;system("pause");exit(-1);}}else{return0;//没找到该登入号}}}else{std::cout<<"查询failed"<<std::endl;system("pause");exit(-1);}return1;//成功}//更具书籍登入号查询书籍信息boolBook_mysql::selectBookByEntername(infoData&Data){charorder[1024];intflag=0;sprintf_s(order,"SELECT*FROMm_bookWHERE书籍登入号='%s'",Data.enterName);res=mysql_query(&myCont,order);if(!res){result=mysql_store_result(&myCont);if(result){if(sql_row=mysql_fetch_row(result))//获取具体的数据{flag=1;strcpy_s(Data.enterName,sql_row[0]);strcpy_s(Data.bookName,sql_row[1]);strcpy_s(Data.bookNum,sql_row[2]);strcpy_s(Data.bookWriter,sql_row[3]);if(atoi(sql_row[4])==1){Data.borFlag=1;strcpy_s(Data.readerName,sql_row[5]);strcpy_s(Data.readerNum,sql_row[6]);strcpy_s(Data.borrowTime,sql_row[7]);}else{Data.borFlag=0;}}if(flag==0){return0;//登入号没找到}}}else{std::cout<<"查询failed"<<std::endl;system("pause");exit(-1);}return1;}//笼统打印voidBook_mysql::displayInformation(infoData&Data,constSOCKET&sClient){charorder[1024];chartempName[30];chartempWriter[30];intlend=0;intremain=0;sprintf_s(order,"SELECT书名,书编号,书作者FROMm_book");res=mysql_query(&myCont,order);if(!res){result=mysql_store_result(&myCont);if(result){while(sql_row=mysql_fetch_row(result))//获取具体的数据{strcpy_s(Data.bookName,sql_row[0]);strcpy_s(Data.bookNum,sql_row[1]);strcpy_s(Data.bookWriter,sql_row[2]);if(strcmp(Data.bookName,tempName)==0&&strcmp(Data.bookWriter,tempWriter)==0)//相同书籍不予计算{continue;}lend=0;remain=0;char_order[1024];//已借书情况sprintf_s(_order,"SELECTcount(*)FROMm_bookwhere书名='%s'and书作者='%s'and借阅=1",Data.bookName,Data.bookWriter);res=mysql_query(&myCont,_order);if(!res){MYSQL_RES*_result=mysql_store_result(&myCont);if(_result){MYSQL_ROW_sql_row;while(_sql_row=mysql_fetch_row(_result))//获取具体的数据{lend=atoi(_sql_row[0]);}}}else{std::cout<<"查询failed"<<std::endl;system("pause");exit(-1);}//剩余没借情况sprintf_s(_order,"SELECTcount(*)FROMm_bookwhere书名='%s'and书作者='%s'and借阅=0",Data.bookName,Data.bookWriter);res=mysql_query(&myCont,_order);if(!res){MYSQL_RES*_result=mysql_store_result(&myCont);if(_result){MYSQL_ROW_sql_row;while(_sql_row=mysql_fetch_row(_result))//获取具体的数据{remain=atoi(_sql_row[0]);}}}else{std::cout<<"查询failed"<<std::endl;system("pause");exit(-1);}Data.lend=lend;Data.remain=remain;if(strcmp(Data.bookName,tempName)!=0&&strcmp(Data.bookWriter,tempWriter)!=0){Data.flag=0;send(sClient,(char*)&Data,sizeof(Data),0);}strcpy_s(tempName,Data.bookName);strcpy_s(tempWriter,Data.bookWriter);}}}else{std::cout<<"查询failed"<<std::endl;system("pause");exit(-1);}Data.flag=1;send(sClient,(char*)&Data,sizeof(Data),0);}//打印指定读者借书情况voidBook_mysql::showTheReaderBook(infoData&Data,SOCKET&sClient){charorder[1024];intflag=0;sprintf_s(order,"select书籍登入号,书名,书编号,书作者,借阅时间fromm_bookwhere读者名='%s'and读者借书号='%s';",Data.readerName,Data.readerNum);res=mysql_query(&myCont,order);if(!res){result=mysql_store_result(&myCont);if(result){while(sql_row=mysql_fetch_row(result))//如果查询不为空{flag=1;strcpy_s(Data.enterName,sql_row[0]);strcpy_s(Data.bookName,sql_row[1]);strcpy_s(Data.bookNum,sql_row[2]);strcpy_s(Data.bookWriter,sql_row[3]);strcpy_s(Data.borrowTime,sql_row[4]);Data.flag=6;send(sClient,(char*)&Data,sizeof(Data),0);}}}else{std::cout<<"查询失败"<<std::endl;system("pause");exit(-1);}if(flag==0){Data.flag=0;//没有借书send(sClient,(char*)&Data,sizeof(Data),0);}Data.flag=-1;send(sClient,(char*)&Data,sizeof(Data),0);}//打印(详细)voidBook_mysql::DISPLAY(infoData&Data,SOCKET&sClient){charorder[1024];intflag=0;inti=0;sprintf_s(order,"select*fromm_book");res=mysql_query(&myCont,order);if(!res){result=mysql_store_result(&myCont);if(result){while(sql_row=mysql_fetch_row(result))//如果查询不为空{flag=1;strcpy_s(Data.enterName,sql_row[0]);strcpy_s(Data.bookName,sql_row[1]);strcpy_s(Data.bookNum,sql_row[2]);strcpy_s(Data.bookWriter,sql_row[3]);if(atoi(sql_row[4])==1){Data.borFlag=1;strcpy_s(Data.readerName,sql_row[5]);strcpy_s(Data.readerNum,sql_row[6]);strcpy_s(Data.borrowTime,sql_row[7]);}else{Data.borFlag=0;}Data.flag=6;send(sClient,(char*)&Data,sizeof(Data),0);}}if(flag==0)//没有结果{Data.flag=-2;send(sClient,(char*)&Data,sizeof(Data),0);}}Data.flag=-1;//打印退出flagsend(sClient,(char*)&Data,sizeof(Data),0);}

MySQL读者表的实现

readerMysql.cpp

#include"serverHead.h"Reader_mysql::Reader_mysql(){strcpy_s(user,"root");strcpy_s(pswd,"123456");strcpy_s(host,"localhost");port=3306;mysql_init(&myCont);if(mysql_real_connect(&myCont,host,user,pswd,"mysql",port,NULL,0)){res=mysql_query(&myCont,"createdatabaseifnotexistszhanghsun");mysql_query(&myCont,"SETNAMESGBK");if(res){std::cout<<"创建库失败"<<std::endl;system("pause");exit(-1);}res=mysql_query(&myCont,"usezhanghsun");if(res){std::cout<<"use失败"<<std::endl;system("pause");exit(-1);}charorder[1024];sprintf_s(order,"createtableIFNOTEXISTSm_reader(读者名char(20)defaultnull,读者借书号char(20)defaultnull)");res=mysql_query(&myCont,order);if(res){std::cout<<"创建表failed"<<std::endl;system("pause");exit(-1);}}else{std::cout<<"链接失败"<<std::endl;system("pause");exit(-1);}}//判断是否重复注册boolReader_mysql::testTheReaderWhetherExist(infoData&Data){charorder[1024];sprintf_s(order,"select*fromm_readerwhere读者名='%s'and读者借书号='%s'",Data.readerName,Data.readerNum);res=mysql_query(&myCont,order);if(!res){result=mysql_store_result(&myCont);if(result){if(sql_row=mysql_fetch_row(result))//如果查询不为空{return1;//有该读者}}}else{std::cout<<"查询失败"<<std::endl;system("pause");exit(-1);}return0;//没有此人}//插入新读者voidReader_mysql::insertTo(infoData&Data){charorder[102];sprintf_s(order,"insertintom_reader(读者名,读者借书号)values('%s','%s')",Data.readerName,Data.readerNum);res=mysql_query(&myCont,order);if(res){std::cout<<"插入失败"<<std::endl;system("pause");exit(-1);}//else注册成功}

服务器端总实现

server.cpp

#include"serverHead.h"//增加书籍voidaddBook(infoData&Data,constSOCKET&sClient,Book_mysql&book_mysql){std::cout<<"客户端"<<sClient<<"正在录入书籍!"<<std::endl;if(book_mysql.testTheBookWhetherExist(Data))//数据库中判断该书是否已经存在{std::cout<<"客户端"<<sClient<<"添加书籍重复已退回"<<std::endl;Data.flag=-1;send(sClient,(char*)&Data,sizeof(Data),0);return;}//按库存随机每本书的登入号并插入srand((unsigned)time(NULL));//通过时间来随机登入号for(inti=0;i<Data.remainBook;i++){intx=rand()%1000;_itoa_s(x,Data.enterName,10);book_mysql.insertTo(Data);//在数据库中插入}Data.flag=0;send(sClient,(char*)&Data,sizeof(Data),0);std::cout<<"客户端"<<sClient<<"录入书籍成功!"<<std::endl;}//删除书籍voiddeleteBook(infoData&Data,constSOCKET&sClient,Book_mysql&book_mysql){std::cout<<"客户端"<<sClient<<"正在删除书籍"<<std::endl;intflag=0;if(!(book_mysql.testTheBookWhetherExist(Data)))//首先判断该书是否存在{std::cout<<"没有该书"<<std::endl;Data.flag=-1;send(sClient,(char*)&Data,sizeof(Data),0);return;}//删除switch(book_mysql.deleteFrom(Data)){case-1:Data.flag=-2;send(sClient,(char*)&Data,sizeof(Data),0);std::cout<<"该书全部出借,无法进行删除"<<std::endl;break;case1:Data.flag=0;send(sClient,(char*)&Data,sizeof(Data),0);std::cout<<"已删除此书的所有未出借书籍"<<std::endl;break;}}//借阅书籍voidborrowBook(infoData&Data,constSOCKET&sClient,Book_mysql&book_mysql,Reader_mysql&reader_mysql){std::cout<<"客户端"<<sClient<<"正在借阅书籍"<<std::endl;//判断该读者是否存在switch(reader_mysql.testTheReaderWhetherExist(Data)){case0:Data.flag=-1;send(sClient,(char*)&Data,sizeof(Data),0);return;//没有找到该读者,直接returncase1:Data.flag=0;send(sClient,(char*)&Data,sizeof(Data),0);break;//找到该读者,继续操作}recv(sClient,(char*)&Data,sizeof(Data),0);if(book_mysql.selectWhetherEmpty()){std::cout<<"未录入任何书籍"<<std::endl;Data.flag=-1;send(sClient,(char*)&Data,sizeof(Data),0);return;}if(!(book_mysql.testTheBookWhetherExist(Data))){Data.flag=-2;std::cout<<"没有这本书"<<std::endl;send(sClient,(char*)&Data,sizeof(Data),0);return;}//判断是否多借if(!(book_mysql.whetherSurpass(Data))){switch(book_mysql.readerBorrowBook(Data)){case1:Data.flag=0;break;//借书成功case0:Data.flag=-3;break;//书全借光}}else{Data.flag=-4;//借此书已达上限}send(sClient,(char*)&Data,sizeof(Data),0);}//归还书籍voidreturnBook(infoData&Data,SOCKET&sClient,Book_mysql&book_mysql,Reader_mysql&reader_mysql){std::cout<<"客户端"<<sClient<<"正在归还书籍"<<std::endl;showTheReaderBook(Data,sClient,book_mysql,reader_mysql);recv(sClient,(char*)&Data,sizeof(Data),0);switch(book_mysql.readerReturnBook(Data)){case0:std::cout<<"没有找到该登入号"<<std::endl;Data.flag=-1;send(sClient,(char*)&Data,sizeof(Data),0);break;case1:std::cout<<"还书成功"<<std::endl;Data.flag=0;send(sClient,(char*)&Data,sizeof(Data),0);break;}}//通过登入号搜索书籍voidsearchBook(infoData&Data,constSOCKET&sClient,Book_mysql&book_mysql){std::cout<<"客户端"<<sClient<<"正在通过登入号搜索书籍"<<std::endl;switch(book_mysql.selectBookByEntername(Data)){case1:send(sClient,(char*)&Data,sizeof(Data),0);return;case0:Data.flag=3;send(sClient,(char*)&Data,sizeof(Data),0);return;}}//打印书籍(笼统打印,不打印登入号)voiddisplayAllBook(infoData&Data,SOCKET&sClient,Book_mysql&book_mysql){std::cout<<"客户端"<<sClient<<"正在打印书籍(概括)"<<std::endl;if(book_mysql.selectWhetherEmpty()){std::cout<<"还没有录入书籍"<<std::endl;Data.flag=-1;send(sClient,(char*)&Data,sizeof(Data),0);return;}book_mysql.displayInformation(Data,sClient);}//打印每一本书籍的信息(打印每一本书的详细信息)voidDISPLAY(infoData&Data,SOCKET&sClient,Book_mysql&book_mysql){std::cout<<"客户端"<<sClient<<"正在打印全部书籍信息"<<std::endl;book_mysql.DISPLAY(Data,sClient);}//打印指定读者的借书情况voidshowTheReaderBook(infoData&Data,SOCKET&sClient,Book_mysql&book_mysql,Reader_mysql&reader_mysql){std::cout<<"客户端"<<sClient<<"正在打印指定读者的借书情况"<<std::endl;switch(reader_mysql.testTheReaderWhetherExist(Data)){case0:Data.flag=-1;send(sClient,(char*)&Data,sizeof(Data),0);return;//没有找到该读者,直接returncase1:Data.flag=0;send(sClient,(char*)&Data,sizeof(Data),0);break;//找到该读者,继续操作}book_mysql.showTheReaderBook(Data,sClient);}//注册新的读者voidsetNewReader(infoData&Data,constSOCKET&sClient,Reader_mysql&reader_mysql){std::cout<<"客户端"<<sClient<<"正在注册新读者"<<std::endl;switch(reader_mysql.testTheReaderWhetherExist(Data)){case1:Data.flag=-1;break;//已有该读者case0:Data.flag=1;reader_mysql.insertTo(Data);break;//注册成功}send(sClient,(char*)&Data,sizeof(Data),0);}//线程voidclientSocketThreadFunction(SOCKETsClient){Book_mysqlbook_mysql;Reader_mysqlreader_mysql;//对客户端的数据进行解析while(true){infoDataData;intret=recv(sClient,(char*)&Data,sizeof(Data),0);if(SOCKET_ERROR==ret){std::cout<<sClient<<"可能下线"<<std::endl;return;}intflag=0;switch(Data.flag)//错误:控制传输跳过的实例化,方法,加{}{//增加书籍case1:addBook(Data,sClient,book_mysql);break;//删除书籍case2:deleteBook(Data,sClient,book_mysql);break;//借书case3:borrowBook(Data,sClient,book_mysql,reader_mysql);break;//还书case4:returnBook(Data,sClient,book_mysql,reader_mysql);break;//搜索借书者case5:searchBook(Data,sClient,book_mysql);break;//浏览所有书籍信息case6:displayAllBook(Data,sClient,book_mysql);break;//查看登入读者的借书情况case7:showTheReaderBook(Data,sClient,book_mysql,reader_mysql);break;//注册case8:setNewReader(Data,sClient,reader_mysql);break;//退出flagcase9:flag=1;break;//所有书被借情况一览表case-10:DISPLAY(Data,sClient,book_mysql);break;default:std::cout<<"输入错误请重新输入:"<<std::endl;}if(flag==1){std::cout<<"客户端"<<sClient<<"下线"<<std::endl;break;}}}intmain(intargc,char*argv[]){serverSocketmySocket(8888);while(1){SOCKETsClient=mySocket.serverAccpetSocket();if(sClient==-1){continue;}else{std::cout<<"接收到一个客户端:"<<sClient<<std::endl;}std::threadt1(clientSocketThreadFunction,sClient);//启用线程t1.detach();}return0;}

client

头文件

clientHead.h

#ifndefCLIENTHEAH#defineCLIENTHEAH#include<WINSOCK2.H>#include<iostream>#include<string>#include<thread>usingnamespacestd;#pragmacomment(lib,"ws2_32.lib")structinfoData{intflag;//各种命令intborflag;//借阅情况1表示借出,0表示未出借charenter_name[20];//书籍登入号,一本份charbook_name[50];//书籍名charbook_num[20];//书编号charbook_writer[50];//书作者intremain_book;//最初设置的库存charreader_name[50];//借书者charreader_num[50];//借书证charborrow_time[30];//借书时间intremain;//库中还剩此书数量intlend;//从库中借出的此书数量};classclientSocket{public:clientSocket(stringipAdress,shortport);~clientSocket();boolconnectToServer();voidreceiveData(infoData&data);voidsendData(infoData&data);private:WSADATAwsd;//WSADATA变量SOCKETsClient;//客户端套接字SOCKADDR_INservAddr;//服务器地址intret;//返回值};voidmenu();voidtapAnyKeyNext();intaddBook(infoData&Data,clientSocket&mySocket);intdisplayAllbook(infoDataData,clientSocket&mySocket);intdeleteBook(infoData&Data,clientSocket&mySocket);intborrowBook(infoData&Data,clientSocket&mySocket);intreturnBook(infoData&Data,clientSocket&mySocket);intsearchBook(infoData&Data,clientSocket&mySocket);intshowTheReaderBook(infoData&Data,clientSocket&mySocket);intsetNewReader(infoData&Data,clientSocket&mySocket);voidDISPLAY(infoData&Data,clientSocket&mySocket);#endif

套接字类的实现

mySocket.cpp

#include"clientHead.h"clientSocket::clientSocket(stringipAdress,shortport){//初始化套结字动态库if(WSAStartup(MAKEWORD(2,2),&wsd)!=0){cout<<"WSAStartupfailed!"<<endl;return;}//创建套接字sClient=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);if(INVALID_SOCKET==sClient){cout<<"socketfailed!"<<endl;return;}servAddr.sin_family=AF_INET;//如果编译通不过属性c++常规sdl改成否servAddr.sin_addr.s_addr=inet_addr(ipAdress.c_str());//设置服务端地址这里表示本机servAddr.sin_port=htons(port);intnServAddlen=sizeof(servAddr);}boolclientSocket::connectToServer(){//连接服务器ret=connect(sClient,(LPSOCKADDR)&servAddr,sizeof(servAddr));if(SOCKET_ERROR==ret){cout<<"connectfailed!"<<endl;system("pause");returnfalse;}//成功建立连接可以开始通信了returntrue;}voidclientSocket::sendData(infoData&Data){//向服务器发送数据ret=send(sClient,(char*)&Data,sizeof(Data),0);if(SOCKET_ERROR==ret){cout<<"sendfailed!"<<endl;return;}}voidclientSocket::receiveData(infoData&Data){//接收服务器端的数据recv(sClient,(char*)&Data,sizeof(Data),0);/*if(ret<0){cout<<"recvfailed"<<endl;return;}*/}clientSocket::~clientSocket(){closesocket(sClient);//关闭套接字WSACleanup();//释放套接字资源}

客户端总实现

client

#include"clientHead.h"voidmenu(){cout<<"*****************************"<<endl;cout<<endl;cout<<"1.录入书籍"<<endl;cout<<"2.删除书籍"<<endl;cout<<"3.借书"<<endl;cout<<"4.还书"<<endl;cout<<"5.按书籍登入号查看信息"<<endl;cout<<"6.查询所有图书信息"<<endl;cout<<"7.查看你的借书详情"<<endl;cout<<"8.注册"<<endl;cout<<"9.退出"<<endl;cout<<"-10.查看所有书籍信息"<<endl;cout<<endl;cout<<"*****************************"<<endl;}//增加书籍intaddBook(infoData&Data,clientSocket&mySocket){Data.flag=1;cout<<"开始录入书籍!"<<endl;cout<<"输入书名:";cin>>Data.book_name;cout<<"输入书编号:";cin>>Data.book_num;cout<<"输入书作者:";cin>>Data.book_writer;cout<<"输入书库存:";cin>>Data.remain_book;mySocket.sendData(Data);mySocket.receiveData(Data);if(Data.flag==-1){cout<<"已有此书,请勿重复添加"<<endl;}if(Data.flag==0){cout<<"添加成功"<<endl;}cout<<"是否继续增加书籍操作?1:0";while(1){intx;cin>>x;switch(x){case0:return0;//退出case1:return1;//继续default:cout<<"输入错误请重新输入:";}}}//删除书籍intdeleteBook(infoData&Data,clientSocket&mySocket){if(displayAllbook(Data,mySocket)){tapAnyKeyNext();return0;}cout<<"已经外借的那本无法删除"<<endl;cout<<"输入书名:";cin>>Data.book_name;cout<<"输入作者:";cin>>Data.book_writer;Data.flag=2;mySocket.sendData(Data);mySocket.receiveData(Data);if(Data.flag==-1){cout<<"没有该书"<<endl;}elseif(Data.flag==-2){cout<<"该书全部出借,无法进行删除"<<endl;}elseif(Data.flag==0){cout<<"删除成功"<<endl;}cout<<"是否继续删除书籍操作?1:0";while(1){intx;cin>>x;switch(x){case0:return0;//退出case1:return1;//继续default:cout<<"输入错误请重新输入:";}}}//借书intborrowBook(infoData&Data,clientSocket&mySocket){Data.flag=3;cout<<"输入你的姓名:"<<endl;cin>>Data.reader_name;cout<<"输入你的借书号:"<<endl;cin>>Data.reader_num;mySocket.sendData(Data);mySocket.receiveData(Data);if(Data.flag==-1){cout<<"没有找到该读者,是否重新输入?1:2"<<endl;charch[10];cin>>ch;intx=atoi(ch);switch(x){case1:return1;break;case2:return0;break;}}elseif(Data.flag==0){cout<<"登入成功"<<endl;}cout<<"输入书籍名:";cin>>Data.book_name;cout<<"输入书作者:";cin>>Data.book_writer;mySocket.sendData(Data);mySocket.receiveData(Data);if(Data.flag==0){cout<<"借书成功"<<endl;}elseif(Data.flag==-1){cout<<"未录入任何书籍"<<endl;}elseif(Data.flag==-2){cout<<"没有这本书"<<endl;}elseif(Data.flag==-3){cout<<"该书已借光"<<endl;}elseif(Data.flag==-4){cout<<"借此书此书已达上限"<<endl;}cout<<"是否继续借书操作?1:0";while(true){while(1){intx;cin>>x;switch(x){case0:return0;//退出case1:return1;//继续default:cout<<"输入错误请重新输入:";}}}}//还书intreturnBook(infoData&Data,clientSocket&mySocket){Data.flag=4;cout<<"输入你的姓名:"<<endl;cin>>Data.reader_name;cout<<"输入你的借书号:"<<endl;cin>>Data.reader_num;mySocket.sendData(Data);mySocket.receiveData(Data);if(Data.flag==-1){cout<<"没有找到该读者,是否重新输入?1:2"<<endl;charch[10];cin>>ch;intx=atoi(ch);switch(x){case1:return1;break;case2:return0;break;}}elseif(Data.flag==0){cout<<"登入成功"<<endl;}cout<<"您所借书如下:"<<endl;cout<<endl;mySocket.receiveData(Data);if(Data.flag==0){cout<<endl;std::cout<<"你没有借书"<<std::endl;cout<<endl;return1;}while(Data.flag==6){cout<<"登入号:"<<Data.enter_name<<endl;cout<<"书籍名:"<<Data.book_name<<endl;cout<<"书籍号:"<<Data.book_num<<endl;cout<<"作者名:"<<Data.book_writer<<endl;cout<<"借阅时间:"<<Data.borrow_time<<endl;cout<<endl;mySocket.receiveData(Data);}cout<<"输入登入号还书:"<<endl;cin>>Data.enter_name;mySocket.sendData(Data);mySocket.receiveData(Data);if(Data.flag==-1){cout<<"没有查询到该登入号"<<endl;}elseif(Data.flag==0){cout<<"还书成功"<<endl;}cout<<"是否继续还书操作?1:0";while(true){while(1){intx;cin>>x;switch(x){case0:return0;//退出case1:return1;//继续default:cout<<"输入错误请重新输入:";}}}}//打印(笼统)intdisplayAllbook(infoDataData,clientSocket&mySocket){Data.flag=6;mySocket.sendData(Data);cout<<endl;while(true){mySocket.receiveData(Data);if(Data.flag==-1){cout<<"没有录入书籍"<<endl;return-1;}elseif(Data.flag==1){break;}elseif(Data.flag==0){cout<<"书籍名:"<<Data.book_name<<endl;cout<<"书籍号:"<<Data.book_num<<endl;cout<<"作者名:"<<Data.book_writer<<endl;cout<<"还剩余:"<<Data.remain<<"本"<<endl;cout<<"已借出:"<<Data.lend<<"本"<<endl;cout<<endl;}}return0;}//通过书籍登入号查找书籍信息intsearchBook(infoData&Data,clientSocket&mySocket){Data.flag=5;cout<<"请输入书籍登入号"<<endl;cin>>Data.enter_name;mySocket.sendData(Data);mySocket.receiveData(Data);if(Data.flag==3){cout<<"没有这个登入号"<<endl;tapAnyKeyNext();return0;}cout<<endl;cout<<"查询结果如下"<<endl;cout<<endl;cout<<"登入号:"<<Data.enter_name<<endl;cout<<"书籍名:"<<Data.book_name<<endl;cout<<"书籍号:"<<Data.book_num<<endl;cout<<"作者名:"<<Data.book_writer<<endl;if(Data.borflag==1){cout<<"**该书已被借**"<<endl;cout<<"借书者:"<<Data.reader_name<<endl;cout<<"借书证:"<<Data.reader_num<<endl;}elseif(Data.borflag==0){cout<<"**该书没被借**"<<endl;}cout<<endl;cout<<"查询完成"<<endl;cout<<"是否继续查询操作?1:0";while(true){while(1){intx;cin>>x;switch(x){case0:return0;//退出case1:return1;//继续default:cout<<"输入错误请重新输入:";}}}}//查看指定读者借书情况intshowTheReaderBook(infoData&Data,clientSocket&mySocket){Data.flag=7;cout<<"输入你的姓名:"<<endl;cin>>Data.reader_name;cout<<"输入你的借书号:"<<endl;cin>>Data.reader_num;mySocket.sendData(Data);mySocket.receiveData(Data);if(Data.flag==-1){cout<<"没有找到该读者,是否重新输入?1:2"<<endl;charch[10];cin>>ch;intx=atoi(ch);switch(x){case1:return1;break;case2:return0;break;}}elseif(Data.flag==0){cout<<"登入成功"<<endl;}mySocket.receiveData(Data);if(Data.flag==0){cout<<"你还没借书"<<endl;}else{cout<<endl;cout<<"查询结果如下"<<endl;cout<<endl;while(Data.flag==6){cout<<"登入号:"<<Data.enter_name<<endl;cout<<"书籍名:"<<Data.book_name<<endl;cout<<"书籍号:"<<Data.book_num<<endl;cout<<"作者名:"<<Data.book_writer<<endl;cout<<"借书时间:"<<Data.borrow_time<<endl;cout<<endl;mySocket.receiveData(Data);}}cout<<"以上是您所有的借书情况"<<endl;cout<<"是否继续此查询操作?1:0";while(true){while(1){intx;cin>>x;switch(x){case0:return0;//退出case1:return1;//继续default:cout<<"输入错误请重新输入:";}}}}//创建新读者intsetNewReader(infoData&Data,clientSocket&mySocket){Data.flag=8;cout<<"输入你的姓名:"<<endl;cin>>Data.reader_name;cout<<"输入你的借书号:"<<endl;cin>>Data.reader_num;mySocket.sendData(Data);mySocket.receiveData(Data);if(Data.flag==1){cout<<"注册成功"<<endl;tapAnyKeyNext();return0;}elseif(Data.flag==-1){cout<<"名字借书号重复,是否重新输入?1:2"<<endl;charch[10];cin>>ch;intx=atoi(ch);switch(x){case1:return1;break;case2:return0;break;}}}//遍历每一本书的详情信息voidDISPLAY(infoData&Data,clientSocket&mySocket){Data.flag=-10;inti=0;mySocket.sendData(Data);cout<<endl;while(true){mySocket.receiveData(Data);if(Data.flag==-2){cout<<"没有录入书籍"<<endl;tapAnyKeyNext();return;}if(Data.flag==-1){break;}/*std::cout<<"i="<<i<<std::endl;++i;*/cout<<"登入号:"<<Data.enter_name<<endl;cout<<"书籍名:"<<Data.book_name<<endl;cout<<"书籍号:"<<Data.book_num<<endl;cout<<"作者名:"<<Data.book_writer<<endl;if(Data.borflag==1){cout<<"**该书已被借**"<<endl;cout<<"借书者:"<<Data.reader_name<<endl;cout<<"借书证:"<<Data.reader_num<<endl;cout<<"借阅时间:"<<Data.borrow_time<<endl;}elseif(Data.borflag==0){cout<<"**该书没被借**"<<endl;}cout<<endl;}cout<<"查询完成"<<endl;cout<<endl;tapAnyKeyNext();}//按任意键退出voidtapAnyKeyNext(){while(1){cout<<"按任意键返回主菜单"<<endl;stringx;cin>>x;return;}}intmain(){clientSocketmySocket("127.0.0.1",8888);if(!mySocket.connectToServer()){return0;}//向服务端发送数据while(true){menu();infoDataData;charch[20];cout<<"输入操作:";cin>>ch;intx=atoi(ch);intflag=0;switch(x){case1:{while(1){if(addBook(Data,mySocket)==0){break;}}}break;case2:{while(1){if(deleteBook(Data,mySocket)==0){break;}}}break;case3:{while(1){if(borrowBook(Data,mySocket)==0){break;}}}break;case4:{while(1){if(returnBook(Data,mySocket)==0){break;}}}break;case5:{while(1){if(searchBook(Data,mySocket)==0){break;}}}break;case6:displayAllbook(Data,mySocket);tapAnyKeyNext();break;case7:{while(1){if(showTheReaderBook(Data,mySocket)==0){break;}}}break;case8:{while(1){if(setNewReader(Data,mySocket)==0){break;}}}break;case9:flag=1;break;case-10:DISPLAY(Data,mySocket);break;default:cout<<"输入错误请重新输入:"<<endl;}if(flag==1){Data.flag=9;mySocket.sendData(Data);break;}}WSACleanup();return0;}

平台:vs2013
MySQL5.7

关于“如何用C++实现简单图书馆管理系统”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注恰卡编程网行业资讯频道,小编每天都会为大家更新不同的知识点。

发布于 2022-03-13 23:40:56
收藏
分享
海报
0 条评论
41
上一篇:vue跳转页面常用的方法有哪些 下一篇:html和css怎么实现图片滚动切换效果
目录

    0 条评论

    本站已关闭游客评论,请登录或者注册后再评论吧~

    忘记密码?

    图形验证码