0


图书管理系统 (单链表实现,C++及文件操作,超详细)

大家好!这里是小张,五一即将到来,小张在这里提前祝大家劳动节快乐!那么今天小张给大家带来的是由单链表,C++以及文件操作实现的图书信息管理系统。

另外小张给大家推荐一款什么阶段都适用的刷题神器和模拟面试好用神器点击这里进入神器****

目录


图书管理系统菜单

  1. void menu()
  2. {
  3. cout << " ******************************************************************" << endl;
  4. cout << " *******************欢迎光临图书管理系统***************************" << endl;
  5. cout << " **************** 1.输入-1 退出程序 *******************" << endl;
  6. cout << " **************** 2.输入1 录入信息 *******************" << endl;
  7. cout << " **************** 3.输入2 陈列信息 *******************" << endl;
  8. cout << " **************** 4.输入3 按编号查找 *******************" << endl;
  9. cout << " **************** 5.输入4 按书名查找 *******************" << endl;
  10. cout << " **************** 6.输入5 删除 *******************" << endl;
  11. cout << " **************** 7.输入6 插入 *******************" << endl;
  12. cout << " **************** 8.输入7 修改 *******************" << endl;
  13. cout << " ******************************************************************" << endl;
  14. }

单链表的实现

  1. ypedef struct londe
  2. {
  3. admin data;//数据域
  4. struct londe *next;//指针域
  5. }londe, *strlonde;

图书类

  1. class admin
  2. {
  3. public:
  4. char name[100];
  5. char num[100];
  6. char tel[100];
  7. char name1[100];
  8. char adress[100];
  9. };

图书管理系统单链表的创建

  1. void londecreat(strlonde&L,int n)
  2. {
  3. L = (strlonde)malloc(sizeof(londe));
  4. L->next = NULL;
  5. strlonde p, q;
  6. q = L;
  7. int i =0;
  8. cout << "请您依次输入图书编号,图书名,图书作者,图书出版社,价格" << endl;
  9. for(i=0;i<n;i++)
  10. {
  11. p= (strlonde)malloc(sizeof(londe));
  12. cin >> p->data.name;
  13. cin >> p->data.num;
  14. cin >> p->data.tel;
  15. cin >> p->data.name1;
  16. cin >> p->data.adress;
  17. p->next = NULL;
  18. q->next = p;
  19. q = p;
  20. }
  21. ofstream ofs;//创建文件流
  22. ofs.open("text.txt", ios::out);//指定打开方式(在双引号里,可以根据自行情况进行更改)
  23. strlonde S;
  24. S = L->next;
  25. while (S)
  26. {
  27. ofs << S->data.name << " " << S->data.num << " " << S->data.tel << " " <<S->data.name1<<" "<<S->data.adress<<" "<< endl;
  28. S = S->next;
  29. }
  30. ofs.close();//关闭文件
  31. }

文件操作

我们为什么要使用文件操作,是因为“许多程序在实现过程中,依赖于把数据保存到变量中,而变量是通过内存单元存储数据的,数据的处理完全由程序控制。当一个程序运行完成或者终止的时候,所有变量的值不再保存。另外,当输入输出数据量较大时,就会受限,带来不便。文件是解决这些问题的有效办法,他通过把数据存储在磁盘文件中,得以长久保存。当有大量数据输入时,可通过编辑工具事先建立输入数据的文件,程序运行时将不再从键盘输入,而是从指定的文件上读入,从而实现数据的一次输入多次使用。”

写文件分为五步操作:

1.包含头文件
#include
2.创建流对象
ofstream ofs;
3.打开文件
ofs.open(“文件路径”,打开方式);
4.写数据
ofs<<“写入的数据”;
5.关闭文件
ofs.close();

读文件也是五步操作:

1.包含头文件
#include
2.创建流对象
ifstream ifs;
3.打开文件并判断是否成功
ifs.open(“文件路径”,打开方式);
4.读数据
四种方式读取
5.关闭文件
ifs.close();

具体的代码在图书信息管理系统中实现

