当前位置:   article > 正文

c++实现简易图书管理系统(链表+STL基础运用)_c++和链表实现的图书管理系统

c++和链表实现的图书管理系统

目录

一、图书馆功能总览

        图书功能

        用户管理

 二、结构解析

三、功能实现

光标移动

登录与注册

界面绘制

界面的切换

信息载入与储存

用户信息(链表储存)

信息储存

信息载入

书籍信息(链表储存)

书籍信息储存

书籍信息载入 

借阅信息(vector储存)

借阅信息储存

 借阅信息载入

图书管理

全部图书(图书列表)

查找图书

以书名搜索

 以ISBN搜索

以出版社搜索

以作者搜索

删除图书

以书名删除

以ISBN删除

增加图书

书籍借还(借还记录用vector记录)

借书

还书

个人信息

修改密码

借阅记录

退出登录

用户管理(需要管理员权限)

用户列表

增加用户

删除用户

重置指定用户密码

排行榜

热门图书

借书次数

热门图书

四、源代码资源


一、图书馆功能总览

        图书功能

  1. 读取txt内的图书信息,并整理成可翻页,可跳转的列表,并且输入当前页对应书籍的编号可以查看该书籍的详细信息。
  2. 可根据书名和isbn删除图书,可增加新图书
  3. 可以根据书名和isbn搜索图书,可通过作者或出版社模糊搜索图书
  4. 可以借阅图书,归还图书
  5. 有排行榜系统,包含热门图书排行榜(以用户借阅数量排行),借书次数排行榜,最新图书排行榜

        用户管理

  1. 有账户与密码,需注册。
  2. 用户有身份标识,有管理员、普通用户和游客,管理员有全部权限,普通用户不能增删图书和用户,游客除了不能增删图书和用户,还不能借阅图书
  3. 管理员可以通过用户名增删用户,可以查看所有用户(用户列表)。
  4. 管理员可以重置指定用户密码。
  5. 用户可以修改密码
  6. 用户可以查看自己个人的借阅记录
  7. 可以退出登录

 二、结构解析

        这个项目用多文件写成,分为

  1. head.h是放整个项目头文件包含,宏定义声明,类的声明与定义,各类函数的声明
  2. main.cpp,是项目运行的主线
  3. Register&SignUp.cpp是放登录与注册相关函数的定义
  4. FileIO.cpp是放有关程序与储存交互的函数定义
  5. BookManage.cpp是放实现图书管理功能的函数定义
  6. Page.cpp是放有关页面的函数定义
  7. BorrowandReturn.cpp是放有关借阅与还书方面功能的函数定义

  8. Rankings.cpp是放有关排行榜方面的函数定义
  9. UserManage.cpp是放有关用户管理方面的函数定义

三、功能实现


光标移动

  1. void SetCCPos(int x, int y)
  2. {
  3. HANDLE hOut;
  4. hOut = GetStdHandle(STD_OUTPUT_HANDLE);//获取标注输出句柄
  5. COORD pos;
  6. pos.X = x;pos.Y = y;
  7. SetConsoleCursorPosition(hOut, pos);//偏移光标位置
  8. }

 光标从(0,0)开始偏移,只要输入对应的坐标即可,且每次光标都是从(0,0)开始,两次偏移之间不会相互影响。

登录与注册

思路:while循环输入账户与密码,当检测到用户名是reg时跳转到注册页面,否则就是正常检测输入的账户与密码能否与已有用户链表中的用户对的上,对的上则登录成功,否则登录失败

head.h中,创建用户链表

  1. class User
  2. {
  3. public:
  4. void addBorrowBook(string& bo) //增加已借阅的图书
  5. {
  6. borrowbookname.push_back(bo);
  7. }
  8. User():name(""),key(""),borrownum(0),sumbooknum(0){} //初始化
  9. string name; //用户名
  10. string key; //密码
  11. short iden; //身份(0是管理员,1是普通用户,-1是游客)
  12. short borrownum; //现在借书量
  13. short sumbooknum; //总借书量
  14. vector <string> borrowbookname; //现借书的书名
  15. User *next;
  16. };
  17. struct UserList
  18. {
  19. User* head; //头节点
  20. User* tail; //尾节点
  21. int length; //节点数
  22. };

之后在main中 

  1. string name = "", //装用户输入的账户与密码
  2. key = "";
  3. User *user = nullptr; //指向登录成功后的用户
  4. UserList *u_list = new UserList; //创建一个管理用户链表对象
  5. u_list->head = u_list->tail = user;
  6. u_LordData(u_list); //载入已储存的用户链表
  7. User* current_user; //当前登录账号的信息
  8. while(1)
  9. {
  10. step1:
  11. system("cls");
  12. Page1(tips); //绘制登录页面
  13. SetCCPos(9,4);
  14. cin>>name;
  15. if(name == "reg") //进入注册界面
  16. {
  17. name = "unreg";
  18. User* tem = new User; //注册用的节点
  19. Page2(tips); //绘制注册界面
  20. SetCCPos(9,4);
  21. cin>>tem->name;
  22. SetCCPos(7,5);
  23. cin>>tem->key;
  24. SetCCPos(7,6);
  25. cin>>tem->iden;
  26. if(Register(tem,u_list)) //检查该用户名是否被占用
  27. {
  28. tips = "注册成功,请登录";
  29. u_SaveData(u_list);
  30. goto step1;
  31. }
  32. else
  33. {
  34. delete tem;
  35. tips = "用户名已被占用";
  36. goto step1;
  37. }
  38. }
  39. else
  40. {
  41. SetCCPos(7,5);
  42. cin>>key;
  43. }
  44. if(key == "0") //游客
  45. {
  46. current_user = new User;
  47. current_user->name = name;
  48. current_user->key = key;
  49. current_user->iden = -1;
  50. goto in;
  51. }
  52. if(SignUp(name,key,u_list)) //检查用户名与密码是否对应
  53. {
  54. current_user = LoacteUser(name,u_list);
  55. break;
  56. }
  57. else
  58. {
  59. tips = "登录失败";
  60. continue;
  61. }
  62. }
  63. in:

界面绘制

界面我分为4种

a,选择界面

b,功能页面

c,列表界面

d,其他

先易后难,先解释

  • 其他界面的绘制
  1. void PageTitle(void)
  2. {
  3. cout<<"╭-------------------■□■□■□--╮"<<endl;
  4. cout<<"│ 图书管理系统 │"<<endl;
  5. cout<<"╰--■□■□■□-------------------╯"<<endl;
  6. }
  7. void Page1(string &tips) //登录界面
  8. {
  9. PageTitle();
  10. cout<<"----------登录--------------"<<endl;
  11. cout<<"■用户名:"<<endl;
  12. cout<<"□密码:"<<"\t\t\t密码为0是游客"<<endl;
  13. cout<<tips<<endl;
  14. cout<<"(没有账号?)"<<endl;
  15. cout<<"(在用户名输入reg进行注册.)"<<endl;
  16. }
  17. void Page2(string &tips) //注册界面
  18. {
  19. system("cls");
  20. PageTitle();
  21. cout<<"----------注册--------------"<<endl;
  22. cout<<"■用户名:"<<endl;
  23. cout<<"□密码:"<<endl;
  24. cout<<"■身份:"<<endl;
  25. cout<<"(管理员-0 普通用户-1)"<<endl;
  26. cout<<tips;
  27. }

  • 选择界面、列表界面与功能界面的绘制

