您好,欢迎访问三七文档
当前位置:首页 > IT计算机/网络 > 数据库 > 用mysql数据库实现的C++图书管理系统
#includefstream#includeiostream#includestdlib.h#includectime#includecmath#includetermios.h#includesstream#includestring.h#includeassert.h#includemysql/mysql.h//改变字体颜色#defineNONE\033[m#defineRED\033[0;32;31m#defineGREEN\033[0;32;32m#defineBLUE\033[0;32;34m#defineYELLOW\033[1;33m#defineLIGHT_RED\033[1;31m#defineLIGHT_GREEN\033[1;32m#defineLIGHT_BLUE\033[1;34m/*在编译程序之前,请先开启mysql服务器(命令为sudomysqld_safe&),然后再登录mysql客户端(命令为mysql-uroot-p)建立数据库stu;建立数据表reader,book;具体操作语句如下:createdatabasestu;createtablereader(stu_namevarchar(20),stu_phonevarchar(15),stu_passwordvarchar(10),stu_numint,debtfloat,lend_timedouble,back_timedouble,countint);createtablebook(book_namevarchar(40),book_autvarchar(40),book_prevarchar(40),book_numint,book_muxint,book_conint);编译时用如下命令:g++$(mysql_config--cflags)110.cpp-ot$(mysql_config--libs)*///定义mysql数据库变量MYSQLmysql;MYSQL_RES*results;MYSQL_FIELD*fileds;MYSQL_ROWrows;charstrHost[]=localhost;charstrUser[]=root;charstrPasswd[]=3335599;charstrDb[]=stu;charstrSQL[200];unsignedintnum_fields;/*程序导读:1.程序中对书的操作,可通过书名,编号来进行,flag=0按书名来操作,flag=1按书编号来操作2.程序中对用户的操作,也可通过姓名,用户ID号或编号两种方式来进行,flag=0按姓名来操作,flag=1按用户ID号或编号来操作3.本程序分5个部分,具体已标识如(1)图书管理...4.本程序即可实现将数据保存至本地即stu.txt,book.txt,又可将数据保存至mysql数据库,只需稍加修改,具体如何修改,在此就不做说明*/usingnamespacestd;classBook{public:stringbook_name;stringbook_num;//编号stringbook_pre;//出版社stringbook_aut;//作者intbook_con;//这样的书还有几本intbook_mux;//这样的书总共有几本public:Book(){book_con=0;book_mux=0;}voidshow_book();};/****************************************(1)图书管理*******************************************/voidBook::show_book(){cout书名:book_nameendl;cout出版社:book_preendl;cout此书的作者:book_autendl;coutISBN编号:book_numendl;cout此书共有book_mux本endl;cout还有book_con本书未借出!endl;}classBookNode{public:Bookbook;BookNode*next;};BookNode*headbook=NULL;voidsavebook(BookNode*p);//保存图书信息到数据库voiddel_sql_book(BookNode*p);//将图书信息从数据库中删除classBookManage{public:inttotolbook;public:BookManage(){totolbook=0;}voidaddbook();voiddelbook(strings,intnum,intflag);voidfindbook(strings,intflag);//查询图书};voidBookManage::addbook(){stringh;cout添加输入0,退出输入-1endl;cinh;if(h==-1)return;elseif(h==0)while(1){if(h==-1)break;elseif(h==0){stringna,nu,p1,aut;intcon;BookNode*p=newBookNode;cout请输入书名:endl;cinna;p-book.book_name=na;cout请输入ISBN编号:endl;cinnu;p-book.book_num=nu;cout请输入出版社:endl;cinp1;p-book.book_pre=p1;cout请输入此书的作者:endl;cinaut;p-book.book_aut=aut;cout请输入此书共有几本:endl;cincon;p-book.book_con=con;p-book.book_mux=con;p-next=headbook;headbook=p;savebook(p);//添加至数据库BookManage::totolbook+=con;cout继续添加输入0,取消添加输入-1endl;cinh;}elsecoutYELLOW输入字符无效!NONEendl;}elsecoutYELLOW输入字符无效!NONEendl;}voidBookManage::delbook(strings,intnum,intflag){BookNode*p=headbook;if(headbook!=NULL){switch(flag){case0:if(headbook-book.book_name==s){if(headbook-book.book_con1){headbook-book.book_con-=num;headbook-book.book_mux-=num;}else{headbook=p-next;totolbook-=num;del_sql_book(p);//从数据库中删除//deletep;}}elseif(p-next){for(p=headbook;p-next!=NULL&&p!=NULL;p=p-next){if(p-next-book.book_name==s){if(p-next-book.book_con1){p-next-book.book_con-=num;headbook-book.book_mux-=num;break;}else{p-next=p-next-next;totolbook-=num;del_sql_book(p-next);//从数据库中删除//deletep-next;break;}}}if(p-next==NULL)coutYELLOW此书不存在!NONEendl;}break;case1:if(headbook-book.book_num==s){if(headbook-book.book_con1){headbook-book.book_con-=num;headbook-book.book_mux-=num;}else{headbook=p-next;totolbook-=num;del_sql_book(p);//从数据库中删除//deletep;}}elseif(p-next){for(p=headbook;p-next!=NULL&&p!=NULL;p=p-next){if(p-next-book.book_num==s){if(p-next-book.book_con1){p-next-book.book_con-=num;headbook-book.book_mux-=num;break;}else{p-next=p-next-next;totolbook-=num;del_sql_book(p-next);//从数据库中删除//deletep-next;break;}}}if(p-next==NULL)coutYELLOW此书不存在!NONEendl;}break;default:coutYELLOW输入数字无效!NONEendl;break;}}}voidBookManage::findbook(strings,intflag){BookNode*p;inth=0;switch(flag){case0:for(p=headbook;p!=NULL;p=p-next)//先查看是否有此书{if(p-book.book_name==s)break;}if(NULL==p)coutYELLOW此书不存在!NONEendl;for(p=headbook;p!=NULL;p=p-next)//查看名为s的图书共有几本{if(p-book.book_name==s)h++;}if(h0)coutGREEN这种名字的书共有h本NONEendl;for(p=headbook;p!=NULL;p=p-next)//查看图书,把所有名为s的图书的信息都打印出来{if(p-book.book_name==s){p-book.show_book();//显示出图书的基本信息if(p-book.book_con==0)coutYELLOW该书已全被借出!NONEendl;coutendl;}}break;case1:for(p=headbook;p!=NULL;p=p-next){if(p-book.book_num==s){p-book.show_book();if(p-book.book_con==0)coutYELLOW该书已全被借出!NONEendl;break;}}if(NULL==p)coutYELLOW此书不存在!NONEendl;break;default:coutYELLOW输入数字无效!NONEendl;break;}}/****************************************(2)用户管理*******************************************/classAdmin//管理员{public:stringadm_name;//帐号stringadm_passw;//密码public:Admin(){adm_name=adm;adm_passw=123;}};classLendBook{public:stringbookname[3];intcount;//借多少本数time_tlend_time;time_tback_time;LendBook(){count=0;}};classStu{public:stringstu_na
本文标题:用mysql数据库实现的C++图书管理系统
链接地址:https://www.777doc.com/doc-4454030 .html