陈列信息

  1. void display(strlonde&L)
  2. {
  3. ifstream ifs;//创建流对象
  4. ifs.open("text.txt",ios::in);//打开文件
  5. char buf[10000] = { 0 };
  6. int i = 0;
  7. cout << "下面是您录入的信息" << endl;
  8. while (ifs >> buf)//读取文件中数据
  9. {
  10. if (i % 5 == 0)
  11. {
  12. cout <<endl;
  13. }
  14. cout << buf << " ";
  15. i++;
  16. }
  17. ifs.close();//关闭文件
  18. }

根据编号查找图书

  1. void findname(char *arr)
  2. {
  3. char a[100];
  4. strcpy_s(a, arr);
  5. ifstream ifs;//创建流对象
  6. ifs.open("text.txt", ios::in);//打开文件
  7. char buf[1000] = { 0 };
  8. int i = 0;
  9. while (ifs >> buf)//读入文件
  10. {
  11. int n = strcmp(a, buf);
  12. if (n == 0)
  13. {
  14. cout << buf << " ";
  15. i++;
  16. continue;
  17. }
  18. if (i > 5)
  19. {
  20. return;
  21. }
  22. if (i <=5 && i >=1)
  23. {
  24. cout << buf << " ";
  25. i++;
  26. }
  27. }
  28. ifs.close();//关闭文件
  29. }

根据书名查找

  1. void findnum(char *arr)
  2. {
  3. char a[100];
  4. strcpy_s(a, arr);
  5. ifstream ifs;
  6. ifs.open("text.txt", ios::in);
  7. char buf[10000] = { 0 };
  8. int i = 0;
  9. while (ifs >> buf)
  10. {
  11. if (i == 3)
  12. {
  13. cout << buf << " ";
  14. return;
  15. }
  16. if (i==2)
  17. {
  18. cout << buf << " ";
  19. i++;
  20. }
  21. if (i == 1)
  22. {
  23. cout << buf << " ";
  24. i++;
  25. }
  26. int n = strcmp(a, buf);
  27. if (n == 0)
  28. {
  29. cout << buf << " ";
  30. i++;
  31. }
  32. }
  33. ifs.close();
  34. }

通过图书编号删除图书信息

  1. void delete1(strlonde&L,char *arr)
  2. {
  3. char a[100], b[100];
  4. int i = 0, j = 0, count = 0;
  5. strcpy_s(a, arr);
  6. while (*arr != '')
  7. {
  8. count++;
  9. arr++;
  10. }
  11. strlonde p, q, r;
  12. q = L;
  13. p = L->next;
  14. while (p != NULL)
  15. {
  16. strcpy_s(b, p->data.name);
  17. i = 0, j = 0;
  18. while (a[i] == b[j] && a[i] != ''&&b[j] != '')
  19. {
  20. i++;
  21. j++;
  22. }
  23. if (i == count)
  24. {
  25. r = p;
  26. while (q)
  27. {
  28. while (q->next == r)
  29. {
  30. q->next = q->next->next;
  31. free(r);
  32. cout << "删除图书信息成功!" << endl;
  33. ofstream ofs;
  34. ofs.open("text.txt", ios::out);
  35. strlonde S;
  36. S = L->next;
  37. while (S)
  38. {
  39. ofs << S->data.name << " " << S->data.num << " " << S->data.tel << " " << S->data.name1 << " " << S->data.adress << " " << endl;
  40. S = S->next;
  41. }
  42. ofs.close();
  43. return;
  44. }
  45. q = q->next;
  46. }
  47. }
  48. p = p->next;
  49. }
  50. }