思路:先创建一个page类,一个里装着这个页面的所需要打印的信息和该页面的识别编号,当读到该编号时绘制该页面

  1. //head.h中
  2. class Page
  3. {
  4. public:
  5. Page(const std::string* b,int nu,int numbe,string tipsc)
  6. {
  7. n = nu;
  8. number = numbe;
  9. for(int i = 0;i < n;i++)
  10. {
  11. button[i] = b[i];
  12. }
  13. tips = tipsc;
  14. };
  15. Page(const std::string* b,int nu,int numbe):tips("\0")
  16. {
  17. n = nu;
  18. number = numbe;
  19. for(int i = 0;i < n;i++)
  20. {
  21. button[i] = b[i];
  22. }
  23. };
  24. Page(const std::string* b,int nu,int numbe,string na,string ident):tips("\0")
  25. {
  26. n = nu;
  27. number = numbe;
  28. button[0] = b[0]+na;
  29. button[1] = b[1]+ident;
  30. for(int i = 2;i < n;i++)
  31. {
  32. button[i] = b[i];
  33. }
  34. }
  35. int number; //编号
  36. void CoutDir(void); //输出指引
  37. void GetTips(void); //获取提示
  38. void CoutTips(void); //输出提示
  39. void PtintPage(void); //打印选择页面
  40. void PrintFunctionPage(UserList*&,Page*&,User*&,BookList*&,BorrowManage& bor); //打印功能页面
  41. void CoutFunctionDir(void); //打印功能页面指引
  42. void PrintFPage(void); //打印功能页面
  43. void ReflashSelect(int&); //刷新选择的页面
  44. private:
  45. string button[10]; //用来装标题指引
  46. string tips; //用来装提示
  47. int n; //指引个数
  48. };
  1. //Page.cpp中
  2. void Page::CoutDir(void) //输出指引
  3. {
  4. for(int i = 0;i < n-1;i++)
  5. {
  6. cout<<Page::button[i]<<endl;
  7. }
  8. SetCCPos(0,12);
  9. cout<<button[n-1];
  10. SetCCPos(5,12);
  11. }
  12. void Page::GetTips(void) //获取提示
  13. {
  14. cin>>Page::tips;
  15. }
  16. void Page::CoutTips(void) //输出提示
  17. {
  18. cout<<tips;
  19. }
  20. void Page::PtintPage(void) //打印该页面
  21. {
  22. system("cls");
  23. PageTitle();
  24. CoutDir();
  25. CoutTips();
  26. }
  27. void Page::CoutFunctionDir(void)
  28. {
  29. for(int i = 0;i < n;i++)
  30. {
  31. cout<<Page::button[i]<<endl;
  32. }
  33. }
  34. void Page::PrintFPage(void)
  35. {
  36. system("cls");
  37. PageTitle();
  38. CoutFunctionDir();
  39. CoutTips();
  40. }
  41. void Page::ReflashSelect(int& select)
  42. {
  43. select = number *10 +select;
  44. }

 用这种逻辑制作页面,需要先将全部页面信息按顺序录入,这就需要一个对象来装着这些页面

  1. //head.h中
  2. class PageManage
  3. {
  4. public:
  5. Page* arrow; //指向当前的Page
  6. vector <Page*> allpage; //全部页面
  7. void ChangePage(Page*&); //切换Page
  8. void addPage(Page* page) //增加页面
  9. {
  10. allpage.push_back(page);
  11. }
  12. };

