鱼C论坛

 找回密码
 立即注册
查看: 1701|回复: 7

这个,第一次打开,所有功能都没问题,但是,有录入数据后,就错误。不知道为什么....

[复制链接]
发表于 2022-12-10 09:50:52 | 显示全部楼层 |阅读模式
30鱼币
CKU`{(W_5CVLG@TO[Q(}873.png
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>

  4. #define MAX 1024

  5. struct Book *book_pool = NULL;//内存池
  6. int count_book = 0;
  7. struct User *user_pool = NULL;
  8. int count_user = 0;

  9. struct Date{
  10.         int year;
  11.         int month;
  12.         int day;
  13. };

  14. struct Book{
  15.         char name[40];//书名
  16.         char author[128];//作者
  17.         char publisher[40];//出版社
  18.         int exist;
  19.         struct Date date;//日期
  20.         struct Book *next;
  21. };

  22. struct User{
  23.         char name[40];//用户名
  24.         char cypher[32];//密码
  25.         int amount;//借取书本量
  26.         struct Date date;//注册日期
  27.         struct User *next;
  28. };

  29. void welcome(struct Book **book, struct User **user);//打印欢迎和使用说明
  30. void consumer(void);//打印用户使用说明
  31. void manage(void);//打印管理员使用说明
  32. int log_on(struct User *head);//登录
  33. int administrator(void);//登录管理员
  34. //用户相关的函数
  35. struct User *seek_user(struct User *head);//寻找用户,返回地址
  36. void print_user_one(struct User *head);//根据地址打印用户的信息
  37. void increase(struct User **head);//增加用户
  38. void reduce(struct User **head);//减少用户
  39. void query(struct User *book);//查询用户信息
  40. void modify(struct User *head);//修改用户信息
  41. void display(struct User *head);//显示所有用户信息
  42. //图书相关的函数
  43. struct Book *seek_book(struct Book *head);//寻找图书,返回地址
  44. void print_book_one(struct Book *head);//根据地址打印图书的信息
  45. void insert(struct Book **head);//录入新的图书
  46. void searchEvent(struct Book *book);//查找已有的图书
  47. void change(struct Book **head);//更改已有的图书
  48. void delete(struct Book **head);//删除已有的图书
  49. void print(struct Book *head);//打印所有的图书信息
  50. void enter(struct Book *book, struct User *user);//结束程序时将信息放入文件
  51. void borrow(void);//借书//
  52. void still(void);//还书//

  53. void welcome(struct Book **head_book, struct User **head_user){//打印欢迎和使用说明
  54.         FILE *fp1, *fp2;
  55.         struct Book *temp_book = NULL, *book;
  56.         struct User *temp_user = NULL, *user;

  57.         book = *head_book;
  58.         user = *head_user;

  59.         printf("| 欢迎使用图书馆程序 |\n");
  60.         printf("|------ 1:注册 ------|\n");
  61.         printf("|------ 2:登录 ------|\n");
  62.         printf("|------ 3:退出 ------|\n");
  63.         printf("|--------------------|\n");

  64.         if ((fp1 = fopen("book.txt", "rb")) == NULL){//fopen获取文件指针
  65.                 printf("打开文件失败!\n");
  66.                 exit(EXIT_FAILURE);
  67.         }
  68.         while (1){
  69.                 if (book_pool != NULL){//如果内存池非空,直接从里面获取空间
  70.                         temp_book = book_pool;
  71.                         book_pool = book_pool->next;
  72.                         count_book--;
  73.                 }
  74.                 else{//如果内存池空,使用malloc申请内存
  75.                         temp_book = (struct Book *)malloc(sizeof(struct Book));
  76.                         if(temp_book == NULL){
  77.                                 printf("内存分配失败了。\n");
  78.                                 exit(EXIT_FAILURE);
  79.                         }
  80.                 }

  81.                 fread(temp_book, sizeof(struct Book), 1, fp1);// 将文件数据读取出来
  82.                 if (feof(fp1)){
  83.                         if (temp_user == NULL){
  84.                                 free(temp_book);
  85.                         }
  86.                         break;
  87.                 }

  88.                 if (*head_book != NULL){
  89.                         temp_book = *head_book;
  90.                         *head_book = book;
  91.                         book->next = temp_book;
  92.                 }
  93.                 else{
  94.                         *head_book = book;
  95.                         book->next = NULL;
  96.                 }
  97.         }
  98.         fclose(fp1);//关闭文件!!!!
  99.         if ((fp2 = fopen("user.txt", "rb")) == NULL){//fopen获取文件指针
  100.                 printf("打开文件失败!\n");
  101.                 exit(EXIT_FAILURE);
  102.         }
  103.         while (1){
  104.                 if (user_pool != NULL){//如果内存池非空,直接从里面获取空间
  105.                         temp_user = user_pool;
  106.                         user_pool = user_pool->next;
  107.                         count_user--;
  108.                 }
  109.                 else{//如果内存池空,使用malloc申请内存
  110.                         temp_user = (struct User *)malloc(sizeof(struct User));
  111.                         if(temp_user == NULL){
  112.                                 printf("内存分配失败了。\n");
  113.                                 exit(EXIT_FAILURE);
  114.                         }
  115.                 }

  116.                 fread(temp_user, sizeof(struct User), 1, fp2);// 将文件数据读取出来
  117.                 if (feof(fp2)){
  118.                         if (temp_user == NULL){
  119.                                 free(temp_user);
  120.                         }
  121.                         break;
  122.                 }

  123.                 if (*head_user != NULL){
  124.                         temp_user = *head_user;
  125.                         *head_user = user;
  126.                         user->next = temp_user;
  127.                 }
  128.                 else{
  129.                         *head_user = user;
  130.                         user->next = NULL;
  131.                 }
  132.         }
  133.         fclose(fp2);
  134. }

  135. void consumer(void){//打印用户使用说明
  136.         printf("| 欢迎使用图书馆参观程序 |\n");
  137.         printf("|--- 1:查找已有图书 -----|\n");
  138.         printf("|--- 2:显示当前图书库 ---|\n");
  139.         printf("|--- 3:借取已有图书 -----|\n");
  140.         printf("|--- 4:归还借取图书 -----|\n");
  141.         printf("|--- 5:进入管理员模式 ---|\n");
  142.         printf("|--- 6:退出图书馆程序 ---|\n");
  143.         printf("|----------------------|\n");
  144. }

  145. void manage(void){//打印管理员使用说明
  146.         printf("| 欢迎使用图书馆管理程序 |\n");
  147.         printf("|--- 1:插入新的图书 -----|\n");
  148.         printf("|--- 2:更改已有图书 -----|\n");
  149.         printf("|--- 3:删除已有图书 -----|\n");
  150.         printf("|--- 4:显示当前图书库 ---|\n");
  151.         printf("|--- 5:更改用户信息 -----|\n");
  152.         printf("|--- 6:查询用户信息 -----|\n");
  153.         printf("|--- 7:删除用户信息 -----|\n");
  154.         printf("|--- 8:显示用户名单 -----|\n");
  155.         printf("|--- 9:退出图书馆程序 ---|\n");
  156.         printf("|----------------------|\n");
  157. }

  158. int log_on(struct User *head){//登录/
  159.         struct User *user;
  160.         char mm[32];

  161.         user = seek_user(head);
  162.         printf("请输入密码:");
  163.         scanf("%s", mm);
  164.         while (user != NULL){
  165.                 if (!strcmp(user->cypher, mm)){
  166.                         printf("登录成功");
  167.                         return 1;
  168.                 }
  169.                 printf("密码错误,登录失败");
  170.                 return 0;
  171.         }
  172.         printf("没有找到帐号,登录失败");
  173.         return 0;
  174. }

  175. int administrator(void){
  176.         char mm[32];

  177.         printf("请输入管理员密码:");
  178.         scanf("%s", mm);
  179.         if (!strcmp("123456", mm)){
  180.                 printf("登录成功");
  181.                 return 1;
  182.         }
  183.         printf("密码错误,登录失败");
  184.         return 0;
  185. }

  186. struct User *seek_user(struct User *head){//寻找用户,返回地址
  187.         struct User *target;
  188.         char name[40];

  189.         printf("请输入用户:");
  190.         scanf("%s", name);

  191.         target = head;
  192.         while (target != NULL && strcmp(target->name, name)){
  193.                 target = target->next;
  194.         }

  195.         return target;
  196. }

  197. void print_user_one(struct User *head){//根据地址打印用户的信息
  198.         printf("密码:%s\n", head->cypher);
  199.         printf("注册日期(aa-bb-cc):%d-%d-%d\n", head->date.year, head->date.month, head->date.day);
  200.         printf("借取图书 %d 本", head->amount);
  201. }

  202. void increase(struct User **head){//增加用户/
  203.         struct User *user;
  204.         struct User *temp;

  205.         if (user_pool != NULL){//如果内存池非空,直接从里面获取空间
  206.                 user = user_pool;
  207.                 user_pool = user_pool->next;
  208.                 count_user--;
  209.         }
  210.         else{//如果内存池空,使用malloc申请内存
  211.                 user = (struct User *)malloc(sizeof(struct User));
  212.                 if(user == NULL){
  213.                         printf("内存分配失败了。\n");
  214.                         exit(1);
  215.                 }
  216.         }
  217.         printf("请输入用户名:");
  218.         scanf("%s", user->name);
  219.         printf("请设置密码:");
  220.         scanf("%s", user->cypher);
  221.         printf("请输入注册日期(aa-bb-cc)");
  222.         scanf("%d-%d-%d", &user->date.year, &user->date.month, &user->date.day);
  223.         user->amount = 0;

  224.         if (*head != NULL){
  225.                 temp = *head;
  226.                 *head = user;
  227.                 user->next = temp;
  228.         }
  229.         else{
  230.                 *head = user;
  231.                 user->next = NULL;
  232.         }
  233. }

  234. void reduce(struct User **head){//减少用户
  235.         struct User *target;//目标
  236.         struct User *now;
  237.         struct User *temp;//上一次

  238.         target = seek_user(*head);// 先找到待删除的节点指针

  239.         if (target == NULL){
  240.                 printf("找不到该用户!\n");
  241.         }
  242.         else{
  243.                 now = *head;
  244.                 temp = NULL;

  245.                 while (now != NULL && now != target){// current定位到待删除的节>点
  246.                         temp = now;
  247.                         now = now->next;
  248.                 }
  249.                 if (temp == NULL){// 待删除的节点是第一个节点
  250.                         *head = now->next;
  251.                 }
  252.                 else{// 待删除的节点不是第一个节点
  253.                         temp->next = now->next;
  254.                 }

  255.                 if (count_user < MAX){//判断内存池有没有空位
  256.                         if (user_pool != NULL){
  257.                                 temp = user_pool;
  258.                                 user_pool = target;
  259.                                 target->next = NULL;
  260.                         }                        
  261.                         else{
  262.                                 user_pool = target;
  263.                                 target->next = NULL;
  264.                         }
  265.                         count_user++;
  266.                 }
  267.                 else{
  268.                         free(target);
  269.                 }
  270.         }
  271. }


  272. void query(struct User *head){//查询用户信息
  273.         struct User *user;
  274.         char name[32];

  275.         printf("请输入用户名:");
  276.         scanf("%s", name);

  277.         user = head;

  278.         while (user != NULL){
  279.                 if (!strcmp(user->name, name)){
  280.                         break;
  281.                 }
  282.                 user = user->next;
  283.         }
  284.         if(user == NULL){
  285.                 printf("抱歉,没有找到。\n");
  286.         }
  287.         else{
  288.                 print_user_one(user);
  289.         }
  290. }

  291. void modify(struct User *head){//修改用户信息/
  292.         struct User *target;

  293.         char name[32];

  294.         target = seek_user(head);// 先找到待更改的节点指针

  295.         if (target == NULL){
  296.                 printf("找不到该用户!\n");
  297.         }
  298.         else{
  299.                 printf("请设置用户名:");
  300.                 scanf("%s", target->name);
  301.               printf("请设置密码:");
  302.                 scanf("%s", target->cypher);
  303.                 printf("请设置注册日期(aa-bb-cc):");
  304.                 scanf("%d-%d-%d", &target->date.year, &target->date.month, &target->date.day);
  305.                 printf("请设置借取图书量:");
  306.                 scanf("%D", &head->amount);
  307.         }
  308. }

  309. void display(struct User *head){//显示所有用户信息/
  310.         struct User *now;

  311.         now = head;
  312.         while (now != NULL){
  313.                 printf("用户名:%s\n", now->name);
  314.                 print_user_one(now);
  315.                 now = now->next;
  316.         }
  317. }
  318. //--------------------------图书------------------------------
  319. struct Book *seek_book(struct Book *head){//寻找图书,返回地址
  320.         struct Book *target;
  321.         char name[40];
  322.    
  323.         printf("请输入书名:");
  324.         scanf("%s", name);
  325.    
  326.         target = head;
  327.         while (target != NULL && strcmp(target->name, name)){
  328.                 target = target->next;
  329.         }
  330.    
  331.         return target;
  332. }

  333. void print_one(struct Book *head){//根据地址打印图书的信息
  334.         printf("作者:%s\n", head->author);
  335.         printf("出版社:%s\n", head->publisher);
  336.         printf("日期:%d-%d-%d\n", head->date.year, head->date.month, head->date.day);
  337.         printf("书籍状态:");
  338.         switch (head->exist){
  339.                 case 1: printf("可借取\n");break;
  340.                 case 0: printf("不可借取\n");break;
  341.         }
  342. }

  343. void insert(struct Book **head){//录入新的图书
  344.         struct Book *book;
  345.         struct Book *temp;

  346.         if (book_pool != NULL){//如果内存池非空,直接从里面获取空间
  347.                 book = book_pool;
  348.                 book_pool = book_pool->next;
  349.                 count_book--;
  350.         }
  351.         else{//如果内存池空,使用malloc申请内存
  352.                 book = (struct Book *)malloc(sizeof(struct Book));
  353.                 if(book == NULL){
  354.                         printf("内存分配失败了。\n");
  355.                         exit(1);
  356.                 }
  357.         }
  358.         printf("请输入书名:");
  359.         scanf("%s", book->name);
  360.         printf("请输入作者:");
  361.         scanf("%s", book->author);
  362.         printf("请输入出版社:");
  363.         scanf("%s", book->publisher);
  364.         printf("请输入日期(aa-bb-cc):");
  365.         scanf("%d-%d-%d", &book->date.year, &book->date.month, &book->date.day);
  366.         book->exist = 1;

  367.         if (*head != NULL){
  368.                 temp = *head;
  369.                 *head = book;
  370.                 book->next = temp;
  371.         }
  372.         else{
  373.                 *head = book;
  374.                 book->next = NULL;
  375.         }
  376.         putchar('\n');
  377. }


  378. void searchEvent(struct Book *head){//查找已有的书名
  379.         struct Book *book;
  380.         char name[32];

  381.         printf("请输入书名:");
  382.         scanf("%s", name);

  383.         book = head;
  384.        
  385.         while (book != NULL){
  386.                 if (!strcmp(book->name, name)){
  387.                         break;
  388.                 }
  389.                 book = book->next;
  390.         }
  391.         if(book == NULL){
  392.                 printf("抱歉,没有找到。\n");
  393.         }
  394.         else{
  395.                 print_one(book);
  396.         }
  397. }

  398. void change(struct Book **head){//更改
  399.         struct Book *target;

  400.         char name[32];

  401.         target = seek_book(*head);// 先找到待更改的节点指针

  402.         if (target == NULL){
  403.                 printf("找不到该书名!\n");
  404.         }
  405.         else{
  406.                 printf("请输入作者:");
  407.                 scanf("%s", target->author);
  408.                 printf("请输入出版社:");
  409.                 scanf("%s", target->publisher);
  410.                 printf("请输入日期(aa-bb-cc):");
  411.                 scanf("%d-%d-%d", &target->date.year, &target->date.month, &target->date.day);
  412.                 printf("是否可借取(1/0):");
  413.                 scanf("%d", &target->exist);
  414.         }
  415. }

  416. void delete(struct Book **head){//删除
  417.         struct Book *target;//目标
  418.         struct Book *now;
  419.         struct Book *temp;//上一次

  420.         target = seek_book(*head);// 先找到待删除的节点指针

  421.         if (target == NULL){
  422.                 printf("找不到该书名!\n");
  423.         }
  424.         else{
  425.                 now = *head;
  426.                 temp = NULL;
  427.    
  428.                 while (now != NULL && now != target){// current定位到待删除的节点
  429.                         temp = now;
  430.                         now = now->next;
  431.                 }
  432.                 if (temp == NULL){// 待删除的节点是第一个节点
  433.                         *head = now->next;
  434.                 }

  435.                 else{// 待删除的节点不是第一个节点
  436.                         temp->next = now->next;
  437.                 }

  438.                 if (count_book < MAX){//判断内存池有没有空位
  439.                         if (book_pool != NULL){
  440.                                 temp = book_pool;
  441.                                 book_pool = target;
  442.                                 target->next = NULL;
  443.                         }
  444.                         else{
  445.                                 book_pool = target;
  446.                                 target->next = NULL;
  447.                         }
  448.                         count_book++;
  449.                 }
  450.                 else{
  451.                         free(target);
  452.                 }
  453.         }
  454. }

  455. void print(struct Book *head){//打印
  456.         struct Book *now;

  457.         now = head;
  458.         while (now != NULL){
  459.                 printf("书名:%s\n", now->author);
  460.                 print_one(now);
  461.                 now = now->next;
  462.         }
  463. }

  464. void enter(struct Book *book, struct User *user){//结束程序时将图书信息放入文件
  465.         struct Book *book_temp;
  466.         struct User *user_temp;
  467.         FILE *fp1, *fp2;

  468.         if ((fp1 = fopen("book.txt", "wb")) == NULL){//fopen获取文件指针
  469.                 printf("打开文件失败!\n");
  470.                 exit(EXIT_FAILURE);
  471.         }

  472.         if ((fp2 = fopen("user.txt", "wb")) == NULL){//fopen获取文件指针
  473.                 printf("打开文件失败!\n");
  474.                 exit(EXIT_FAILURE);
  475.         }

  476.         while (book != NULL){
  477.                 fwrite(book, sizeof(struct Book), 1, fp1);// 将数据写入到文件中,释放内存
  478.                 book_temp = book;
  479.                 book = book->next;
  480.                 free(book_temp);
  481.         }

  482.         while (user != NULL){
  483.                 fwrite(user, sizeof(struct User), 1, fp2);// 将数据写入到文件中,释放内存
  484.                 user_temp = user;
  485.                 user = user->next;
  486.                 free(user_temp);
  487.         }

  488.         fclose(fp1);//关闭文件!!!!
  489.         fclose(fp2);

  490.         while (user_pool != NULL){//释放内存池
  491.                 user_temp = user_pool;
  492.                 user_pool = user_pool->next;
  493.                 free(user_temp);
  494.         }
  495.         while (book_pool != NULL){//释放内存池
  496.                 book_temp = book_pool;
  497.                 book_pool = book_pool->next;
  498.                 free(book_temp);
  499.         }
  500. }

  501. int main(void){
  502.         struct Book *book = NULL;
  503.         struct User *user = NULL;
  504.         char name[32];
  505.         int i;

  506.         welcome(&book, &user);

  507.         while (1){
  508.                 printf("\n请输入指令代码:");
  509.                 scanf("%d", &i);
  510.                 switch (i){
  511.                         case 1: increase(&user);break;
  512.                         case 2: if (log_on(user)){
  513.                                         consumer();
  514.                                         while (1){
  515.                                                 printf("\n请输入指令代码:");
  516.                                                 scanf("%d", &i);
  517.                                                 switch (i){
  518.                                                         case 1: searchEvent(book);break;
  519.                                                         case 2: ;break;//
  520.                                                         case 3: ;break;//
  521.                                                         case 4: print(book);break;
  522.                                                         case 5:if (administrator()){
  523.                                                                         manage();
  524.                                                                         while (1){
  525.                                                                                 printf("\n请输入指令代码:");
  526.                                                                                 scanf("%d", &i);
  527.                                                                                 switch (i){
  528.                                                                                         case 1: insert(&book);break;
  529.                                                                                         case 2: change(&book);break;
  530.                                                                                         case 3: delete(&book);break;
  531.                                                                                         case 4: print(book);break;
  532.                                                                                         case 5: modify(user);break;
  533.                                                                                         case 6: query(user);break;
  534.                                                                                         case 7: reduce(&user);break;
  535.                                                                                         case 8: display(user);break;
  536.                                                                                         case 9: enter(book, user);exit(i);
  537.                                                                                 }
  538.                                                                         }
  539.                                                                 }
  540.                                                                 else{
  541.                                                                         printf("密码错误\n");
  542.                                                                 }
  543.                                                                 break;
  544.                                                         case 6: enter(book, user);exit(i);
  545.                                                 }
  546.                                         }
  547.                                 }
  548.                                 else{
  549.                                         printf("密码错误\n");
  550.                                 }
  551.                                 break;
  552.                         case 3: enter(book, user);exit(i);
  553.                 }
  554.         }
  555.         return 0;
  556. }
复制代码

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-12-10 16:00:40 | 显示全部楼层
完成
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>

  4. #define MAX 1024

  5. struct Book *book_pool = NULL;//内存池
  6. int count_book = 0;
  7. struct User *user_pool = NULL;
  8. int count_user = 0;

  9. struct Date{
  10.         int year;
  11.         int month;
  12.         int day;
  13. };

  14. struct Book{
  15.         char name[40];//书名
  16.         char author[128];//作者
  17.         char publisher[40];//出版社
  18.         int exist;
  19.         struct Date date;//日期
  20.         struct Book *next;
  21. };

  22. struct User{
  23.         char name[40];//用户名
  24.         char cypher[32];//密码
  25.         int amount;//借取书本量
  26.         struct Date date;//注册日期
  27.         struct User *next;
  28. };


  29. void welcome(void);//打印欢迎和使用说明
  30. void consumer(void);//打印用户使用说明
  31. void manage(void);//打印管理员使用说明
  32. int log_on(struct User *head);//登录
  33. int administrator(void);//登录管理员
  34. //用户相关的函数
  35. struct User *seek_user(struct User *head);//寻找用户,返回地址
  36. void print_user_one(struct User *head);//根据地址打印用户的信息
  37. void increase(struct User **head);//增加用户
  38. void reduce(struct User **head);//减少用户
  39. void query(struct User *book);//查询用户信息
  40. void modify(struct User *head);//修改用户信息
  41. void display(struct User *head);//显示所有用户信息
  42. //图书相关的函数
  43. struct Book *seek_book(struct Book *head);//寻找图书,返回地址
  44. void print_book_one(struct Book *head);//根据地址打印图书的信息
  45. void insert(struct Book **head);//录入新的图书
  46. void searchEvent(struct Book *book);//查找已有的图书
  47. void change(struct Book **head);//更改已有的图书
  48. void delete(struct Book **head);//删除已有的图书
  49. void print(struct Book *head);//打印所有的图书信息
  50. void enter(struct Book **book, struct User **user);//结束程序时将信息放入文件
  51. void borrow(void);//借书//
  52. void still(void);//还书//

  53. void welcome(void){//打印欢迎和使用说明
  54.         printf("| 欢迎使用图书馆程序 |\n");
  55.         printf("|------ 1:注册 ------|\n");
  56.         printf("|------ 2:登录 ------|\n");
  57.         printf("|------ 3:退出 ------|\n");
  58.         printf("|--------------------|\n");
  59. }

  60. void consumer(void){//打印用户使用说明
  61.         printf("| 欢迎使用图书馆参观程序 |\n");
  62.         printf("|--- 1:查找已有图书 -----|\n");
  63.         printf("|--- 2:显示当前图书库 ---|\n");
  64.         printf("|--- 3:借取已有图书 -----|\n");
  65.         printf("|--- 4:归还借取图书 -----|\n");
  66.         printf("|--- 5:进入管理员模式 ---|\n");
  67.         printf("|--- 6:退出图书馆程序 ---|\n");
  68.         printf("|----------------------|\n");
  69. }

  70. void manage(void){//打印管理员使用说明
  71.         printf("| 欢迎使用图书馆管理程序 |\n");
  72.         printf("|--- 1:插入新的图书 -----|\n");
  73.         printf("|--- 2:更改已有图书 -----|\n");
  74.         printf("|--- 3:删除已有图书 -----|\n");
  75.         printf("|--- 4:显示当前图书库 ---|\n");
  76.         printf("|--- 5:更改用户信息 -----|\n");
  77.         printf("|--- 6:查询用户信息 -----|\n");
  78.         printf("|--- 7:删除用户信息 -----|\n");
  79.         printf("|--- 8:显示用户名单 -----|\n");
  80.         printf("|--- 9:退出图书馆程序 ---|\n");
  81.         printf("|----------------------|\n");
  82. }

  83. int log_on(struct User *head){//登录/
  84.         struct User *user;
  85.         char mm[32];

  86.         user = seek_user(head);
  87.         printf("请输入密码:");
  88.         scanf("%s", mm);
  89.         while (user != NULL){
  90.                 if (!strcmp(user->cypher, mm)){
  91.                         printf("登录成功");
  92.                         return 1;
  93.                 }
  94.                 printf("密码错误,登录失败");
  95.                 return 0;
  96.         }
  97.         printf("没有找到帐号,登录失败");
  98.         return 0;
  99. }

  100. int administrator(void){
  101.         char mm[32];

  102.         printf("请输入管理员密码:");
  103.         scanf("%s", mm);
  104.         if (!strcmp("123456", mm)){
  105.                 printf("登录成功");
  106.                 return 1;
  107.         }
  108.         printf("密码错误,登录失败");
  109.         return 0;
  110. }

  111. struct User *seek_user(struct User *head){//寻找用户,返回地址
  112.         struct User *target;
  113.         char name[40];

  114.         printf("请输入用户:");
  115.         scanf("%s", name);

  116.         target = head;
  117.         while (target != NULL && strcmp(target->name, name)){
  118.                 target = target->next;
  119.         }

  120.         return target;
  121. }

  122. void print_user_one(struct User *head){//根据地址打印用户的信息
  123.         printf("密码:%s\n", head->cypher);
  124.         printf("注册日期(aa-bb-cc):%d-%d-%d\n", head->date.year, head->date.month, head->date.day);
  125.         printf("借取图书 %d 本", head->amount);
  126. }

  127. void increase(struct User **head){//增加用户/
  128.         struct User *user;
  129.         struct User *temp;

  130.         if (user_pool != NULL){//如果内存池非空,直接从里面获取空间
  131.                 user = user_pool;
  132.                 user_pool = user_pool->next;
  133.                 count_user--;
  134.         }
  135.         else{//如果内存池空,使用malloc申请内存
  136.                 user = (struct User *)malloc(sizeof(struct User));
  137.                 if(user == NULL){
  138.                         printf("内存分配失败了。\n");
  139.                         exit(1);
  140.                 }
  141.         }
  142.         printf("请输入用户名:");
  143.         scanf("%s", user->name);
  144.         printf("请设置密码:");
  145.         scanf("%s", user->cypher);
  146.         printf("请输入注册日期(aa-bb-cc)");
  147.         scanf("%d-%d-%d", &user->date.year, &user->date.month, &user->date.day);
  148.         user->amount = 0;

  149.         if (*head != NULL){
  150.                 temp = *head;
  151.                 *head = user;
  152.                 user->next = temp;
  153.         }
  154.         else{
  155.                 *head = user;
  156.                 user->next = NULL;
  157.         }
  158. }

  159. void reduce(struct User **head){//减少用户
  160.         struct User *target;//目标
  161.         struct User *now;
  162.         struct User *temp;//上一次

  163.         target = seek_user(*head);// 先找到待删除的节点指针

  164.         if (target == NULL){
  165.                 printf("找不到该用户!\n");
  166.         }
  167.         else{
  168.                 now = *head;
  169.                 temp = NULL;

  170.                 while (now != NULL && now != target){// current定位到待删除的节>点
  171.                         temp = now;
  172.                         now = now->next;
  173.                 }
  174.                 if (temp == NULL){// 待删除的节点是第一个节点
  175.                         *head = now->next;
  176.                 }
  177.                 else{// 待删除的节点不是第一个节点
  178.                         temp->next = now->next;
  179.                 }

  180.                 if (count_user < MAX){//判断内存池有没有空位
  181.                         if (user_pool != NULL){
  182.                                 temp = user_pool;
  183.                                 user_pool = target;
  184.                                 target->next = NULL;
  185.                         }                        
  186.                         else{
  187.                                 user_pool = target;
  188.                                 target->next = NULL;
  189.                         }
  190.                         count_user++;
  191.                 }
  192.                 else{
  193.                         free(target);
  194.                 }
  195.         }
  196. }


  197. void query(struct User *head){//查询用户信息
  198.         struct User *user;
  199.         char name[32];

  200.         printf("请输入用户名:");
  201.         scanf("%s", name);

  202.         user = head;

  203.         while (user != NULL){
  204.                 if (!strcmp(user->name, name)){
  205.                         break;
  206.                 }
  207.                 user = user->next;
  208.         }
  209.         if(user == NULL){
  210.                 printf("抱歉,没有找到。\n");
  211.         }
  212.         else{
  213.                 print_user_one(user);
  214.         }
  215. }

  216. void modify(struct User *head){//修改用户信息/
  217.         struct User *target;

  218.         char name[32];

  219.         target = seek_user(head);// 先找到待更改的节点指针

  220.         if (target == NULL){
  221.                 printf("找不到该用户!\n");
  222.         }
  223.         else{
  224.                 printf("请设置用户名:");
  225.                 scanf("%s", target->name);
  226.               printf("请设置密码:");
  227.                 scanf("%s", target->cypher);
  228.                 printf("请设置注册日期(aa-bb-cc):");
  229.                 scanf("%d-%d-%d", &target->date.year, &target->date.month, &target->date.day);
  230.                 printf("请设置借取图书量:");
  231.                 scanf("%D", &head->amount);
  232.         }
  233. }

  234. void display(struct User *head){//显示所有用户信息/
  235.         struct User *now;

  236.         now = head;
  237.         while (now != NULL){
  238.                 printf("用户名:%s\n", now->name);
  239.                 print_user_one(now);
  240.                 now = now->next;
  241.         }
  242. }
  243. //--------------------------图书------------------------------
  244. struct Book *seek_book(struct Book *head){//寻找图书,返回地址
  245.         struct Book *target;
  246.         char name[40];
  247.    
  248.         printf("请输入书名:");
  249.         scanf("%s", name);
  250.    
  251.         target = head;
  252.         while (target != NULL && strcmp(target->name, name)){
  253.                 target = target->next;
  254.         }
  255.    
  256.         return target;
  257. }

  258. void print_one(struct Book *head){//根据地址打印图书的信息
  259.         printf("作者:%s\n", head->author);
  260.         printf("出版社:%s\n", head->publisher);
  261.         printf("日期:%d-%d-%d\n", head->date.year, head->date.month, head->date.day);
  262.         printf("书籍状态:");
  263.         switch (head->exist){
  264.                 case 1: printf("可借取\n");break;
  265.                 case 0: printf("不可借取\n");break;
  266.         }
  267. }

  268. void insert(struct Book **head){//录入新的图书
  269.         struct Book *book;
  270.         struct Book *temp;

  271.         if (book_pool != NULL){//如果内存池非空,直接从里面获取空间
  272.                 book = book_pool;
  273.                 book_pool = book_pool->next;
  274.                 count_book--;
  275.         }
  276.         else{//如果内存池空,使用malloc申请内存
  277.                 book = (struct Book *)malloc(sizeof(struct Book));
  278.                 if(book == NULL){
  279.                         printf("内存分配失败了。\n");
  280.                         exit(1);
  281.                 }
  282.         }
  283.         printf("请输入书名:");
  284.         scanf("%s", book->name);
  285.         printf("请输入作者:");
  286.         scanf("%s", book->author);
  287.         printf("请输入出版社:");
  288.         scanf("%s", book->publisher);
  289.         printf("请输入日期(aa-bb-cc):");
  290.         scanf("%d-%d-%d", &book->date.year, &book->date.month, &book->date.day);
  291.         book->exist = 1;

  292.         if (*head != NULL){
  293.                 temp = *head;
  294.                 *head = book;
  295.                 book->next = temp;
  296.         }
  297.         else{
  298.                 *head = book;
  299.                 book->next = NULL;
  300.         }
  301.         putchar('\n');
  302. }


  303. void searchEvent(struct Book *head){//查找已有的书名
  304.         struct Book *book;
  305.         char name[32];

  306.         printf("请输入书名:");
  307.         scanf("%s", name);

  308.         book = head;
  309.        
  310.         while (book != NULL){
  311.                 if (!strcmp(book->name, name)){
  312.                         break;
  313.                 }
  314.                 book = book->next;
  315.         }
  316.         if(book == NULL){
  317.                 printf("抱歉,没有找到。\n");
  318.         }
  319.         else{
  320.                 print_one(book);
  321.         }
  322. }

  323. void change(struct Book **head){//更改
  324.         struct Book *target;

  325.         char name[32];

  326.         target = seek_book(*head);// 先找到待更改的节点指针

  327.         if (target == NULL){
  328.                 printf("找不到该书名!\n");
  329.         }
  330.         else{
  331.                 printf("请输入作者:");
  332.                 scanf("%s", target->author);
  333.                 printf("请输入出版社:");
  334.                 scanf("%s", target->publisher);
  335.                 printf("请输入日期(aa-bb-cc):");
  336.                 scanf("%d-%d-%d", &target->date.year, &target->date.month, &target->date.day);
  337.                 printf("是否可借取(1/0):");
  338.                 scanf("%d", &target->exist);
  339.         }
  340. }

  341. void delete(struct Book **head){//删除
  342.         struct Book *target;//目标
  343.         struct Book *now;
  344.         struct Book *temp;//上一次

  345.         target = seek_book(*head);// 先找到待删除的节点指针

  346.         if (target == NULL){
  347.                 printf("找不到该书名!\n");
  348.         }
  349.         else{
  350.                 now = *head;
  351.                 temp = NULL;
  352.    
  353.                 while (now != NULL && now != target){// current定位到待删除的节点
  354.                         temp = now;
  355.                         now = now->next;
  356.                 }
  357.                 if (temp == NULL){// 待删除的节点是第一个节点
  358.                         *head = now->next;
  359.                 }

  360.                 else{// 待删除的节点不是第一个节点
  361.                         temp->next = now->next;
  362.                 }

  363.                 if (count_book < MAX){//判断内存池有没有空位
  364.                         if (book_pool != NULL){
  365.                                 temp = book_pool;
  366.                                 book_pool = target;
  367.                                 target->next = NULL;
  368.                         }
  369.                         else{
  370.                                 book_pool = target;
  371.                                 target->next = NULL;
  372.                         }
  373.                         count_book++;
  374.                 }
  375.                 else{
  376.                         free(target);
  377.                 }
  378.         }
  379. }

  380. void print(struct Book *head){//打印
  381.         struct Book *now;

  382.         now = head;
  383.         while (now != NULL){
  384.                 printf("书名:%s\n", now->author);
  385.                 print_one(now);
  386.                 now = now->next;
  387.         }
  388. }

  389. void enter(struct Book **book, struct User **user){//结束程序时将图书信息放入文件
  390.         struct Book *book_temp;
  391.         struct User *user_temp;
  392.         FILE *fp1, *fp2;

  393.         if ((fp1 = fopen("book.txt", "wb")) == NULL){//fopen获取文件指针
  394.                 printf("打开文件失败!\n");
  395.                 exit(EXIT_FAILURE);
  396.         }

  397.         if ((fp2 = fopen("user.txt", "wb")) == NULL){//fopen获取文件指针
  398.                 printf("打开文件失败!\n");
  399.                 exit(EXIT_FAILURE);
  400.         }

  401.         while (*book != NULL){
  402.                 fwrite(*book, sizeof(struct Book), 1, fp1);// 将数据写入到文件中,释放内存
  403.                 book_temp = *book;
  404.                 *book = (*book)->next;
  405.                 free(book_temp);
  406.         }

  407.         while (*user != NULL){
  408.                 fwrite(*user, sizeof(struct User), 1, fp2);// 将数据写入到文件中,释放内存
  409.                 user_temp = *user;
  410.                 *user = (*user)->next;
  411.                 free(user_temp);
  412.         }

  413.         fclose(fp1);//关闭文件!!!!
  414.         fclose(fp2);

  415.         while (user_pool != NULL){//释放内存池
  416.                 user_temp = user_pool;
  417.                 user_pool = user_pool->next;
  418.                 free(user_temp);
  419.         }
  420.         while (book_pool != NULL){//释放内存池
  421.                 book_temp = book_pool;
  422.                 book_pool = book_pool->next;
  423.                 free(book_temp);
  424.         }
  425. }

  426. int main(void){
  427.         FILE *fp1,*fp2;
  428.         struct Book *book, *temp_book, *head_book = NULL;
  429.         struct User *user, *temp_user, *head_user = NULL;
  430.         char name[32];
  431.         int i;

  432.         welcome();
  433.         //读取文件//
  434.         if ((fp1 = fopen("user.txt", "rb")) == NULL){//fopen获取文件指针
  435.                 printf("打开文件失败!\n");
  436.                 exit(EXIT_FAILURE);
  437.         }
  438.         while (1){       
  439.                 if (user_pool != NULL){//如果内存池非空,直接从里面获取空间
  440.                         temp_user = user_pool;
  441.                         user_pool = user_pool->next;
  442.                         count_user--;
  443.                 }
  444.                 else{//如果内存池空,使用malloc申请内存
  445.                         user = (struct User *)malloc(sizeof(struct User));
  446.                         if(user == NULL){
  447.                                 printf("内存分配失败了。\n");
  448.                                 exit(EXIT_FAILURE);
  449.                         }
  450.                 }
  451.                 if (fread(user, sizeof(struct User), 1, fp1) == 0){
  452.                         free(user);
  453.                         break;
  454.                 }
  455.                 if (head_user != NULL){
  456.                         temp_user = head_user;
  457.                         head_user = user;
  458.                         user->next = temp_user;
  459.                 }
  460.                 else{
  461.                         head_user = user;
  462.                         user->next = NULL;
  463.                 }
  464.         }
  465.         fclose(fp1);

  466.         if ((fp2 = fopen("book.txt", "rb")) == NULL){//fopen获取文件指针
  467.                 printf("打开文件失败!\n");
  468.                 exit(EXIT_FAILURE);
  469.         }
  470.         while (1){       
  471.                 if (book_pool != NULL){//如果内存池非空,直接从里面获取空间
  472.                         temp_book = book_pool;
  473.                         book_pool = book_pool->next;
  474.                         count_book--;
  475.                 }
  476.                 else{//如果内存池空,使用malloc申请内存
  477.                         book = (struct Book *)malloc(sizeof(struct Book));
  478.                         if(book == NULL){
  479.                                 printf("内存分配失败了。\n");
  480.                                 exit(EXIT_FAILURE);
  481.                         }
  482.                 }
  483.                 if (fread(book, sizeof(struct Book), 1, fp2) == 0){
  484.                         free(book);
  485.                         break;
  486.                 }
  487.                 if (head_book != NULL){
  488.                         temp_book = head_book;
  489.                         head_book = book;
  490.                         book->next = temp_book;
  491.                 }
  492.                 else{
  493.                         head_book = book;
  494.                         book->next = NULL;
  495.                 }
  496.         }
  497.         fclose(fp1);

  498.         //读取完成//
  499.         while (1){
  500.                 printf("\n请输入指令代码:");
  501.                 scanf("%d", &i);
  502.                 switch (i){
  503.                         case 1: increase(&head_user);break;
  504.                         case 2: if (log_on(head_user)){
  505.                                         consumer();
  506.                                         while (1){
  507.                                                 printf("\n请输入指令代码:");
  508.                                                 scanf("%d", &i);
  509.                                                 switch (i){
  510.                                                         case 1: searchEvent(head_book);break;
  511.                                                         case 2: ;break;//
  512.                                                         case 3: ;break;//
  513.                                                         case 4: print(book);break;
  514.                                                         case 5:if (administrator()){
  515.                                                                         manage();
  516.                                                                         while (1){
  517.                                                                                 printf("\n请输入指令代码:");
  518.                                                                                 scanf("%d", &i);
  519.                                                                                 switch (i){
  520.                                                                                         case 1: insert(&head_book);break;
  521.                                                                                         case 2: change(&head_book);break;
  522.                                                                                         case 3: delete(&head_book);break;
  523.                                                                                         case 4: print(head_book);break;
  524.                                                                                         case 5: modify(head_user);break;
  525.                                                                                         case 6: query(head_user);break;
  526.                                                                                         case 7: reduce(&head_user);break;
  527.                                                                                         case 8: display(head_user);break;
  528.                                                                                         case 9: enter(&head_book, &head_user);exit(i);
  529.                                                                                 }
  530.                                                                         }
  531.                                                                 }
  532.                                                                 else{
  533.                                                                         printf("密码错误\n");
  534.                                                                 }
  535.                                                                 break;
  536.                                                         case 6: enter(&head_book, &head_user);exit(i);
  537.                                                 }
  538.                                         }
  539.                                 }
  540.                                 else{
  541.                                         printf("密码错误\n");
  542.                                 }
  543.                                 break;
  544.                         case 3: enter(&head_book, &head_user);exit(i);
  545.                 }
  546.         }
  547.         return 0;
  548. }
复制代码
了,有一个问题,为什么在函数里不行,一模一样的搬到int main()里面就可以了? 1VV@~AI}QCL~6TVYWMJ_LVM.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-12-10 13:00:25 | 显示全部楼层
你没有考虑过第一次启动吗?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-12-10 14:33:46 | 显示全部楼层
jhq999 发表于 2022-12-10 13:00
你没有考虑过第一次启动吗?

第一次可以呀。唔文件已经创建了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-12-10 14:35:32 | 显示全部楼层
想要问一下[attachimg]163155[/attachim
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>

  4. struct User *user_pool = NULL;
  5. int count_user = 0;

  6. struct Date{
  7.         int year;
  8.         int month;
  9.         int day;
  10. };

  11. struct User{
  12.         char name[40];//用户名
  13.         char cypher[32];//密码
  14.         int amount;//借取书本量
  15.         struct Date date;//注册日期
  16.         struct User *next;
  17. };

  18. struct User *read(FILE *fp1, struct User **head_user);

  19. struct User *read(FILE *fp1, struct User **head_user){       
  20.         struct User *temp_user;
  21.         struct User *user;

  22.         if (user_pool != NULL){//如果内存池非空,直接从里面获取空间
  23.                 temp_user = user_pool;
  24.                 user_pool = user_pool->next;
  25.                 count_user--;
  26.         }
  27.         else{//如果内存池空,使用malloc申请内存
  28.                 user = (struct User *)malloc(sizeof(struct User));
  29.                 if(user == NULL){
  30.                         printf("内存分配失败了。\n");
  31.                         exit(EXIT_FAILURE);
  32.                 }
  33.         }

  34.         if (fread(user, sizeof(struct User), 1, fp1) == 0){// 将文件数据读取出来
  35.                 free(user);
  36.         }
  37.         if (*head_user != NULL){
  38.                 temp_user = *head_user;
  39.                 *head_user = user;
  40.                 user->next = temp_user;
  41.         }
  42.         else{
  43.                 *head_user = user;
  44.                 user->next = NULL;
  45.         }

  46.         return user;
  47. }

  48. int main(){
  49.         FILE *fp1;
  50.         struct User **head_user;
  51.         struct User *temp_user;       
  52.         struct User *user;

  53.         *head_user = NULL;

  54.         if ((fp1 = fopen("user.txt", "rb")) == NULL){//fopen获取文件指针
  55.                 printf("打开文件失败!\n");
  56.                 exit(EXIT_FAILURE);
  57.         }

  58.         //user = read(fp1, head_user);
  59.                
  60.         if (user_pool != NULL){//如果内存池非空,直接从里面获取空间
  61.                 temp_user = user_pool;
  62.                 user_pool = user_pool->next;
  63.                 count_user--;
  64.         }
  65.         else{//如果内存池空,使用malloc申请内存
  66.                 user = (struct User *)malloc(sizeof(struct User));
  67.                 if(user == NULL){
  68.                         printf("内存分配失败了。\n");
  69.                         exit(EXIT_FAILURE);
  70.                 }
  71.         }

  72.         if (fread(user, sizeof(struct User), 1, fp1) == 0){// 将文件数据读取出来
  73.                 free(user);
  74.         }
  75.         if (*head_user != NULL){
  76.                 temp_user = *head_user;
  77.                 *head_user = user;
  78.                 user->next = temp_user;
  79.         }
  80.         else{
  81.                 *head_user = user;
  82.                 user->next = NULL;
  83.         }

  84.         printf("用户名:%s\n", user->name);
  85.         printf("密码:%s\n", user->cypher);
  86.         printf("注册日期:%d-%d-%d\n", user->date.year, user->date.month, user->date.day);
  87.         printf("借取图书 %d 本\n", user->amount);

  88.         fclose(fp1);
  89.         return 0;
  90. }
复制代码
g]这个为什么放到函数里面就不行了?
M{IS(WU`3H~KTX)DYU3~AKO.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-12-10 14:46:11 | 显示全部楼层
jhq999 发表于 2022-12-10 13:00
你没有考虑过第一次启动吗?

文件没创建的确不行,但是这个不难,可以后面加。和现在的问题没有关系
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-12-10 15:15:07 | 显示全部楼层
御坂19090 发表于 2022-12-10 14:46
文件没创建的确不行,但是这个不难,可以后面加。和现在的问题没有关系

没多少人有时间用眼睛给你找错,把book.txt上传上来
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-12-10 15:31:01 | 显示全部楼层
jhq999 发表于 2022-12-10 15:15
没多少人有时间用眼睛给你找错,把book.txt上传上来

唔,里面没有东西。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-30 21:54

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表