插入图书信息

  1. void insert(strlonde&L, char*arr, char*arr2, char*arr3, char*arr4,char*arr5,char*arr6)
  2. { strlonde p, q, s;
  3. char a[100], b[100];
  4. int count = 0, i = 0, j = 0;
  5. s = (strlonde)malloc(sizeof(londe));
  6. strcpy_s(s->data.name, arr3);
  7. strcpy_s(s->data.num, arr2);
  8. strcpy_s(s->data.tel, arr4);
  9. strcpy_s(s->data.name1, arr5);
  10. strcpy_s(s->data.adress, arr6);
  11. strcpy_s(a, arr);
  12. while (*arr != '')
  13. {
  14. count++;
  15. arr++;
  16. }
  17. q = L;
  18. p = L->next;
  19. while (p != NULL)
  20. {
  21. strcpy_s(b, p->data.name);
  22. i = 0, j = 0;
  23. while (a[i] == b[j] && a[i] != ''&&b[j] != '')
  24. {
  25. i++;
  26. j++;
  27. }
  28. if (i == count)
  29. {
  30. while (q)
  31. {
  32. while (q->next == p)
  33. {
  34. q->next = s;
  35. s->next = p;
  36. cout << "插入成功" << endl;
  37. ofstream ofs;
  38. ofs.open("text.txt", ios::out);
  39. strlonde S;
  40. S = L->next;
  41. while (S)
  42. {
  43. ofs << S->data.name << " " << S->data.num << " " << S->data.tel << " " << S->data.name1 << " " << S->data.adress << " " << endl;
  44. S = S->next;
  45. }
  46. ofs.close();
  47. return;
  48. }
  49. q = q->next;
  50. }
  51. }
  52. p = p->next;
  53. }
  54. }

修改图书信息

  1. void modify(strlonde&L, char*arr,char*arr1,char*arr2,char*arr3,char*arr4,char*arr5)
  2. {
  3. char a[100], b[100];
  4. strcpy_s(a, arr);
  5. strlonde p;
  6. int count = 0, i = 0, j = 0;
  7. while (*arr != '')
  8. {
  9. count++;
  10. arr++;
  11. }
  12. p = L->next;
  13. while (p)
  14. {
  15. strcpy_s(b, p->data.name);
  16. i = 0, j = 0;
  17. while (a[i] == b[j] && a[i] != ''&&b[j] != '')
  18. {
  19. i++;
  20. j++;
  21. }
  22. if (i == count)
  23. {
  24. strcpy_s(p->data.num, arr1);
  25. strcpy_s(p->data.name, arr2);
  26. strcpy_s(p->data.tel, arr3);
  27. strcpy_s(p->data.name1, arr4);
  28. strcpy_s(p->data.adress, arr5);
  29. cout << "修改成功" << endl;
  30. ofstream ofs;
  31. ofs.open("text.txt", ios::out);
  32. strlonde S;
  33. S = L->next;
  34. while (S)
  35. {
  36. ofs << S->data.name << " " << S->data.num << " " << S->data.tel << " " << S->data.name1 << " " << S->data.adress << " " << endl;
  37. S = S->next;
  38. }
  39. ofs.close();
  40. return;
  41. }
  42. p = p->next;
  43. }
  44. }

switch语句实现功能

  1. switch (x)
  2. {
  3. case 1:
  4. cout << "现在开始录入信息,请输入您要录入几本书的信息" << endl;
  5. cin >> n;
  6. londecreat(L, n);
  7. cout << "录入完成" << endl;
  8. break;
  9. case 2:
  10. display(L);
  11. break;
  12. case 3:
  13. cout << "请输入您要进行查找的图书的编号" << endl;
  14. cin >> arr;
  15. findname(arr);
  16. break;
  17. case 4:
  18. cout << "请输入您要进行查找图书名" << endl;
  19. cin >> arr2;
  20. findnum(arr2);
  21. break;
  22. case 5:
  23. cout << "请输入您要删除图书的编号" << endl;
  24. cin >> arr4;
  25. delete1(L, arr4);
  26. break;
  27. case 6:
  28. cout << "请输入您要进行插入地方图书的编号以及插入的图书编号,图书名,图书作者,图书出版社,价格" << endl;
  29. cin >> arr5 >> arr6 >> arr7 >> arr8 >> arr13 >> arr14;
  30. insert(L, arr5, arr6, arr7, arr8,arr13,arr14);
  31. break;
  32. case 7:
  33. cout << "请输入您要进行修改的用户的编号以及您需要修改的信息" << endl;
  34. cin >> arr9 >> arr10 >> arr11 >> arr12 >> arr15 >> arr16;
  35. modify(L, arr9, arr10, arr11, arr12,arr15,arr16);
  36. break;
  37. default:
  38. cout << "您的输入有误,请重新输入" << endl;
  39. }
  40. }