之后在main中按一定顺序录入页面信息

  1. //载入页面
  2. PageManage pm; //页面管理对象
  3. //****************************零级页面
  4. //Page Star;
  5. string p0[8] = {"1-图书浏览","2-借还图书","3-个人信息","4-用户管理","5-作者信息","6-各排行榜","7-退出系统","编号:"};
  6. Page* page0 = new Page(p0,8,0);
  7. pm.addPage(page0);
  8. //****************************一级页面
  9. //Page BrowseBook;
  10. string p1[6] = {"1-全部图书","2-查找图书","3-删除图书","4-增加图书","0-上一页","编号:"};
  11. Page* page1 = new Page(p1,6,1);
  12. pm.addPage(page1);
  13. //Page Borrow;
  14. string p2[6] = {"1-借书","2-还书","0-上一页","编号:"};
  15. Page* page2 = new Page(p2,4,2);
  16. pm.addPage(page2);
  17. //Page PersonalInformation;
  18. string p3[7] = {"用户名:","身份:","1-修改密码","2-借阅记录","3-退出登录","0-上一页","编号:"};
  19. string t;
  20. if(0 == current_user->iden)
  21. {
  22. t = "管理员";
  23. }
  24. else if(1 == current_user->iden)
  25. {
  26. t = "普通用户";
  27. }
  28. else if(-1 == current_user->iden)
  29. {
  30. t = "游客";
  31. }
  32. Page* page3 = new Page(p3,7,3,current_user->name,t);
  33. pm.addPage(page3);
  34. //Page ManageUser
  35. if(0 == current_user->iden) //是管理员
  36. {
  37. string p4[6] = {"1-用户列表","2-增加用户","3-删除用户","4-重置指定用户密码","0-上一页","编号:"};
  38. Page* page4 = new Page(p4,6,4);
  39. pm.addPage(page4);
  40. }
  41. else //非管理员
  42. {
  43. string p4[3] = {"您没有使用该功能的权限!","0-上一页","编号:"};
  44. Page* page4 = new Page(p4,3,4);
  45. pm.addPage(page4);
  46. }
  47. //Page AuthorInformation
  48. string p5[4] = {"作者:梁某","QQ:2803234009","0-上一页","编号:"};
  49. Page* page5 = new Page(p5,4,5);
  50. pm.addPage(page5);
  51. string p6[4] = {"1-热门图书","2-借书狂人","3-最新图书","编号:"};
  52. Page* page6 = new Page(p6,4,6);
  53. pm.addPage(page6);
  54. //Page EXIXT
  55. string p7[3] = {"已保存数据","退出成功","\0"};
  56. Page* page7 = new Page(p7,3,7);
  57. pm.addPage(page7);
  58. //*************************二级页面
  59. //Page allbook //list func
  60. string p8[4] = {"全部图书","0-上一页","输入书籍id可查看详细信息","---------'+'下一页,‘-’上一页,‘*’跳转---------"};
  61. Page* page8 = new Page(p8,4,11);
  62. pm.addPage(page8);
  63. //Page searchbook
  64. string p9[7] = {"搜索图书","1-按书名搜索","2-按isbn搜索","3-按出版社搜索","4-按作者搜索","0-上一页","编号:"};
  65. Page* page9 = new Page(p9,7,12);
  66. pm.addPage(page9);
  67. //Page delbook
  68. if(0 == current_user->iden)
  69. {
  70. string p10[5] = {"删除图书","1-按书名删除","2-按isbn删除","0-上一页","编号:"};
  71. Page* page10 = new Page(p10,5,13);
  72. pm.addPage(page10);
  73. }
  74. else
  75. {
  76. string p10[3] = {"您没有使用该功能的权限!","0-上一页","编号:"};
  77. Page* page10 = new Page(p10,3,13);
  78. pm.addPage(page10);
  79. }
  80. //Page addbook //func
  81. if(0 == current_user->iden)
  82. {
  83. string p11[9] = {"增加图书","书名:","ISBN:","作者:","出版社:","出版时间:","价格:","描述:","0-上一页"};
  84. Page* page11 = new Page(p11,9,14);
  85. pm.addPage(page11);
  86. }
  87. else
  88. {
  89. string p11[3] = {"您没有使用该功能的权限!","0-上一页","编号:"};
  90. Page* page11 = new Page(p11,3,14);
  91. pm.addPage(page11);
  92. }
  93. if(-1 == current_user->iden)
  94. {
  95. string p12[3] = {"您没有使用该功能的权限!","0-上一页","编号:"};
  96. Page* page12 = new Page(p12,3,21);
  97. pm.addPage(page12);
  98. string p13[3] = {"您没有使用该功能的权限!","0-上一页","编号:"};
  99. Page* page13 = new Page(p13,3,22);
  100. pm.addPage(page13);
  101. }
  102. else
  103. {
  104. //Page borrowbook //func
  105. string p12[5] = {"借书","1-书名","2-ISBN","0-上一页","搜索:"};
  106. Page* page12 = new Page(p12,5,21,"用书名搜索 1-xxx,isbn 2-12...");
  107. pm.addPage(page12);
  108. //Page returnbook //func
  109. string p13[5] = {"还书","1-书名","2-ISBN","0-上一页","搜索:"};
  110. Page* page13 = new Page(p13,5,22,"书名搜索 1-xxx,isbn搜索 2-xxx");
  111. pm.addPage(page13);
  112. }
  113. //Page rewritekey //func
  114. string p14[4] = {"修改密码","新密码:","确认密码:","0-上一页"};
  115. Page* page14 = new Page(p14,4,31);
  116. pm.addPage(page14);
  117. //Page borrowrecord //list func
  118. string p15[2] = {"借阅记录","0-上一页"};
  119. Page* page15 = new Page(p15,2,32);
  120. pm.addPage(page15);
  121. //Page logoff 33
  122. //Page Userlist //list func
  123. string p16[2] = {"用户列表","0-上一页"};
  124. Page* page16 = new Page(p16,2,41);
  125. pm.addPage(page16);
  126. //Page addUser //func
  127. string p17[4] = {"添加用户","用户名:","身份:","0-上一页"};
  128. Page* page17 = new Page(p17,4,42,"身份0-管理员 1-普通用户,密码默认为000000");
  129. pm.addPage(page17);
  130. //Page delUser //func
  131. string p18[3] = {"删除用户","用户名:","0-上一页"};
  132. Page* page18 = new Page(p18,3,43);
  133. pm.addPage(page18);
  134. //Page resetket //func
  135. string p19[3] = {"重置密码","用户名:","0-上一页"};
  136. Page* page19 = new Page(p19,3,44,"密码重置为000000");
  137. pm.addPage(page19);
  138. //Pafe hotbook //rank
  139. string p20[2] = {"热门图书","0-上一页"};
  140. Page* page20 = new Page(p20,2,61);
  141. pm.addPage(page20);
  142. //Page borrowMaxbook //rank
  143. string p21[2] = {"借书狂人","0-上一页"};
  144. Page* page21 = new Page(p21,2,62);
  145. pm.addPage(page21);
  146. //Page newbook //rank
  147. string p22[2] = {"最新图书","0-上一页"};
  148. Page* page22 = new Page(p22,2,63);
  149. pm.addPage(page22);
  150. //**************************************三级页面
  151. //Page searchbytitle //func
  152. string p23[3] = {"按书名搜索","书名:","0-上一页"};
  153. Page* page23 = new Page(p23,3,121);
  154. pm.addPage(page23);
  155. //Page searchbyisbn //func
  156. string p24[3] = {"按isbn搜索","ISBN:","0-上一页"};
  157. Page* page24 = new Page(p24,3,122);
  158. pm.addPage(page24);
  159. //Page searchbypublisher //func
  160. string p25[3] = {"按出版社搜索","出版社:","0-上一页"};
  161. Page* page25 = new Page(p25,3,123);
  162. pm.addPage(page25);
  163. //Page searchbyauthor //func
  164. string p26[3] = {"按作者搜索","作者:","0-上一页"};
  165. Page* page26 = new Page(p26,3,124);
  166. pm.addPage(page26);
  167. //Page delbytitle //func
  168. string p27[3] = {"按书名删除","书名:","0-上一页"};
  169. Page* page27 = new Page(p27,3,131);
  170. pm.addPage(page27);
  171. //Page delbyisdn //func
  172. string p28[3] = {"按isdn删除","ISBN:","0-上一页"};
  173. Page* page28 = new Page(p28,3,132);
  174. pm.addPage(page28);

如果是选择页面,绘制时只需要调用PtintPage()即可,如果是功能页面,调用PrintFunctionPage()即可

  1. Page* arrow = pm.allpage[0]; //打印第0级页面
  2. while(1)
  3. {
  4. if(arrow == pm.allpage[0]||arrow == pm.allpage[1]||arrow == pm.allpage[2]||arrow == pm.allpage[3]||arrow == pm.allpage[4]||
  5. arrow == pm.allpage[5]||arrow == pm.allpage[7]||arrow == pm.allpage[9]||arrow == pm.allpage[10])
  6. arrow->PtintPage(); //选择页面
  7. else if(arrow == nullptr) //退出登录
  8. {
  9. u_SaveData(u_list);
  10. book_SaveData(book_list);
  11. borrow_SaveData(borrow);
  12. goto step1;
  13. }
  14. else //功能页面和列表页面
  15. arrow->PrintFunctionPage(u_list,arrow,current_user,book_list,borrow);
  16. if(arrow == pm.allpage[7]) //保存退出系统
  17. break;
  18. pm.ChangePage(arrow); //切换页面
  19. }

如果是功能页面或列表页面,就调用 PrintFunctionPage(),当然功能页面不仅只有指引,还要实现指定功能,只需调用对应函数实现即可

  1. void Page::PrintFunctionPage(UserList*& ulc,Page*& arrowc,User*& cu,BookList*& blc,BorrowManage& bor)
  2. {
  3. arrowc->PrintFPage(); //打印功能页面和列表页面的页面指引
  4. switch(arrowc->number)
  5. {
  6. case 11:
  7. PrintBookList(blc,arrowc);
  8. break;
  9. case 12: //列表页面,在上面已输出
  10. break;
  11. case 13: //列表页面,在上面已输出
  12. break;
  13. case 14:
  14. if(cu->iden!=0)
  15. break;
  16. AddBook(blc);
  17. break;
  18. case 21:
  19. BorrowBooks(blc,cu,bor);
  20. break;
  21. case 22:
  22. ShowBorrowRecord(bor,cu);
  23. ReturnBook(bor,cu);
  24. break;
  25. case 31:
  26. ChangePassport(cu);
  27. break;
  28. case 32:
  29. ShowBorrowRecord(bor,cu);
  30. break;
  31. case 41:
  32. if(cu->iden!=0)
  33. break;
  34. printUserList(ulc);
  35. break;
  36. case 42:
  37. if(cu->iden!=0)
  38. break;
  39. addUser(ulc);
  40. break;
  41. case 43:
  42. if(cu->iden!=0)
  43. break;
  44. SetCCPos(0,10);
  45. printUserList(ulc);
  46. delUser(ulc,bor);
  47. break;
  48. case 44:
  49. resetKey(ulc);
  50. break;
  51. case 61:
  52. HotBookRank(bor);
  53. break;
  54. case 62:
  55. CrazyBorrwoMan(ulc);
  56. break;
  57. case 63:
  58. LatestBook(blc);
  59. break;
  60. case 121:
  61. SearchByName(blc);
  62. break;
  63. case 122:
  64. SearchByISBN(blc);
  65. break;
  66. case 123:
  67. SearchByPublising(blc);
  68. break;
  69. case 124:
  70. SearchByAuthor(blc);
  71. break;
  72. case 131:
  73. DelBookByName(blc);
  74. break;
  75. case 132:
  76. DelBookByISBN(blc);
  77. break;
  78. }
  79. }

