C++课程设计简单图书管理系统.docx
- 文档编号:27456572
- 上传时间:2023-07-01
- 格式:DOCX
- 页数:23
- 大小:275.56KB
C++课程设计简单图书管理系统.docx
《C++课程设计简单图书管理系统.docx》由会员分享,可在线阅读,更多相关《C++课程设计简单图书管理系统.docx(23页珍藏版)》请在冰豆网上搜索。
C++课程设计简单图书管理系统
课程设计
课程名称C++课程设计
题目名称图书管理系统
2016年6月29日
目录
一.设计内容与要求3
二.设计思路:
3
三.关键模块实现及主要代码3
四.实验结果及分析3
五、心得体会3
六、参考文献3
一.设计内容与要求
(1)、设计一个图书管理系统,能够实现用户信息管理,图书信息的录入、查询、删除等功能。
(2)、实现用户管理,系统具有用户注册功能。
管理员的账号和密码由系统指定,登录后可进行修改。
一般用户注册后才能登录使用系统。
管理员具有录入、查询、删除图书信息及一般用户的功能。
一般用户只有图书查询功能。
(3)、登录功能,只有注册了的用户才能进入系统,没有注册的则有提示信息出现。
(4)、图书管理功能,管理员能够录入、查询、删除图书信息。
(5)、图书查询功能,管理员及一般用户能够通过图书的基本信息,如图书作者、书名、出版社、出版日期等等,进行查询。
提示:
上述为基本功能,可参考实际的系统进行扩展。
二.设计思路:
为了实现用户信息以及图书信息的管理,建立一个用户CUser类与图书信息CBoodata类,其中CUser类应该包含用户账号以及密码,CBookdata类应该包含图书作者,书名,出版社,出版日期。
同时应该建立两个dat文件,一个为存储用户信息,一个用来储存图书信息。
程序主要分为4个页面:
(1)登录页面,由Cdialog类派生出CMyprojectDlg类,用来处理账号注册与用户登录。
(2)图书查询页面。
由Cdialog类派生出CSearch类。
包含读取所有图书数据响应函数,以及根据作者姓名或者书名来查询图书响应函数。
(3)管理员图书管理页面。
由Cdialog类派生出CBookdata类。
包含读取所有图书响应函数,添加图书响应函数,修改图书响应函数,删除图书响应函数。
(4)用户信息管理页面,由Cdialog类派生出CUserdata类。
包含读取所有用户信息响应函数,删除用户响应函数、添加用户响应函数、修改用户信息响应函数。
因为本人能力有限,而且自己是根据老师给的教程以及结合书本内容来完成课程设计,于是我第一想到的就是应数组来存储信息,而不是用链表,所以,对于文件数据的删除与修改,难免会过于乏力。
三.关键模块实现及主要代码
(1)自己建立的类
1.用户类:
classCUser
{
public:
intnum;
charpassword[20];
};
2.图书信息类:
classBook//图书类
{
public:
charauthor[10];//设置作者姓名
charbname[20];//设置图书书名
charpub_name[20];//设置出版社名称
intpub_date;//设置出版年月
};
(2)登录页面,由Cdialog类派生出CMyprojectDlg类,用来处理账号注册与用户登录。
1.用户注册处理响应函数:
voidCMyprojectDlg:
:
OnButton3()//用户注册
{
//TODO:
Addyourcontrolnotificationhandlercodehere
//////////////////////////////////////////////////////////////////////////////////////
//以下代码主要还是把这条记录写入文件,注意写入的格式:
文件的前四个字节是保持用户记录数,
//后面依次保持用户记录注意,写用户的每个信息的顺序要与读取时的顺序一致。
UpdateData(TRUE);
CUseruser;
intn_user=0;//用户的个数,管理员默认为第一个。
自己输入为账号:
1111,密码:
0000。
intsize=0;
fstreamfile("user.dat",ios:
:
in|ios:
:
out);//建立一个文件类,关于其用法,请baidu。
file.seekg(ios:
:
beg);//将文件指针指向文件头。
file.read((char*)&n_user,sizeof(n_user));//读取用户数。
n_user+=1;
file.seekp(ios:
:
beg);
file.write((char*)&n_user,sizeof(n_user));//写学生记录个数
user.num=m_num;
//之前定义user.password为string,导致密码匹配时有错误。
。
。
。
。
memcpy(user.password,m_password.GetBuffer(m_password.GetLength()),m_password.GetLength());//把类CString对象的内容赋值到字符数组中
user.password[m_password.GetLength()]='\0';//在数组末尾加结束符号
//在文件末尾,把新的记录写入。
file.seekp(4+(n_user-1)*sizeof(CUser),ios:
:
beg);//这里出错
file.write((char*)&user.num,sizeof(user.num));
file.write((char*)&user.password,sizeof(user.password));
file.close();
/////////////////////////////////////////////////////////////////////////////////////////
//用消息框提示
CStringstr;
str.Format("成功注册,成为第%d个注册用户,请按确定返回重新登录.",n_user-1);///因为第一个为管理员,不算为用户人数。
AfxMessageBox(str);
}
2.用户登录处理响应函数。
其中包含判断是管理员,还是普通用户。
voidCMyprojectDlg:
:
OnButton2()//用户登录
{
//TODO:
Addyourcontrolnotificationhandlercodehere
UpdateData(TRUE);
CUserm_user[100];
inti;
intsize;
intn_user=0;//文件中用户的个数
//////////////////////////////////////////////////////////////////////////////////////////
//读取文件中的用户记录,注意读取顺序与写入的顺序要一致,文件最开始的4个字节是表示记录个数。
//根据记录个数,依次读取用户记录放在成员变量m_st数组中(已在类体中声明了)
CFilefile;
file.Open("user.dat",CFile:
:
modeReadWrite);
size=GetFileSize((void*)file.m_hFile,NULL);//读取文件大小,size==0表示是空文件
if(size>0)
{
file.SeekToBegin();
file.Read(&n_user,sizeof(n_user));
}
if(n_user>0&&n_user<99)//实验中限定为0~100个用户记录
{
for(i=0;i { //请注意与写入数据的顺序要保持一样,否则会读错数据。 file.Read(&(m_user[i].num),sizeof(m_user[i].num)); file.Read(&(m_user[i].password),sizeof(m_user[i].password)); } } file.Close(); ///////////////////////////////////////////////// if(m_num==1111&&m_password=="0000")//管理员进入管理员页面 { CAdministratorsadministrators; administrators.DoModal(); } CStringstr_password; for(i=0;i { str_password=m_user[i].password; if(m_user[i].num==m_num) { if(str_password==m_password) { CSearchsearch; search.DoModal(); } else { CStringstr; str.Format("密码或者账号错误,请重新输入! "); AfxMessageBox(str); } } } } (3)图书查询页面。 包含读取所有图书数据,以及根据作者姓名或者书名来查询图书。 1.读取所有图书响应函数。 voidCSearch: : OnButton5()//显示出所有图书 { //TODO: Addyourcontrolnotificationhandlercodehere inti; intsize; intn_book=0;//文件book的本数 ////////////////////////////////////////////////////////////////////////////////////////// //读取文件中的书本记录,注意读取顺序与写入的顺序要一致,文件最开始的4个字节是表示记录个数。 //根据记录个数,依次读取书籍记录放在成员变量m_user_book数组中(已在类体中声明了) CFilefile; file.Open("book.dat",CFile: : modeReadWrite); size=GetFileSize((void*)file.m_hFile,NULL);//读取文件大小,size==0表示是空文件 if(size>0) { file.SeekToBegin(); file.Read(&n_book,sizeof(n_book)); } if(n_book>0&&n_book<99)//实验中限定为0~100个书本记录 { m_num_book=n_book; for(i=0;i { //请注意与写入数据的顺序要保持一样,否则会读错数据。 file.Read(&(m_user_book[i].author),sizeof(m_user_book[i].author)); file.Read(&(m_user_book[i].bname),sizeof(m_user_book[i].bname)); file.Read(&(m_user_book[i].pub_name),sizeof(m_user_book[i].pub_name)); file.Read(&(m_user_book[i].pub_date),sizeof(m_user_book[i].pub_date)); } } file.Close(); ///////////////////////////////////////////////// //在列表控件中显示所有条记录 //插入每列的表头; m_list.InsertColumn(0,"作者",LVCFMT_LEFT,100,1); m_list.InsertColumn(1,"书名",LVCFMT_LEFT,100,1); m_list.InsertColumn(2,"出版社",LVCFMT_LEFT,100,1); m_list.InsertColumn(3,"出版日期",LVCFMT_LEFT,100,1); //每一行插入一条书本记录 for(i=0;i { CStringstr; str=m_user_book[i].author; m_list.InsertItem(i,str);//InsertItem向CListCtr控件申请一行用来存放数据,同时写入一行的头名称(即编号为0列的数据) str=m_user_book[i].bname; m_list.SetItemText(i,1,str);//设置第i行第1列的的数据为书名 str=m_user_book[i].pub_name; m_list.SetItemText(i,2,str);//设置第i行第2列的的数据为出版社 str.Format("%d",m_user_book[i].pub_date);///整型变为字符串,便于在控件显示 m_list.SetItemText(i,3,str);//设置第i行第3列的的数据为出版日期 } } 2.图书查询响应函数。 voidCSearch: : OnButton4()//查询图书 { //TODO: Addyourcontrolnotificationhandlercodehere UpdateData(TRUE); m_list.DeleteAllItems(); intnum=0;//表示查找结果的个数 inti; for(i=0;i m_num_book是类的数据成员,相当于类中的全局变量,所以这里也可以用。 { CStringstr; intret1=0,ret2=0; str=m_user_book[i].author; ret1=str.Find(m_author,0);//ret1>=0,表示str中包含了m_author的字段 str=m_user_book[i].bname; ret2=str.Find(m_bname,0);//ret2>=0,表示str中包含了m_bname的字段 if(ret1>=0)//按作者查询 { str=m_user_book[i].author; m_list.InsertItem(num,str);//InsertItem向CListCtr控件申请一行用来存放数据,同时写入一行的头名称(即编号为0列的数据) str=m_user_book[i].bname; m_list.SetItemText(num,1,str);//设置第i行第1列的的数据为书名 str=m_user_book[i].pub_name; m_list.SetItemText(num,2,str);//设置第i行第2列的的数据为出版社 str.Format("%d",m_user_book[i].pub_date);///整型变为字符串,便于在控件显示 m_list.SetItemText(num,3,str);//设置第i行第3列的的数据为出版日期 num+=1;//结果+1; } } } (4)管理员图书管理页面。 包含读取所有图书,添加图书,修改图书,删除图书的功能。 1.读取所有图书功能实现与(3)的实现一样。 2.添加图书响应函数。 voidCBookdata: : OnButton7()//添加图书 { UpdateData(TRUE); Bookbook; intn_book=0;//书本的个数 intsize=0; fstreamfile("book.dat",ios: : in|ios: : out);//建立一个文件类,关于其用法,请baidu。 file.seekg(ios: : beg);//将文件指针指向文件头。 file.read((char*)&n_book,sizeof(n_book));//读取书籍数。 ////////////////////////////////////////////////////////////////////////////////////// //以下代码主要还是把这条记录写入文件,注意写入的格式: 文件的前四个字节是保持书本记录数, //后面依次保持书本记录注意,写学生的每个信息的顺序要与读取时的顺序一致。 n_book+=1; file.seekp(ios: : beg); file.write((char*)&n_book,sizeof(n_book));//写学生记录个数 memcpy(book.author,m_add_author.GetBuffer(m_add_author.GetLength()),m_add_author.GetLength());//把类CString对象的内容赋值到字符数组中 book.author[m_add_author.GetLength()]='\0';//在数组末尾加结束符号 memcpy(book.bname,m_add_bname.GetBuffer(m_add_bname.GetLength()),m_add_bname.GetLength());//把类CString对象的内容赋值到字符数组中 book.bname[m_add_bname.GetLength()]='\0';//在数组末尾加结束符号 memcpy(book.pub_name,m_add_pub_name.GetBuffer(m_add_pub_name.GetLength()),m_add_pub_name.GetLength());//把类CString对象的内容赋值到字符数组中 book.pub_name[m_add_pub_name.GetLength()]='\0';//在数组末尾加结束符号 book.pub_date=m_add_pub_date; //在文件末尾,把新的记录写入。 file.seekp(4+(n_book-1)*54,ios: : beg); file.write((char*)&book.author,sizeof(book.author)); file.write((char*)&book.bname,sizeof(book.bname)); file.write((char*)&book.pub_name,sizeof(book.pub_name)); file.write((char*)&book.pub_date,sizeof(book.pub_date)); file.close(); ///////////////////////////////////////////////////////////////////////////////////////// //用消息框提示 CStringstr; str.Format("文件中已有%d本书籍记录.",n_book); AfxMessageBox(str); } 3.修改图书响应函数。 voidCBookdata: : OnButton5()//修改图书,,,必须先读取图书才能修改图书。 { //TODO: Addyourcontrolnotificationhandlercodehere UpdateData(TRUE); m_list.DeleteAllItems(); m_list.InsertColumn(0,"作者",LVCFMT_LEFT,100,1); m_list.InsertColumn(1,"书名",LVCFMT_LEFT,100,1); m_list.InsertColumn(2,"出版社",LVCFMT_LEFT,100,1); m_list.InsertColumn(3,"出版日期",LVCFMT_LEFT,100,1); CStringstr_author,str_bname,str_pub_name,str;//将book类的字符串数组赋值为字符串变量,方便比较。 CFilefile;//建立一个文件类,关于其用法,请baidu。 file.Open("book.dat",CFile: : modeReadWrite);//打开文件 file.SeekToBegin(); file.Write(&m_num_book,sizeof(m_num_book));//写书本记录本数 for(inti=0;i { str_author=m_book[i].author; str_bname=m_book[i].bname; str_pub_name=m_book[i].pub_name; if(str_author==m_author||str_bname==m_bname||str_pub_name==m_pub_name||m_book[i].pub_date==m_pub_date) { memcpy(m_book[i].author,m_modify_author.GetBuffer(m_modify_author.GetLength()),m_modify_author.GetLength());//把类CString对象的内容赋值到字符数组中 m_book[i].author[m_modify_author.GetLength()]='\0';//在数组末尾加结束符号 memcpy(m_book[i].bname,m_modify_bname.GetBuffer(m_modify_bname.GetLength()),m_modify_bname.GetLength());//把类CString对象的内容赋值到字符数组中 m_book[i].bname[m_modify_bname.GetLength()]='\0';//在数组末尾加结束符号 memcpy(m_book[i].pub_name,m_modify_pub_name.GetBuffer(m_modify_pub_name.GetLength()),m_modify_pub_name.GetLength());//把类CString对象的内容赋值到字符数组中 m_book[i].pub_name[m_modify_pub_name.GetLength()]='\0';//在数组末尾加结束符号 m_book[i].pub_date=m_modify_pub_date;
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C+ 课程设计 简单 图书 管理 系统