整体代码

  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<string>
  4. #include<malloc.h>
  5. #include<stdlib.h>
  6. #include<string.h>
  7. #include<fstream>
  8. #define Max 1000
  9. using namespace std;
  10. class admin
  11. {
  12. public:
  13. char name[100];
  14. char num[100];
  15. char tel[100];
  16. char name1[100];
  17. char adress[100];
  18. };
  19. typedef struct londe
  20. {
  21. admin data;
  22. struct londe *next;
  23. }londe, *strlonde;
  24. void londecreat(strlonde&L,int n)
  25. {
  26. L = (strlonde)malloc(sizeof(londe));
  27. L->next = NULL;
  28. strlonde p, q;
  29. q = L;
  30. int i =0;
  31. cout << "请您依次输入图书编号,图书名,图书作者,图书出版社,价格" << endl;
  32. for(i=0;i<n;i++)
  33. {
  34. p= (strlonde)malloc(sizeof(londe));
  35. cin >> p->data.name;
  36. cin >> p->data.num;
  37. cin >> p->data.tel;
  38. cin >> p->data.name1;
  39. cin >> p->data.adress;
  40. p->next = NULL;
  41. q->next = p;
  42. q = p;
  43. }
  44. ofstream ofs;
  45. ofs.open("text.txt", ios::out);
  46. strlonde S;
  47. S = L->next;
  48. while (S)
  49. {
  50. ofs << S->data.name << " " << S->data.num << " " << S->data.tel << " " <<S->data.name1<<" "<<S->data.adress<<" "<< endl;
  51. S = S->next;
  52. }
  53. ofs.close();
  54. }
  55. void display(strlonde&L)
  56. {
  57. ifstream ifs;
  58. ifs.open("text.txt",ios::in);
  59. char buf[10000] = { 0 };
  60. int i = 0;
  61. cout << "下面是您录入的信息" << endl;
  62. while (ifs >> buf)
  63. {
  64. if (i % 5 == 0)
  65. {
  66. cout <<endl;
  67. }
  68. cout << buf << " ";
  69. i++;
  70. }
  71. ifs.close();
  72. }
  73. void findname(char *arr)
  74. {
  75. char a[100];
  76. strcpy_s(a, arr);
  77. ifstream ifs;
  78. ifs.open("text.txt", ios::in);
  79. char buf[1000] = { 0 };
  80. int i = 0;
  81. while (ifs >> buf)
  82. {
  83. int n = strcmp(a, buf);
  84. if (n == 0)
  85. {
  86. cout << buf << " ";
  87. i++;
  88. continue;
  89. }
  90. if (i > 5)
  91. {
  92. return;
  93. }
  94. if (i <=5 && i >=1)
  95. {
  96. cout << buf << " ";
  97. i++;
  98. }
  99. }
  100. ifs.close();
  101. }
  102. void findnum(char *arr)
  103. {
  104. char a[100];
  105. strcpy_s(a, arr);
  106. ifstream ifs;
  107. ifs.open("text.txt", ios::in);
  108. char buf[10000] = { 0 };
  109. int i = 0;
  110. while (ifs >> buf)
  111. {
  112. if (i == 3)
  113. {
  114. cout << buf << " ";
  115. return;
  116. }
  117. if (i==2)
  118. {
  119. cout << buf << " ";
  120. i++;
  121. }
  122. if (i == 1)
  123. {
  124. cout << buf << " ";
  125. i++;
  126. }
  127. int n = strcmp(a, buf);
  128. if (n == 0)
  129. {
  130. cout << buf << " ";
  131. i++;
  132. }
  133. }
  134. ifs.close();
  135. }
  136. void delete1(strlonde&L,char *arr)
  137. {
  138. char a[100], b[100];
  139. int i = 0, j = 0, count = 0;
  140. strcpy_s(a, arr);
  141. while (*arr != '')
  142. {
  143. count++;
  144. arr++;
  145. }
  146. strlonde p, q, r;
  147. q = L;
  148. p = L->next;
  149. while (p != NULL)
  150. {
  151. strcpy_s(b, p->data.name);
  152. i = 0, j = 0;
  153. while (a[i] == b[j] && a[i] != ''&&b[j] != '')
  154. {
  155. i++;
  156. j++;
  157. }
  158. if (i == count)
  159. {
  160. r = p;
  161. while (q)
  162. {
  163. while (q->next == r)
  164. {
  165. q->next = q->next->next;
  166. free(r);
  167. cout << "删除图书信息成功!" << endl;
  168. ofstream ofs;
  169. ofs.open("text.txt", ios::out);
  170. strlonde S;
  171. S = L->next;
  172. while (S)
  173. {
  174. ofs << S->data.name << " " << S->data.num << " " << S->data.tel << " " << S->data.name1 << " " << S->data.adress << " " << endl;
  175. S = S->next;
  176. }
  177. ofs.close();
  178. return;
  179. }
  180. q = q->next;
  181. }
  182. }
  183. p = p->next;
  184. }
  185. }
  186. void insert(strlonde&L, char*arr, char*arr2, char*arr3, char*arr4,char*arr5,char*arr6)
  187. { strlonde p, q, s;
  188. char a[100], b[100];
  189. int count = 0, i = 0, j = 0;
  190. s = (strlonde)malloc(sizeof(londe));
  191. strcpy_s(s->data.name, arr3);
  192. strcpy_s(s->data.num, arr2);
  193. strcpy_s(s->data.tel, arr4);
  194. strcpy_s(s->data.name1, arr5);
  195. strcpy_s(s->data.adress, arr6);
  196. strcpy_s(a, arr);
  197. while (*arr != '')
  198. {
  199. count++;
  200. arr++;
  201. }
  202. q = L;
  203. p = L->next;
  204. while (p != NULL)
  205. {
  206. strcpy_s(b, p->data.name);
  207. i = 0, j = 0;
  208. while (a[i] == b[j] && a[i] != ''&&b[j] != '')
  209. {
  210. i++;
  211. j++;
  212. }
  213. if (i == count)
  214. {
  215. while (q)
  216. {
  217. while (q->next == p)
  218. {
  219. q->next = s;
  220. s->next = p;
  221. cout << "插入成功" << endl;
  222. ofstream ofs;
  223. ofs.open("text.txt", ios::out);
  224. strlonde S;
  225. S = L->next;
  226. while (S)
  227. {
  228. ofs << S->data.name << " " << S->data.num << " " << S->data.tel << " " << S->data.name1 << " " << S->data.adress << " " << endl;
  229. S = S->next;
  230. }
  231. ofs.close();
  232. return;
  233. }
  234. q = q->next;
  235. }
  236. }
  237. p = p->next;
  238. }
  239. }
  240. void modify(strlonde&L, char*arr,char*arr1,char*arr2,char*arr3,char*arr4,char*arr5)
  241. {
  242. char a[100], b[100];
  243. strcpy_s(a, arr);
  244. strlonde p;
  245. int count = 0, i = 0, j = 0;
  246. while (*arr != '')
  247. {
  248. count++;
  249. arr++;
  250. }
  251. p = L->next;
  252. while (p)
  253. {
  254. strcpy_s(b, p->data.name);
  255. i = 0, j = 0;
  256. while (a[i] == b[j] && a[i] != ''&&b[j] != '')
  257. {
  258. i++;
  259. j++;
  260. }
  261. if (i == count)
  262. {
  263. strcpy_s(p->data.num, arr1);
  264. strcpy_s(p->data.name, arr2);
  265. strcpy_s(p->data.tel, arr3);
  266. strcpy_s(p->data.name1, arr4);
  267. strcpy_s(p->data.adress, arr5);
  268. cout << "修改成功" << endl;
  269. ofstream ofs;
  270. ofs.open("text.txt", ios::out);
  271. strlonde S;
  272. S = L->next;
  273. while (S)
  274. {
  275. ofs << S->data.name << " " << S->data.num << " " << S->data.tel << " " << S->data.name1 << " " << S->data.adress << " " << endl;
  276. S = S->next;
  277. }
  278. ofs.close();
  279. return;
  280. }
  281. p = p->next;
  282. }
  283. }
  284. void menu()
  285. {
  286. cout << " ******************************************************************" << endl;
  287. cout << " *******************欢迎光临图书管理系统***************************" << endl;
  288. cout << " **************** 1.输入-1 退出程序 *******************" << endl;
  289. cout << " **************** 2.输入1 录入信息 *******************" << endl;
  290. cout << " **************** 3.输入2 陈列信息 *******************" << endl;
  291. cout << " **************** 4.输入3 按编号查找 *******************" << endl;
  292. cout << " **************** 5.输入4 按书名查找 *******************" << endl;
  293. cout << " **************** 6.输入5 删除 *******************" << endl;
  294. cout << " **************** 7.输入6 插入 *******************" << endl;
  295. cout << " **************** 8.输入7 修改 *******************" << endl;
  296. cout << " ******************************************************************" << endl;
  297. }
  298. int main()
  299. {
  300. strlonde L;
  301. char arr[100], arr2[100], arr4[100], arr5[100], arr6[100], arr7[100], arr8[100], arr9[100], arr10[100], arr11[100], arr12[100], arr13[100], arr14[100], arr15[100], arr16[100];
  302. int n = 0;
  303. int x = 0;
  304. menu();
  305. while (cin >> x)
  306. {
  307. if (x < 0)
  308. {
  309. break;
  310. }
  311. switch (x)
  312. {
  313. case 1:
  314. cout << "现在开始录入信息,请输入您要录入几本书的信息" << endl;
  315. cin >> n;
  316. londecreat(L, n);
  317. cout << "录入完成" << endl;
  318. break;
  319. case 2:
  320. display(L);
  321. break;
  322. case 3:
  323. cout << "请输入您要进行查找的图书的编号" << endl;
  324. cin >> arr;
  325. findname(arr);
  326. break;
  327. case 4:
  328. cout << "请输入您要进行查找图书名" << endl;
  329. cin >> arr2;
  330. findnum(arr2);
  331. break;
  332. case 5:
  333. cout << "请输入您要删除图书的编号" << endl;
  334. cin >> arr4;
  335. delete1(L, arr4);
  336. break;
  337. case 6:
  338. cout << "请输入您要进行插入地方图书的编号以及插入的图书编号,图书名,图书作者,图书出版社,价格" << endl;
  339. cin >> arr5 >> arr6 >> arr7 >> arr8 >> arr13 >> arr14;
  340. insert(L, arr5, arr6, arr7, arr8,arr13,arr14);
  341. break;
  342. case 7:
  343. cout << "请输入您要进行修改的用户的编号以及您需要修改的信息" << endl;
  344. cin >> arr9 >> arr10 >> arr11 >> arr12 >> arr15 >> arr16;
  345. modify(L, arr9, arr10, arr11, arr12,arr15,arr16);
  346. break;
  347. default:
  348. cout << "您的输入有误,请重新输入" << endl;
  349. }
  350. }
  351. return 0;
  352. }

到这里,这个图书信息管理系统的基本功能就已经全部实现了,下面我们来看看运行后是什么样子:

结果展示

这里是刚刚打开时的界面

录入及陈列信息

按照编号查找

按照书名查找

根据编号删除图书

插入操作

保存到txt文件中的样子

结束语

到了这里今天的内容就已经全部的结束了,希望小张的图书信息管理系统可以给大家带来帮助,同时也希望大家可以多多支持支持小张,小张在这里提前谢谢大家啦!

标签: c++ 链表 开发语言

本文转载自: https://blog.csdn.net/m0_61083409/article/details/125398294
版权归原作者 m0_61083409 所有, 如有侵权,请联系我们删除。

“图书管理系统 (单链表实现,C++及文件操作,超详细)”的评论:

还没有评论