界面的切换

思路:先要获取当前页面的编号,再获取用户输入的号码,将两者合并就是目标页面的编号,如第一级页面“个人信息”页面的编号是3,如果用户想要去“借阅记录”页面,用户输入了2,那么32就是”借阅记录“的编号。

这就引出了一个新的问题,要将”借阅记录“页面与编号32联系在一起,这就是PageManage中ChangePage()的作用

  1. //Page.h中
  2. void PageManage::ChangePage(Page *&arrow)
  3. {
  4. int select = 0; //当前页的选择
  5. cin>>select;
  6. if(arrow == allpage[3]&&select == 3)//退出登录
  7. {
  8. arrow = nullptr;
  9. return;
  10. }
  11. if(0 == select) //返回上一页
  12. {
  13. arrow->ReflashSelect(select);
  14. select/=100;
  15. }
  16. else
  17. {
  18. arrow->ReflashSelect(select);
  19. }
  20. switch(select)
  21. {
  22. case 0:
  23. arrow = allpage[0];
  24. break;
  25. case 1:
  26. arrow = allpage[1];
  27. break;
  28. case 2:
  29. arrow = allpage[2];
  30. break;
  31. case 3:
  32. arrow = allpage[3];
  33. break;
  34. case 4:
  35. arrow = allpage[4];
  36. break;
  37. case 5:
  38. arrow = allpage[5];
  39. break;
  40. case 6:
  41. arrow = allpage[6];
  42. break;
  43. case 7:
  44. arrow = allpage[7];
  45. break;
  46. case 11:
  47. arrow = allpage[8];
  48. break;
  49. case 12:
  50. arrow = allpage[9];
  51. break;
  52. case 13:
  53. arrow = allpage[10];
  54. break;
  55. case 14:
  56. arrow = allpage[11];
  57. break;
  58. case 21:
  59. arrow = allpage[12];
  60. break;
  61. case 22:
  62. arrow = allpage[13];
  63. break;
  64. case 31:
  65. arrow = allpage[14];
  66. break;
  67. case 32:
  68. arrow = allpage[15];
  69. break;
  70. case 41:
  71. arrow = allpage[16];
  72. break;
  73. case 42:
  74. arrow = allpage[17];
  75. break;
  76. case 43:
  77. arrow = allpage[18];
  78. break;
  79. case 44:
  80. arrow = allpage[19];
  81. break;
  82. case 61:
  83. arrow = allpage[20];
  84. break;
  85. case 62:
  86. arrow = allpage[21];
  87. break;
  88. case 63:
  89. arrow = allpage[22];
  90. break;
  91. case 121:
  92. arrow = allpage[23];
  93. break;
  94. case 122:
  95. arrow = allpage[24];
  96. break;
  97. case 123:
  98. arrow = allpage[25];
  99. break;
  100. case 124:
  101. arrow = allpage[26];
  102. break;
  103. case 131:
  104. arrow = allpage[27];
  105. break;
  106. case 132:
  107. arrow = allpage[28];
  108. break;
  109. }
  110. }

形成新的编号后,就将arrow指向该页面,下一次while循环时就会打印该页面

信息载入与储存

信息储存

思路:用户信息在程序里是用链表储存的,遍历各个节点,将信息写入User.txt就行

  1. //FileIO.cpp
  2. void u_SaveData(UserList* p)
  3. {
  4. ofstream fp(userfile,ios::trunc);
  5. User* current = p->head;
  6. int length = p->length;
  7. while(length--)
  8. {
  9. fp<<endl<<current->name<<" ";
  10. fp<<current->key<<" ";
  11. fp<<current->iden<<" ";
  12. fp<<current->borrownum<<" ";
  13. fp<<current->sumbooknum;
  14. int booknum = current->borrowbookname.size();
  15. if(booknum == 0)
  16. {
  17. current = current->next;
  18. continue;
  19. }
  20. for(int i = 0;i < booknum-1;i++)
  21. {
  22. fp<<" "<<current->borrowbookname[i];
  23. }
  24. fp<<" "<<current->borrowbookname[booknum-1];
  25. current = current->next;
  26. }
  27. fp.close();
  28. }
信息载入

思路:如果User.txt里有数据,先创建一个节点,将信息读出来,并把这个节点连入链表

  1. void u_LordData (UserList* p)
  2. {
  3. ifstream fp(userfile);
  4. if(!fp)
  5. {
  6. return;
  7. }
  8. while(fp.peek() != EOF)
  9. {
  10. u_CreatPoint(p);
  11. User* current = p->tail;
  12. string bookname;
  13. fp>>current->name>>current->key;
  14. fp>>current->iden>>current->borrownum>>current->sumbooknum;
  15. int num = current->borrownum;
  16. while(num--)
  17. {
  18. fp>>bookname;
  19. current->borrowbookname.push_back(bookname);
  20. }
  21. }
  22. fp.close();
  23. }
  24. void u_CreatPoint (UserList* p)
  25. {
  26. User* current = new User;
  27. current->next = nullptr;
  28. if(p->head ==nullptr)
  29. {
  30. p->head = current;
  31. p->tail = current;
  32. }
  33. else
  34. {
  35. p->tail->next = current;
  36. p->tail = p->tail->next;
  37. }
  38. p->length++;
  39. }
  • 书籍信息(链表储存)

书籍信息储存
  1. void book_SaveData(BookList* p)
  2. {
  3. ofstream fp(bookfile,ios::trunc);
  4. Book* current = p->head;
  5. int length = p->length;
  6. fp<<"id isbn name author publishing publisheddate price pages description"<<endl;
  7. while(length--)
  8. {
  9. fp<<current->id<<"\t";
  10. fp<<current->isbn<<"\t";
  11. fp<<current->bookname<<"\t";
  12. fp<<current->author<<"\t";
  13. fp<<current->publising<<"\t";
  14. fp<<current->publisheddate<<"\t";
  15. fp<<current->price<<"\t";
  16. fp<<current->page<<"\t";
  17. fp<<current->description<<"\n";
  18. current = current->next;
  19. }
  20. fp.close();
  21. }
书籍信息载入 
  1. void book_LordData(BookList* p)
  2. {
  3. ifstream fp(bookfile);
  4. if(!fp)
  5. {
  6. return;
  7. }
  8. string title;
  9. getline(fp,title,'\n');
  10. while(fp.peek() != EOF)
  11. {
  12. book_CreatPoint(p);
  13. Book* current = p->tail;
  14. fp>>current->id;
  15. fp.get();
  16. getline(fp,current->isbn,'\t');
  17. getline(fp,current->bookname,'\t');
  18. getline(fp,current->author,'\t');
  19. getline(fp,current->publising,'\t');
  20. getline(fp,current->publisheddate,'\t');
  21. fp>>current->price;
  22. fp.get();
  23. fp>>current->page;
  24. fp.get();
  25. getline(fp,current->description,'\n');
  26. /*char c = fp.peek();
  27. int num = current->id+1;
  28. while(num>=10)
  29. num/=10;
  30. int count = 0;
  31. while(!(c >'0'&&c<='9'))
  32. {
  33. string t;
  34. getline(fp,t,'\n');
  35. if(t == "")
  36. count++;
  37. if(t==""&&count>=5)
  38. {
  39. count = 0;
  40. break;
  41. }
  42. current->description+=t;
  43. c = fp.peek();
  44. }*/
  45. }
  46. fp.close();
  47. }
  48. void book_CreatPoint(BookList* p)
  49. {
  50. Book* current = new Book;
  51. current->next = nullptr;
  52. if(p->head == nullptr)
  53. {
  54. p->head = current;
  55. p->tail = current;
  56. }
  57. else
  58. {
  59. p->tail->next = current;
  60. p->tail = p->tail->next;
  61. }
  62. p->length++;
  63. }

