用mysql数据库实现的C图书管理系统.docx
- 文档编号:2147296
- 上传时间:2023-05-02
- 格式:DOCX
- 页数:35
- 大小:23.70KB
用mysql数据库实现的C图书管理系统.docx
《用mysql数据库实现的C图书管理系统.docx》由会员分享,可在线阅读,更多相关《用mysql数据库实现的C图书管理系统.docx(35页珍藏版)》请在冰点文库上搜索。
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//改变字体颜色
#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<<"书名:
"< cout<<"出版社: "< cout<<"此书的作者: "< cout<<"ISBN编号: "< cout<<"此书共有"< cout<<"还有"< "< } 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,退出输入-1"< cin>>h; 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<<"请输入书名: "< cin>>na; p->book.book_name=na; cout<<"请输入ISBN编号: "< cin>>nu; p->book.book_num=nu; cout<<"请输入出版社: "< cin>>p1; p->book.book_pre=p1; cout<<"请输入此书的作者: "< cin>>aut; p->book.book_aut=aut; cout<<"请输入此书共有几本: "< cin>>con; p->book.book_con=con; p->book.book_mux=con; p->next=headbook; headbook=p; savebook(p);//添加至数据库 BookManage: : totolbook+=con; cout<<"继续添加输入0,取消添加输入-1"< cin>>h; } else cout< "NONE< } else cout< "NONE< } voidBookManage: : delbook(strings,intnum,intflag) { BookNode*p=headbook; if(headbook! =NULL) { switch(flag) { case0: if(headbook->book.book_name==s) { if(headbook->book.book_con>1) {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_con>1) { 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) cout< "NONE< } break; case1: if(headbook->book.book_num==s) { if(headbook->book.book_con>1) {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_con>1) { 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) cout< "NONE< } break; default: cout< "NONE< } } } 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) cout< "NONE< for(p=headbook;p! =NULL;p=p->next)//查看名为s的图书共有几本 { if(p->book.book_name==s) h++; } if(h>0) cout< 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) cout< "NONE< cout< } } 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) cout< "NONE< } } if(NULL==p) cout< "NONE< break; default: cout< "NONE< } } /**************************************** (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_name; stringstu_phone;//联系方式 stringstu_num;//学号(登录帐号) stringpassword;//密码 floatdebt;//欠费额 LendBooklendbook;//用户借书信息 public: voidshow_stu();//把此人所有信息(包括借书)显示出来 }; voidStu: : show_stu() { cout<<"姓名: "< cout<<"联系电话: "< cout<<"ID: "< cout<<"欠费额: "< cout<<"已借书"< } classStuNode { public: Stustu; StuNode*next; }; StuNode*headstu=NULL; voidsavestu(StuNode*p);//保存读者信息到数据库 voiddel_sql_stu(StuNode*p);//将读者信息从数据库中删除 classStuManage: publicBookManage { private: inttotolstu;//用户总数 public: StuManage(){totolstu=0;} voidaddstu();//增加用户 voiddelstu(strings,intflag);//删除用户 voidfindstu(strings,intflag);//查找用户 voidlendbook(stringss,strings,intflag);//借书 voidbackbook(stringss,strings,intflag);//还书 voidpaydebt(strings);//还款 voidmux_stu_book();//查询图书管总的图书量与总的用户量 }; voidStuManage: : addstu() { stringh; cout<<"添加输入0,退出输入-1"< cin>>h; if(h=="-1")return; elseif(h=="0") while (1) { if(h=="-1")break; elseif(h=="0") { stringna,num,p1,pa,pa1,pa2; StuNode*p=newStuNode; cout<<"请输入姓名: "< cin>>na; p->stu.stu_name=na; cout<<"请输入联系电话: "< cin>>p1; p->stu.stu_phone=p1; cout<<"请输入ID: "< cin>>num; p->stu.stu_num=num; p->stu.lendbook.lend_time=0; p->stu.lendbook.back_time=0; while (1) { cout<<"请输入六位密码: "< cin>>pa1; cout<<"请确认六位密码: "< cin>>pa2; if(pa1==pa2) { pa=pa2; p->stu.password=pa; break; } else cout< "NONE< } p->stu.debt=0; p->next=headstu; headstu=p; savestu(p);//保存至数据库 (StuManage: : totolstu)++;//用户总数加一 cout<<"继续添加输入0,取消添加输入-1"< cin>>h; } else cout< "NONE< } else cout< "NONE< } voidStuManage: : delstu(strings,intflag) { StuNode*p=headstu; if(headstu! =NULL) { switch(flag) { case0: if(headstu->stu.stu_name==s)//当要删除的用户位于链表的头结点位置 { headstu=p->next; StuManage: : totolstu--; del_sql_stu(p);//从数据库中删除 deletep; } elseif(p->next)//非头结点位置 { for(p=headstu;p->next! =NULL&&p! =NULL;p=p->next) { if(p->next->stu.stu_name==s) { p->next=p->next->next; totolstu--; del_sql_stu(p->next);//从数据库中删除 //deletep->next; break; } } if(p->next==NULL) cout< "NONE< } break; case1: if(headstu->stu.stu_name==s) { headstu=p->next; totolstu--; del_sql_stu(p);//从数据库中删除 deletep; } elseif(p->next) { for(p=headstu;p->next! =NULL&&p! =NULL;p=p->next) { if(p->next->stu.stu_name==s) { p->next=p->next->next; totolstu--; del_sql_stu(p->next);//从数据库中删除 //deletep->next; break; } } if(p->next==NULL) cout< "NONE< } break; default: cout< "NONE<
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- mysql 数据库 实现 图书 管理 系统