注释掉的内容是为了区分文件中书籍的内容简介,如果书籍数据格式规范,就不需要这个

  • 借阅信息(vector储存)

借阅信息储存
  1. void borrow_SaveData(BorrowManage& bo)
  2. {
  3. ofstream fp(borrowfile,ios::trunc);
  4. int num = bo.borrowrecording.size();
  5. for(int i = 0;i < num;i++)
  6. {
  7. fp<<bo.borrowrecording[i]->Name_book()<<"\t";
  8. fp<<bo.borrowrecording[i]->Isbn_book()<<"\t";
  9. fp<<bo.borrowrecording[i]->Date_borrow()<<"\t";
  10. fp<<bo.borrowrecording[i]->Date_return()<<"\t";
  11. fp<<bo.borrowrecording[i]->Username()<<endl;
  12. }
  13. fp.close();
  14. }
 借阅信息载入
  1. void borrow_LordData(BorrowManage& bo)
  2. {
  3. ifstream fp(borrowfile);
  4. while(fp.peek()!=EOF)
  5. {
  6. string bookname,isbn,borrowdate,returndate,username;
  7. getline(fp,bookname,'\t');
  8. getline(fp,isbn,'\t');
  9. getline(fp,borrowdate,'\t');
  10. getline(fp,returndate,'\t');
  11. getline(fp,username,'\n');
  12. Borrow* temp = new Borrow(bookname,isbn,borrowdate,username);
  13. bo.addPage(temp);
  14. }
  15. fp.close();
  16. }

图书管理

  • 全部图书(图书列表)

思路:宏定义onepage是20,定义begin和end,打印的信息就是从begin到end的节点信息,而begin和end的数值根据page来算得,那就可以通过控制page来控制打印的信息

  1. void PrintBookList(BookList* p,Page*& pa)
  2. {
  3. Book* current = p->head;
  4. int length = p->length;
  5. int page = 1,
  6. begin,
  7. end;
  8. while(1)
  9. {
  10. system("cls");
  11. SetCCPos(0,0);
  12. pa->PrintFPage();
  13. if(page<1) //最前一页
  14. page = 1;
  15. else if(page>ceil(length/20)) //最后一页
  16. page = ceil(length/20.0);
  17. begin = (page-1)*onepage,
  18. end = page*onepage;
  19. cout<<"id name author publishing"<<endl;
  20. current = p->head;
  21. for(int i = 0;i < (page-1)*onepage;i++)
  22. {
  23. current = current->next;
  24. }
  25. for(int i = begin;i < end;i++)
  26. {
  27. cout<<current->id-((page-1)*onepage)<<"\t";
  28. string name = current->bookname;
  29. while(name.length()<55)
  30. name+=" ";
  31. cout<<name;
  32. string author = current->author;
  33. while(author.length()<15)
  34. author+=" ";
  35. cout<<author;
  36. string publishing = current->publising;
  37. while(publishing.length()<10)
  38. publishing+=" ";
  39. cout<<publishing<<"\n";
  40. current = current->next;
  41. if(current == nullptr)
  42. break;
  43. }
  44. cout<<"第"<<page<<"页"<<endl;
  45. int num = 0;
  46. cin.sync(); //清空缓存区
  47. char t = cin.peek();
  48. if(t == '+')
  49. {
  50. page++;
  51. continue;
  52. }
  53. else if(t == '-')
  54. {
  55. page--;
  56. continue;
  57. }
  58. else if(t == '*')
  59. {
  60. cin.ignore();
  61. cout<<"跳转到:";
  62. cin>>page;
  63. continue;
  64. }
  65. else if(t == '\n')
  66. {
  67. continue;
  68. }
  69. cin>>num; //接收用户想查看书籍的编号
  70. switch(num)
  71. {
  72. case 0:
  73. cout<<"输入0返回上一页"<<endl;
  74. return;
  75. default:
  76. num = num+(page-1)*onepage;
  77. PrintBookDetil(num,p);
  78. break;
  79. }
  80. }
  81. }
  1. void PrintBookDetil(int n,BookList* p)
  2. {
  3. system("cls");
  4. Book* current = p->head;
  5. for(int i = 0;i < n-1;i++)
  6. {
  7. current = current->next;
  8. }
  9. cout<<"书名:"<<current->bookname<<endl;
  10. cout<<"ISBN:"<<current->isbn<<endl;
  11. cout<<"作者:"<<current->author<<endl;
  12. cout<<"页数:"<<current->page<<endl;
  13. cout<<"价格:"<<current->price<<endl;
  14. cout<<"出版时间:"<<current->publisheddate<<endl;
  15. cout<<"出版社:"<<current->publising<<endl;
  16. cout<<"简介:"<<current->description<<endl<<endl;
  17. system("pause");
  18. }
  • 查找图书

思路:书籍信息是用链表储存的,从用户输入中获取相关信息,再遍历链表找到对应节点,再输出该节点的相关信息

以书名搜索
  1. //BookManage.cpp
  2. void SearchByName(BookList*& p)
  3. {
  4. SetCCPos(5,4);
  5. string name;
  6. cin>>name;
  7. Book* current = p->head;
  8. SetCCPos(0,6);
  9. cout<<"id name author publishing"<<endl;
  10. while(current != nullptr)
  11. {
  12. if(current->bookname == name)
  13. {
  14. cout<<current->id<<"\t";
  15. string name = current->bookname;
  16. while(name.length()<55) //输出格式化
  17. name+=" ";
  18. cout<<name;
  19. string author = current->author;
  20. while(author.length()<15) //输出格式化
  21. author+=" ";
  22. cout<<author;
  23. string publishing = current->publising;
  24. while(publishing.length()<10) //输出格式化
  25. publishing+=" ";
  26. cout<<publishing<<"\n";
  27. }
  28. current = current->next;
  29. }
  30. }
 以ISBN搜索
  1. void SearchByISBN(BookList*& p)
  2. {
  3. SetCCPos(5,4);
  4. string isbn;
  5. cin>>isbn;
  6. Book* current = p->head;
  7. SetCCPos(0,6);
  8. cout<<"id name author publishing"<<endl;
  9. while(current != nullptr)
  10. {
  11. if(current->isbn == isbn)
  12. {
  13. cout<<current->id<<"\t";
  14. string name = current->bookname;
  15. while(name.length()<55) //输出格式化
  16. name+=" ";
  17. cout<<name;
  18. string author = current->author;
  19. while(author.length()<15) //输出格式化
  20. author+=" ";
  21. cout<<author;
  22. string publishing = current->publising;
  23. while(publishing.length()<10) //输出格式化
  24. publishing+=" ";
  25. cout<<publishing<<"\n";
  26. }
  27. current = current->next;
  28. }
  29. }
以出版社搜索
  1. //BookManage.cpp
  2. void SearchByPublising(BookList*& p)
  3. {
  4. SetCCPos(7,4);
  5. string publising;
  6. cin>>publising;
  7. Book* current = p->head;
  8. SetCCPos(0,6);
  9. cout<<"id name author publishing"<<endl;
  10. while(current != nullptr)
  11. {
  12. if(current->publising == publising)
  13. {
  14. cout<<current->id<<"\t";
  15. string name = current->bookname;
  16. while(name.length()<55)
  17. name+=" ";
  18. cout<<name;
  19. string author = current->author;
  20. while(author.length()<15)
  21. author+=" ";
  22. cout<<author;
  23. string publishing = current->publising;
  24. while(publishing.length()<10)
  25. publishing+=" ";
  26. cout<<publishing<<"\n";
  27. }
  28. current = current->next;
  29. }
  30. }
以作者搜索
  1. void SearchByAuthor(BookList*& p)
  2. {
  3. SetCCPos(5,4);
  4. string author;
  5. cin>>author;
  6. Book* current = p->head;
  7. SetCCPos(0,6);
  8. cout<<"id name author publishing"<<endl;
  9. while(current != nullptr)
  10. {
  11. if(current->author == author)
  12. {
  13. cout<<current->id<<"\t";
  14. string name = current->bookname;
  15. while(name.length()<55)
  16. name+=" ";
  17. cout<<name;
  18. string author = current->author;
  19. while(author.length()<15)
  20. author+=" ";
  21. cout<<author;
  22. string publishing = current->publising;
  23. while(publishing.length()<10)
  24. publishing+=" ";
  25. cout<<publishing<<"\n";
  26. }
  27. current = current->next;
  28. }
  29. }
  • 删除图书

思路:从用户输入获取目标书籍的信息,遍历链表,如果找到记录该书籍的节点,则删除该节点,如果没找到,就输出“未找到该书籍” 。

以书名删除
  1. //BookManagge.cpp
  2. void DelBookByName(BookList* p)
  3. {
  4. SetCCPos(5,4);
  5. string name;
  6. cin>>name;
  7. Book* current = p->head;
  8. while(current!=nullptr)
  9. {
  10. if(current->next->bookname == name)
  11. {
  12. Book* temp = current->next;
  13. current->next = temp->next;
  14. delete temp;
  15. p->length--;
  16. SetCCPos(0,6);
  17. cout<<"删除成功"<<endl;
  18. return;
  19. }
  20. current = current->next;
  21. }
  22. SetCCPos(0,6);
  23. cout<<"未找到该书,请重进"<<endl;
  24. return;
  25. }
以ISBN删除
  1. //BookManage.cpp
  2. void DelBookByISBN(BookList* p)
  3. {
  4. SetCCPos(5,4);
  5. string ISBN;
  6. cin>>ISBN;
  7. Book* current = p->head;
  8. while(current!=nullptr)
  9. {
  10. if(current->next->isbn == ISBN)
  11. {
  12. Book* temp = current->next;
  13. current->next = temp->next;
  14. delete temp;
  15. p->length--;
  16. SetCCPos(0,6);
  17. cout<<"删除成功"<<endl;
  18. return;
  19. }
  20. current = current->next;
  21. }
  22. SetCCPos(0,6);
  23. cout<<"未找到该书,请重进"<<endl;
  24. return;
  25. }
  • 增加图书

  1. void AddBook(BookList* p)
  2. {
  3. book_CreatPoint(p);
  4. Book* current = p->tail;
  5. p->tail->id = p->length;
  6. SetCCPos(5,4);
  7. cin>>current->bookname;
  8. if(current->bookname == "0")
  9. {
  10. SetCCPos(0,12);
  11. cout<<"请输入0退出"<<endl;
  12. return;
  13. }
  14. SetCCPos(5,5);
  15. cin>>current->isbn;
  16. SetCCPos(5,6);
  17. cin>>current->author;
  18. SetCCPos(7,7);
  19. cin>>current->publising;
  20. SetCCPos(9,8);
  21. cin>>current->publisheddate;
  22. SetCCPos(5,9);
  23. cin>>current->price;
  24. SetCCPos(5,10);
  25. cin>>current->description;
  26. SetCCPos(0,12);
  27. cout<<"添加成功,输入0退出"<<endl;
  28. }

书籍借还(借还记录用vector记录)

借书

思路:从用户输入获取相关信息,搜先要判断这本书用户是否已经借了还没有还,如果借了没还,输出“已借过”,否则就查找书籍链表中是否有该书籍,如果有,就记录下这条借阅信息,如果没有该书籍,就输出“查无此书”。

  1. void BorrowBooks(BookList* bl,User* u,BorrowManage& bo)
  2. {
  3. Book* current = bl->head;
  4. SetCCPos(5,7);
  5. string s;
  6. cin>>s;
  7. if(s[0] == '0')
  8. {
  9. SetCCPos(0,10);
  10. cout<<"输入0返回上一页";
  11. return;
  12. }
  13. else if(s[0] == '1')
  14. {
  15. s.erase(0,2);
  16. vector<string>::iterator iter=std::find(u->borrowbookname.begin(),u->borrowbookname.end(),s);
  17. if(iter != u->borrowbookname.end())
  18. {
  19. SetCCPos(0,9);
  20. cout<<u->name<<"已借过"<<"<<"<<s<<">>"<<endl;
  21. return;
  22. }
  23. while(current!=nullptr)
  24. {
  25. if(s == current->bookname)
  26. {
  27. u->borrownum++;
  28. u->sumbooknum++;
  29. RecordBorrowInf(current,bo,u);
  30. u->borrowbookname.push_back(current->bookname);
  31. SetCCPos(0,9);
  32. cout<<u->name<<"成功借阅"<<"<<"<<current->bookname<<">>"<<endl;
  33. return;
  34. }
  35. current = current->next;
  36. }
  37. SetCCPos(0,9);
  38. cout<<"查无此书"<<endl;
  39. return;
  40. }
  41. else if(s[0] == '2')
  42. {
  43. s.erase(0,2);
  44. while(current!=nullptr)
  45. {
  46. if(s == current->isbn)
  47. {
  48. string sbookname = current->bookname;
  49. vector<string>::iterator iter=std::find(u->borrowbookname.begin(),u->borrowbookname.end(),sbookname);
  50. if(iter != u->borrowbookname.end())
  51. {
  52. SetCCPos(0,9);
  53. cout<<u->name<<"已借过"<<"<<"<<current->bookname<<">>"<<endl;
  54. return;
  55. }
  56. u->borrownum++;
  57. u->sumbooknum++;
  58. RecordBorrowInf(current,bo,u);
  59. u->borrowbookname.push_back(current->bookname);
  60. SetCCPos(0,9);
  61. cout<<u->name<<"成功借阅"<<"<<"<<current->bookname<<">>"<<endl;
  62. return;
  63. }
  64. current = current->next;
  65. }
  66. SetCCPos(0,9);
  67. cout<<"查无此书"<<endl;
  68. return;
  69. }
  70. }
  71. void RecordBorrowInf(Book* current,BorrowManage& bo,User* u)
  72. {
  73. time_t timep;
  74. string t;
  75. time(&timep);
  76. t = ctime(&timep);
  77. t.pop_back();
  78. Borrow* temp = new Borrow(current->bookname,current->isbn,t,u->name);
  79. bo.addPage(temp);
  80. }

代码解析:vector<string>::iterator iter=std::find(u->borrowbookname.begin(),u->borrowbookname.end(),s);         //定义了一个vector<string>类型的迭代器iter用来装find函数的返回值,而传递给find函数的三个参数是查找的开头,查找的结尾,查找的内容,如果iter指向borrowbookname.end(),那么就是没有找到,如果不是,iter指向目标位置。

还书

思路:从用户输入获取要还书的书名(或isbn),遍历借阅记录,从中找到该用户的借阅记录,再对比该用户借的书与要还书的书名(或isbn)是否一致,如果一致,就修改借阅记录,将归还时间改为当前时间。如果该用户借的书中没有该书,或没有该用户的借阅记录,都输出“未借阅”。

  1. void ReturnBook(BorrowManage& bo,User* u)
  2. {
  3. SetCCPos(5,7);
  4. string s;
  5. int num = bo.borrowrecording.size();
  6. time_t timep;
  7. string r_time;
  8. time(&timep);
  9. r_time = ctime(&timep);
  10. cin>>s;
  11. if(s[0] == '0')
  12. {
  13. SetCCPos(0,15);
  14. cout<<"输入0返回上一页";
  15. return;
  16. }
  17. else if(s[0] == '1')
  18. {
  19. s.erase(0,2);
  20. for(int i = 0;i < num;i++)
  21. {
  22. string bookname = bo.borrowrecording[i]->Name_book();
  23. if(u->name == bo.borrowrecording[i]->Username())
  24. {
  25. if(s == bookname)
  26. {
  27. bo.borrowrecording[i]->RewriteDate(r_time);
  28. u->borrowbookname.erase(std::remove(u->borrowbookname.begin(), u->borrowbookname.end(), bookname), u->borrowbookname.end());
  29. u->borrownum--;
  30. SetCCPos(0,20);
  31. cout<<u->name<<"成功归还"<<"<<"<<bookname<<">>"<<endl;
  32. return;
  33. }
  34. }
  35. }
  36. SetCCPos(0,20);
  37. cout<<u->name<<"未借阅"<<"<<"<<s<<">>"<<endl;
  38. return;
  39. }
  40. else if(s[0] == '2')
  41. {
  42. s.erase(0,2);
  43. for(int i = 0;i < num;i++)
  44. {
  45. if(s == bo.borrowrecording[i]->Isbn_book())
  46. {
  47. string bookname = bo.borrowrecording[i]->Name_book();
  48. if(u->name == bo.borrowrecording[i]->Username())
  49. {
  50. bo.borrowrecording[i]->RewriteDate(r_time);
  51. u->borrowbookname.erase(std::remove(u->borrowbookname.begin(), u->borrowbookname.end(), bookname), u->borrowbookname.end());
  52. u->borrownum--;
  53. SetCCPos(0,20);
  54. cout<<u->name<<"成功归还"<<"<<"<<bookname<<">>"<<endl;
  55. return;
  56. }
  57. }
  58. }
  59. SetCCPos(0,20);
  60. cout<<u->name<<"未借阅"<<"<<"<<s<<">>"<<endl;
  61. return;
  62. }
  63. }

代码解析:u->borrowbookname.erase(std::remove(u->borrowbookname.begin(), u->borrowbookname.end(), bookname), u->borrowbookname.end());         //该行代码的目的是彻底删除一个元素,从内向外看,首先调用了remove函数,传递了三个参数,分别是查找的开头,查找的结尾,目标内容,它会将要删除的元素移到容器的末尾,并返回一个指向新末尾之后第一个不匹配元素的迭代器,之后调用erase函数,删除remove返回的新结尾到u->borrowbookname.end()之间的所有元素

个人信息

  • 修改密码

  1. //UserManage.cpp
  2. void ChangePassport(User* cu)//31
  3. {
  4. string p1,p2;
  5. SetCCPos(8,4);
  6. cin>>p1;
  7. SetCCPos(9,5);
  8. cin>>p2;
  9. SetCCPos(0,7);
  10. if(p1 == p2)
  11. {
  12. cu->key = p1;
  13. cout<<"修改密码成功"<<endl;
  14. }
  15. else
  16. {
  17. cout<<"前后密码不一致,请重进"<<endl;
  18. }
  19. }
  • 借阅记录

  1. //BorrowandReturn.cpp
  2. void ShowBorrowRecord(BorrowManage& bo,User* u)
  3. {
  4. cout<<endl<<"书籍名字 ISBN 借阅时间 还书状态"<<endl;
  5. int num = bo.borrowrecording.size();
  6. for(int i = 0;i < num;i++)
  7. {
  8. string uname = bo.borrowrecording[i]->Username();
  9. if(uname == u->name)
  10. {
  11. cout<<bo.borrowrecording[i]->Name_book()<<"\t";
  12. cout<<bo.borrowrecording[i]->Isbn_book()<<"\t";
  13. cout<<bo.borrowrecording[i]->Date_borrow()<<"\t";
  14. cout<<bo.borrowrecording[i]->Date_return()<<"\n";
  15. }
  16. }
  17. }
  • 退出登录

将arrow指向null,再在main中判断,如果arrow为空即返回登录界面

用户管理(需要管理员权限)

  • 用户列表

用户信息是用链表储存,遍历链表即可

  1. //UserManage.cpp
  2. void printUserList(UserList* ul)//41
  3. {
  4. User* current = ul->head;
  5. cout<<"用户名"<<" "<<"密码"<<" "<<"身份"<<" "<<"现借阅书数"<<endl;
  6. while(current!= NULL)
  7. {
  8. cout<<current->name;
  9. cout<<" "<<current->key;
  10. cout<<" ";
  11. if(current->iden == 0)
  12. cout<<"管理员"<<" ";
  13. else if(current->iden == 1)
  14. cout<<"普通用户"<<" ";
  15. cout<<current->borrownum<<endl;
  16. current = current->next;
  17. }
  18. }
  • 增加用户

新增节点,接到用户链表结尾

  1. //UserManage.cpp
  2. void addUser(UserList* p)//42
  3. {
  4. User* current = new User;
  5. current->next = nullptr;
  6. SetCCPos(7,4);
  7. cin>>current->name;
  8. SetCCPos(6,5);
  9. cin>>current->iden;
  10. current->key = "000000";
  11. current->borrownum = 0;
  12. SetCCPos(0,8);
  13. if(CheckName(current->name,p))
  14. {
  15. p->tail->next = current;
  16. p->length++;
  17. p->tail = p->tail->next;
  18. cout<<"添加成功"<<endl;
  19. }
  20. else
  21. {
  22. delete current;
  23. cout<<"用户名已被注册,请重进"<<endl;
  24. }
  25. }
  • 删除用户

删除该用户的节点,并且删除该用户的借阅记录

  1. //UserManage.cpp
  2. void delUser(UserList* p,BorrowManage& bo)
  3. {
  4. string dname;
  5. SetCCPos(7,4);
  6. cin>>dname;
  7. User* del = LoacteUser(dname,p);
  8. User* temp = p->head;
  9. while(temp->next != del&&temp!=nullptr)
  10. {
  11. temp = temp->next;
  12. }
  13. if(temp == nullptr)
  14. cout<<"找不到该用户"<<endl;
  15. if(del->next ==nullptr)
  16. {
  17. p->tail = temp;
  18. temp->next = del->next;
  19. p->length--;
  20. int num = bo.borrowrecording.size();
  21. for(int i = 0;i < num;i++)
  22. {
  23. if(bo.borrowrecording[i]->Username() == dname)
  24. bo.borrowrecording.erase(bo.borrowrecording.begin()+i);
  25. }
  26. delete del;
  27. SetCCPos(0,6);
  28. cout<<"删除成功"<<endl;
  29. return;
  30. }
  31. else if(del->next != nullptr)
  32. {
  33. temp->next = del->next;
  34. p->length--;
  35. int num = bo.borrowrecording.size();
  36. for(int i = 0;i < num;i++)
  37. {
  38. if(bo.borrowrecording[i]->Username() == dname)
  39. bo.borrowrecording.erase(bo.borrowrecording.begin()+i);
  40. }
  41. delete del;
  42. SetCCPos(0,6);
  43. cout<<"删除成功"<<endl;
  44. return;
  45. }
  46. SetCCPos(0,6);
  47. cout<<"未查找到该用户,请重进"<<endl;
  48. }
  • 重置指定用户密码

  1. //UserManage.cpp
  2. void resetKey(UserList* p)
  3. {
  4. string rname;
  5. SetCCPos(7,4);
  6. cin>>rname;
  7. User* reset = LoacteUser(rname,p);
  8. reset->key = "000000";
  9. SetCCPos(0,7);
  10. cout<<"重置成功"<<endl;
  11. }

排行榜

  • 热门图书

思路:创建一个借书排行榜类,再创建一个管理借书排行榜类,借书排行榜对象中储存着这本书的书名,isbn和被借阅的次数,每个 借书排行榜对象 的地址装在 管理结束排行榜对象 的vector里,再依据被借阅次数在vector里从大到小重新排序,再输出前十个当作前十排行榜

  1. //head.h
  2. class BorrowBookRank
  3. {
  4. public:
  5. BorrowBookRank(string bookna,string isb):number(1)
  6. {
  7. bookname = bookna;
  8. isbn = isb;
  9. }
  10. string getBookname(void)
  11. {
  12. return bookname;
  13. }
  14. int number; //被借阅次数
  15. private:
  16. string bookname; //书名
  17. string isbn;
  18. };
  19. class BorrowBookRankManage
  20. {
  21. public:
  22. vector <BorrowBookRank*> borrowbookrank;
  23. void addrankbook(BorrowBookRank* bo)
  24. {
  25. borrowbookrank.push_back(bo);
  26. }
  27. };
  1. //Ranking.cpp
  2. bool cmp(BorrowBookRank* a,BorrowBookRank* b)
  3. {
  4. return a->number>b->number;
  5. }
  6. void HotBookRank(BorrowManage& bo)
  7. {
  8. BorrowBookRankManage bbrm; //管理排行榜
  9. int records = bo.borrowrecording.size();
  10. vector <string>exist; //记录用户借过的书
  11. vector<string>::iterator iter;
  12. for(int i = 0;i < records;i++)
  13. {
  14. string recordbookname = bo.borrowrecording[i]->Name_book();
  15. iter = std::find(exist.begin(), exist.end(), recordbookname); //查找是否已经在exist中,防止该书重复添加
  16. if (iter == exist.end())
  17. {
  18. exist.push_back(recordbookname);
  19. BorrowBookRank* temp = new BorrowBookRank(recordbookname,bo.borrowrecording[i]->Isbn_book());
  20. bbrm.addrankbook(temp); //添加到排行榜里
  21. }
  22. else
  23. {
  24. int num = bbrm.borrowbookrank.size();
  25. for(int j = 0;j < num;j++)
  26. {
  27. if(recordbookname == bbrm.borrowbookrank[j]->getBookname())
  28. {
  29. bbrm.borrowbookrank[j]->number++; //被借阅书加一
  30. break;
  31. }
  32. }
  33. }
  34. }
  35. sort(bbrm.borrowbookrank.begin(),bbrm.borrowbookrank.end(),cmp); //排行榜依据被借阅数排序
  36. cout<<"图书排行 借阅人数"<<endl;
  37. if(bbrm.borrowbookrank.size() == 0)
  38. return;
  39. for(int i = 0;i < 10;i++)
  40. {
  41. string na = bbrm.borrowbookrank[i]->getBookname();
  42. while(na.length()<40)
  43. na+=" ";
  44. cout<<na<<bbrm.borrowbookrank[i]->number<<endl;
  45. }
  46. }
  • 借书次数

  1. //Ranking.cpp
  2. struct CompareBySize
  3. {
  4. bool operator()(const short& a, const short& b) const
  5. {
  6. return a > b;
  7. }
  8. };
  9. void CrazyBorrwoMan(UserList* ul)
  10. {
  11. User* current = ul->head;
  12. multimap<short,string,CompareBySize> rankuser; //用户借阅数排行榜
  13. int num = ul->length;
  14. while(num--)
  15. {
  16. rankuser.insert(make_pair(current->sumbooknum,current->name)); //将用户名与借阅数之间形成映射
  17. current = current->next;
  18. }
  19. cout<<"用户名\t\t\t\t借阅数"<<endl;
  20. int count = 0;
  21. for(auto it = rankuser.begin();it != rankuser.end()&&count<10;it++,count++)
  22. {
  23. cout<<it->second<<"\t\t\t\t"<<it->first<<endl;
  24. }
  25. }

使用multimap而不用map和其他是因为如果两个用户的借阅数相同,用map就会将前一个映射覆盖掉,而使用multimap可以储存多重映射,允许借阅数重复 

  • 热门图书

思路:先获取各书籍的出版时间,再创建出版时间对象,然后将出版时间对象与书籍名称形成映射存入mutimap中,(需事先重载>运算符),然后依据日期大小排序,然后输出前十

  1. //Ranking.cpp
  2. struct CompareBySize_date
  3. {
  4. bool operator()(const Date& a, const Date& b) const
  5. {
  6. return a > b;
  7. }
  8. };
  9. void LatestBook(BookList* p)
  10. {
  11. Book* current = p->head;
  12. multimap<Date,string,CompareBySize_date> rankdate; //最新书籍排行榜
  13. int num = p->length;
  14. Date** dd = new Date*[num];
  15. for(int k = 0; k < num;k++)
  16. {
  17. string d = current->publisheddate;
  18. short year = 0,month = 0,day = 0;
  19. int count = 0;
  20. int dl = d.length();
  21. for(int i = 0;i < dl;i++) //获取该数的出版时间
  22. {
  23. if(d[i]>='0'&&d[i]<='9')
  24. {
  25. if(count ==0)
  26. year = year*10 + (d[i]-'0');
  27. else if(count == 1)
  28. month = month*10 + (d[i]-'0');
  29. else if(count == 2)
  30. day = day*10 + (d[i]-'0');
  31. }
  32. else
  33. count++;
  34. }
  35. dd[k] = new Date(year,month,day); //创建出版日期对象
  36. rankdate.insert(make_pair(*dd[k],current->bookname)); //将书籍与出版日期之间形成映射
  37. current = current->next;
  38. }
  39. int count1 = 0;
  40. for(auto it = rankdate.begin();it != rankdate.end()&&count1<10;it++,count1++)
  41. {
  42. string na = it->second;
  43. while(na.length()<50)
  44. na+=" ";
  45. cout<<na<<"\t\t\t\t"<<it->first<<endl;
  46. }
  47. for(int i = 0;i < num;i++)
  48. delete dd[i];
  49. delete[] dd;
  50. }

四、源代码资源

在主页上传的资源里

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/148709
推荐阅读
相关标签
  

闽ICP备14008679号