鱼C论坛

 找回密码
 立即注册
查看: 1370|回复: 1

[已解决]C语言

[复制链接]
发表于 2023-11-8 11:42:09 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5. #pragma warning(disable: 4996)

  6. struct Node1 //显示房间信息Room
  7. {
  8.     int roomnumber;
  9.     int price;
  10.     int available; // 0表示不可用,1表示可用
  11.     int roomtype;
  12.     Node1* next;
  13. };
  14. struct Node2 //显示当前顾客信息Guest
  15. {
  16.      char name[50];
  17.      char idnumber[20];
  18.      char telenumber[20];
  19.      char address[100];
  20.      int roomnumber;
  21.      time_t intime;  // 入住时间
  22.      Node2* next;
  23. };
  24. struct Node3 //过往账单信息
  25. {
  26.      char name[50];
  27.      char idnumber[20];
  28.      char telenumber[20];
  29.      char address[100];
  30.      int roomnumber;
  31.      int price;
  32.      time_t intime;  // 入住时间
  33.      time_t outtime; // 结账时间
  34.      Node3* next;
  35. };
  36. Node1* createrooom(int roomnumber, int price, int available,int roomtype)//为initilize创建每个房间所准备
  37. {   
  38.     Node1* newRoom = (Node1*)malloc(sizeof(Node1));
  39.     newRoom->roomnumber = roomnumber;
  40.     newRoom->price = price;
  41.     newRoom->available = available;
  42.     newRoom->roomtype = roomtype;
  43.     newRoom->next = NULL;
  44.     return newRoom;
  45. }
  46. Node1* initialize()
  47. {
  48.     Node1* rooms = NULL;
  49.     int roomcount;
  50.     printf("\n请输入总房间数目:");
  51.     scanf_s("%d", &roomcount);
  52.     int type1number, type2number;
  53.     printf("\n请输入单人间数:");
  54.     scanf_s("%d", &type1number);
  55.     printf("\n请输入双人间数:");
  56.     scanf_s("%d", &type2number);
  57.     printf("\n请输入单人间价格:");
  58.     int type1price, type2price, type3price;
  59.     scanf_s("%d", &type1price);
  60.     printf("\n请输入双人间价格:");
  61.     scanf_s("%d", &type2price);
  62.     printf("\n请输入三人间价格:");
  63.     scanf_s("%d", &type3price);
  64.     int roomtype1;
  65.     int price1;
  66.     int roomtype2;
  67.     int price2;
  68.     int roomtype3;
  69.     int price3;

  70.     for (int i = 1; i <= type1number; i++)
  71.     {
  72.         price1 = type1price;
  73.         int available = 1;//把所有房间初始状态设置为可用
  74.         roomtype1 = 1;
  75.         Node1* newroom1 = createrooom(i, price1, available,roomtype1);
  76.         newroom1->next = rooms;
  77.         rooms = newroom1; //前移,到表头;
  78.     }
  79.     for (int j = type1number + 1; j <= type2number+type1number; j++)
  80.     {
  81.         price2 = type2price;
  82.         int available = 1;//把所有房间初始状态设置为可用
  83.         roomtype2 = 2;
  84.         Node1* newroom2 = createrooom(j, price2, available, roomtype2);
  85.         newroom2->next = rooms;
  86.         rooms = newroom2; //前移,到表头;
  87.     }
  88.     for (int k = type2number+type1number + 1; k <= roomcount; k++)
  89.     {
  90.         price3 = type3price;
  91.         int available = 1;//把所有房间初始状态设置为可用
  92.         roomtype3 =3;
  93.         Node1* newroom3 = createrooom(k, price3, available,roomtype3);
  94.         newroom3->next = rooms;
  95.         rooms = newroom3; //前移,到表头;
  96.     }
  97.     Node1* p1 = rooms;
  98.     while (p1)
  99.     {
  100.         printf("%d %d %d \n", p1->roomtype, p1->roomnumber,p1->price);
  101.         p1 = p1->next;
  102.     }
  103.     return rooms;
  104.    
  105. }
  106. void update1( Node1* rooms, Node2* guests)//为录入新用户做准备
  107. {
  108.     while (guests)
  109.     {
  110.         int roomnumber = guests->roomnumber;
  111.         Node1* room = rooms;
  112.         while (room)
  113.         {
  114.             if (room->roomnumber == roomnumber)//
  115.             {
  116.                 room->available = 0;
  117.                 break;
  118.             }
  119.             room = room->next;
  120.         }
  121.         guests = guests->next;
  122.     }
  123. }
  124. void update2(Node1* rooms)//为录入新用户做准备
  125. {
  126.     Node1* p1 = rooms;
  127.     while (p1)
  128.     {
  129.         p1->available = 1;
  130.         p1 = p1->next;
  131.     }
  132.    
  133. }

  134. void distribute(Node1*rooms, Node2** guests)//为新用户分配房间
  135. {
  136.     Node2* guest = (Node2*)malloc(sizeof(Node2));
  137.     printf("\n请输入住户姓名: ");
  138.     scanf_s("%s",guest->name,50);
  139.     printf("\n请输入证件号码: ");
  140.     scanf_s("%s", guest->idnumber,20);
  141.     printf("\n请输入电话号码: ");
  142.     scanf_s("%s", guest->telenumber,20);
  143.     printf("\n请输入居住地: ");
  144.     scanf_s("%s", guest->address,100);
  145.     int roomtype;
  146.     printf("\n请选择房间类型:\n1. 单人间;\n 2. 双人间\n 3. 三人间 ");
  147.     scanf_s("%d", &roomtype);
  148.     if (roomtype < 1 || roomtype > 3)
  149.     {
  150.         printf("请问大哥你想住几人间?\n");
  151.         return;
  152.     }
  153.     Node1* selectedroom = NULL;
  154.     Node1* room = rooms;
  155.     while (room != NULL)
  156.     {
  157.         if (roomtype == room->roomtype&&room->available==1)
  158.         {
  159.             selectedroom = room;
  160.             room->available = 0;
  161.             break;
  162.         }
  163.         room = room->next;
  164.     }
  165.     guest->roomnumber = selectedroom->roomnumber;
  166.     time(&guest->intime);//从计算机获取时间
  167.     struct tm Tm;
  168.     guest->next = *guests;
  169.     *guests = guest;
  170.     printf("\n入住办理成功!");
  171.     return;
  172. }

  173. void Seller(Node2**guests,Node1*rooms,Node3**histories)
  174. {
  175.     if (*guests == NULL) {
  176.         printf("当前没有客人入住。\n");
  177.         return;
  178.     }

  179.     int roomnumber;
  180.     printf("请输入要退房的房间号: ");
  181.     scanf("%d", &roomnumber);

  182.     Node2* p1 = *guests;
  183.     Node2* p2 = NULL;
  184.     struct tm Tm1;
  185.     tm* p = &Tm1;
  186.     time_t outtime;
  187.     int price;
  188.     int totalprice;
  189.     int livedtime;
  190.     while (p1 != NULL)
  191.     {
  192.         if (p1->roomnumber == roomnumber)
  193.         {
  194.             Node1* room = rooms;
  195.             while (room!=NULL)
  196.             {
  197.                 if (room->roomnumber == roomnumber)
  198.                 {
  199.                     price = room->price;
  200.                     break;
  201.                 }
  202.                 room = room->next;
  203.             }
  204.             time(&outtime);
  205.             livedtime = difftime(outtime, p1->intime)/(3600*24);
  206.             if (livedtime < 1) livedtime = 1;
  207.             totalprice = livedtime * price;
  208.            Node3* history = (Node3*)malloc(sizeof(history));
  209.             strcpy(history->name, p1->name);
  210.             strcpy(history->idnumber, p1->idnumber);
  211.             history->roomnumber = p1->roomnumber;
  212.             history->price = price;
  213.            history->intime = p1->intime;
  214.            history->outtime = outtime;
  215.             
  216.             history->next = *histories;
  217.             *histories = history;

  218.             printf(" 退房成功。\n");
  219.             printf("入住时间: %s", ctime(&p1->intime));
  220.             printf("退房时间: %s", ctime(&outtime));
  221.             printf("应付: %d 元\n", totalprice);

  222.             if (p2 == NULL) {
  223.                 //移动指针尾部添加
  224.                 *guests = p1->next;
  225.             }
  226.             else {
  227.                 p2->next = p1->next;
  228.             }
  229.             // 更新状态
  230.             Node1* room2 = rooms;
  231.             while (room2 != NULL) {
  232.                 if (room2->roomnumber == roomnumber) {
  233.                     room2->available = 1; // 设置为可用
  234.                     break;
  235.                 }
  236.                 room2 = room2->next;
  237.             }
  238.             
  239.             return;
  240.         }
  241.         p2 = p1;
  242.         p1 = p1->next;
  243.     }

  244.     printf("未找到该房间号的客人。请检查输入的房间号。\n");
  245.    
  246.    
  247. }

  248. void saveguests(Node2* guests)
  249. {
  250.     FILE* fp;
  251.     char fn[50];
  252.     printf("\n请输入保存当前住户信息的文件名:(含路径)");
  253.     scanf_s("%s", fn, 50);
  254.     fopen_s(&fp, fn, "w+");
  255.     if (!fp)

  256.     {
  257.         printf("打开文件出错,请检查文件名是否正确。");
  258.     }
  259.     while (guests)
  260.     {
  261.         fprintf(fp, "%s %s %s %s %d %ld \n", guests->name, guests->idnumber, guests->telenumber, guests->address, guests->roomnumber, guests->intime);
  262.         guests = guests->next;
  263.     }
  264.     fclose(fp);
  265. }
  266. void takeguests(Node2** guests)
  267. {
  268.     FILE* fp;
  269.     char fn[50];
  270.     printf("\n请输入保存当前住户信息的文件名:(含路径)");
  271.     scanf_s("%s", fn, 50);
  272.     fopen_s(&fp, fn, "r");
  273.     if (!fp)

  274.     {
  275.         printf("打开文件出错,请检查文件名是否正确。");
  276.         return;
  277.     }
  278.   
  279.     while (1)//一直读,读到文件结尾
  280.     {
  281.         Node2* guest = (Node2*)malloc(sizeof(Node2));
  282.         int result = fscanf(fp, "%s %s %s %s %d %ld ", guest->name, guest->idnumber, guest->telenumber, guest->address, &guest->roomnumber, &guest->intime);
  283.         if (result != 6)
  284.         {
  285.             free(guest);
  286.             break;
  287.         }
  288.         guest->next = *guests;//guests本身就是NULL
  289.         *guests = guest;//前移,做表头
  290.     }
  291.     fclose(fp);
  292. }
  293. void savehistory(Node3* history)
  294. {
  295.     FILE* fp;
  296.     char fn[50];
  297.     printf("请输入保存历史客户信息的文件名:(含路径)");
  298.     scanf_s("%s", fn, 50);
  299.     fopen_s(&fp, fn, "w+");
  300.     if (!fp)

  301.     {
  302.         printf("打开文件出错,请检查文件名是否正确。");
  303.         return;
  304.     }
  305.     while (history)
  306.     {
  307.         fprintf(fp, "%s %s %s %d %d %ld %ld\n", history->name, history->idnumber, history->address, history->roomnumber, history->price, history->intime, history->outtime);
  308.         history = history->next;
  309.     }
  310.     fclose(fp);
  311. }
  312. void takehistory(Node3** histories)
  313. {
  314.     FILE* fp;
  315.     char fn[50];
  316.     printf("\n请输入保存历史客户信息的文件名:(含路径)");
  317.     scanf_s("%s", fn, 50);
  318.     fopen_s(&fp, fn, "r");
  319.     if (!fp)

  320.     {
  321.         printf("打开文件出错,请检查文件名是否正确。");
  322.         return;
  323.     }
  324.     while (1)
  325.     {
  326.         Node3* history = (Node3*)malloc(sizeof(Node3));
  327.         int result = fscanf(fp, "%s %s %s %d %d %ld %ld", history->name,history->idnumber,history->address, &history->roomnumber, &history->price, &history->intime, &history->outtime);
  328.         if (result != 7)
  329.         {
  330.             free(history);
  331.             break;
  332.         }
  333.         history->next = *histories;
  334.         *histories = history;
  335.     }
  336.     fclose(fp);
  337. }
  338. void namesearch(Node2* guests)
  339. {
  340.     char name[50];
  341.     printf("请输入住户姓名: ");
  342.     scanf_s("%s", name,50);

  343.     while (guests) {
  344.         if (strcmp(guests->name, name) == 0)
  345.         {
  346.             printf("姓名: %s\n", guests->name);
  347.             printf("证件号码: %s\n", guests->idnumber);
  348.             printf("联系电话: %s\n", guests->telenumber);
  349.             printf("居住地: %s\n", guests->address);
  350.             printf("房间号: %d\n", guests->roomnumber);
  351.             printf("入住时间: %s", ctime(&guests->intime));
  352.         }
  353.         guests = guests->next;
  354.     }
  355. }
  356. void historynamesearch(Node3* histories)
  357. {
  358.     char name[50];
  359.     printf("请输入住户姓名: ");
  360.     scanf_s("%s", name,50);

  361.     while (histories) {
  362.         if (strcmp(histories->name, name) == 0)
  363.         {
  364.             printf("姓名: %s\n",histories->name);
  365.             printf("证件号码: %s\n", histories->idnumber);
  366.             printf("联系电话: %s\n", histories->telenumber);
  367.             printf("居住地: %s\n", histories->address);
  368.             printf("房间号: %d\n", histories->roomnumber);
  369.             printf("入住时间: %s", ctime(&histories->intime));
  370.             printf("退房时间:%s",ctime(&histories->outtime));
  371.         }
  372.         histories = histories->next;
  373.     }
  374. }
  375. void roomnumbersearch(Node2* guests)
  376. {
  377.     int roomnumber;
  378.     printf("请输入房间号: ");
  379.     scanf_s("%d", &roomnumber);
  380.     while (guests)
  381.     {
  382.         if (guests->roomnumber == roomnumber)
  383.         {
  384.             printf("姓名: %s\n", guests->name);
  385.             printf("证件号码: %s\n", guests->idnumber);
  386.             printf("电话: %s\n", guests->telenumber);
  387.             printf("居住地: %s\n", guests->address);
  388.             printf("房间号: %d\n", guests->roomnumber);
  389.             printf("入住时间: %s", ctime(&guests->intime));
  390.             
  391.         }
  392.         guests = guests->next;
  393.     }
  394. }
  395. void historyroomnumbersearch(Node3* histories)
  396. {
  397.     int roomnumber;
  398.     printf("请输入房间号: ");
  399.     scanf_s("%d", &roomnumber);
  400.     while (histories)
  401.     {
  402.         if (histories->roomnumber == roomnumber)
  403.         {
  404.             printf("姓名: %s\n", histories->name);
  405.             printf("证件号码: %s\n", histories->idnumber);
  406.             printf("电话: %s\n", histories->telenumber);
  407.             printf("居住地: %s\n", histories->address);
  408.             printf("房间号: %d\n", histories->roomnumber);
  409.             printf("入住时间: %s", ctime(&histories->intime));
  410.             printf("退房时间:%s", ctime(&histories->outtime));
  411.         }
  412.         histories = histories->next;
  413.     }
  414. }

  415. void historytimesearch(Node3* histories)
  416. {
  417.     time_t time1, time2;
  418.     struct tm tm1, tm2;;
  419.     printf("\n请输入查询起始时间:");
  420.     printf("\n年:");
  421.     scanf_s("%d", &tm1.tm_year);
  422.     tm1.tm_year = tm1.tm_year - 1900;//tm结构体中的tm_year是从1900年开始计算的
  423.     printf("\n月:");
  424.     scanf_s("%d", &tm1.tm_mon);
  425.     tm1.tm_mon = tm1.tm_mon - 1;//tm结构体中的tm_mon的范围是0-11
  426.     printf("\n日:");
  427.     scanf_s("%d", &tm1.tm_mday);
  428.     printf("\n时:");
  429.     scanf_s("%d", &tm1.tm_hour);
  430.     printf("\n分:");
  431.     scanf_s("%d", &tm1.tm_min);
  432.     printf("\n秒:");
  433.     scanf_s("%d", &tm1.tm_sec);
  434.     time1 = mktime(&tm1);//将time_t结构体中的本地时间换算为自1970年1月1日起到现在的秒数,也叫UTC时间
  435.     printf("\n请输入查询结束时间:");
  436.     printf("\n年:");
  437.     scanf_s("%d", &tm2.tm_year);
  438.     tm1.tm_year = tm2.tm_year - 1900;//tm结构体中的tm_year是从1900年开始计算的
  439.     printf("\n月:");
  440.     scanf_s("%d", &tm2.tm_mon);
  441.     tm1.tm_mon = tm2.tm_mon - 1;//tm结构体中的tm_mon的范围是0-11
  442.     printf("\n日:");
  443.     scanf_s("%d", &tm2.tm_mday);
  444.     printf("\n时:");
  445.     scanf_s("%d", &tm2.tm_hour);
  446.     printf("\n分:");
  447.     scanf_s("%d", &tm2.tm_min);
  448.     printf("\n秒:");
  449.     scanf_s("%d", &tm2.tm_sec);
  450.     time2 = mktime(&tm2);//将time_t结构体中的本地时间换算为自1970年1月1日起到现在的秒数,也叫UTC时间
  451.     printf("\n查询结果如下:");

  452.     int a = 0;//用a来表示查找状态

  453.     while (histories)
  454.     {

  455.         if (histories->intime > time1 && histories->outtime < time2)
  456.         {
  457.             printf("姓名: %s\n", histories->name);
  458.             printf("证件号码: %s\n", histories->idnumber);
  459.             printf("联系电话: %s\n", histories->telenumber);
  460.             printf("居住地: %s\n", histories->address);
  461.             printf("房间号: %d\n", histories->roomnumber);
  462.             printf("入住时间: %s", ctime(&histories->intime));
  463.             printf("退房时间:%s", ctime(&histories->outtime));
  464.             a = 1;
  465.         }
  466.         histories = histories->next;

  467.     }
  468.     if (a == 0)
  469.     {
  470.         printf("\n该时间段内没有人入住");
  471.     }
  472. }
  473. void search(Node2* guests, Node3* histories)
  474. {
  475.     printf("\n****************************************");
  476.     printf("\n****************************************");
  477.     printf("\n**------------查询---------------------**");
  478.     printf("\n**--------1.当前客户姓名查询------------**");
  479.     printf("\n**--------2.当前客户房号查询------------**");
  480.     printf("\n**--------3.历史客户姓名查询------------**");
  481.     printf("\n**--------4.历史客户房号查询------------**");
  482.     printf("\n**--------5.历史客户时段查询------------**");
  483.     printf("\n****************************************");
  484.     printf("\n****************************************\n");
  485.     int a;
  486.     scanf_s("%d", &a);
  487.     switch (a)
  488.     {
  489.     case 1:namesearch(guests); break;
  490.     case 2:roomnumbersearch(guests); break;
  491.     case 3:historynamesearch(histories); break;
  492.     case 4:historyroomnumbersearch(histories); break;
  493.     case 5:historytimesearch(histories); break;

  494.     default:
  495.     {
  496.         printf("暂无此功能 请重试");
  497.     }
  498.     break;
  499.     }
  500. }
  501. void Clearguests(Node2* phead)
  502. {
  503.     Node2* ptemp = phead->next;
  504.     while (phead)
  505.     {
  506.         free(phead);    //释放指向的节点内存
  507.         phead = ptemp;
  508.         if (ptemp)
  509.             ptemp = ptemp->next;
  510.     }
  511. }
  512. void Clearrooms(Node1* phead)
  513. {
  514.     Node1* ptemp = phead->next;
  515.     while (phead)
  516.     {
  517.         free(phead);    //释放指向的节点内存
  518.         phead = ptemp;
  519.         if (ptemp)
  520.             ptemp = ptemp->next;
  521.     }
  522. }
  523. void Clearhistories(Node3* phead)
  524. {
  525.     Node3* ptemp = phead->next;
  526.     while (phead)
  527.     {
  528.         free(phead);    //释放指向的节点内存
  529.         phead = ptemp;
  530.         if (ptemp)
  531.             ptemp = ptemp->next;
  532.     }
  533. }
  534. int main()
  535. {
  536.     printf("\n**--------欢迎使用酒店管理系统--------**");
  537.    
  538.     Node2* guests = NULL;
  539.     Node3* histories = NULL;
  540.    
  541.     Node1* rooms = initialize();
  542.    
  543.     takeguests(&guests);
  544.     printf("\n是否为首次使用该程序?\n1.是\n2.否\n");
  545.     int q;
  546.     scanf_s("%d", &q);
  547.     if (q == 1)update2(rooms);
  548.     else update1(rooms, guests);//更新房间状态
  549.     printf("\n是否结过账?\n1.是\n2.否\n");
  550.     int p;
  551.     scanf_s("%d", &p);
  552.     if(p==1)takehistory(&histories);
  553.    

  554.     int a;
  555.    
  556.     do {

  557.         printf("\n****************************************");
  558.         printf("\n****************************************");
  559.         printf("\n**--------欢迎使用酒店管理系统--------**");
  560.         printf("\n**--------1.办理入住--------------------**");
  561.         printf("\n**--------2.退房结账------------------**");
  562.         printf("\n**--------3.查询信息------------------**");
  563.         printf("\n**--------4.退出---------------------**");
  564.         printf("\n**--------输入序号使用功能------------**");
  565.         printf("\n****************************************");
  566.         printf("\n****************************************\n");
  567.       
  568.         scanf_s("%d", &a);
  569.         switch (a)
  570.         {
  571.         case 1:distribute(rooms, &guests); break;
  572.         case 2:Seller(&guests, rooms,&histories); break;
  573.         case 3:search(guests, histories); break;
  574.         case 4: {saveguests(guests); savehistory(histories); printf("\n下班愉快,感谢使用"); break; }
  575.         default:
  576.         {
  577.             printf("暂无此功能 请重试");
  578.         }

  579.         }
  580.     } while (a != 4);
  581.     Clearguests(guests);
  582.     Clearrooms(rooms);
  583.     Clearhistories(histories);
  584.     return 0;
  585.    
  586. }


  587. /*使用说明:1.初始化各房间数目不可轻易改动,若改动必须保证当前住户全部结账情况下。这里假定房间数60、单人间20、双人间30、三人间10*/
复制代码
最佳答案
2023-11-8 14:18:42
代码主体设计的流程基本没问题。

主函数:

1、初始化房间
2、读取文件中原有信息
3、主菜单循环
        1、录入新客人
        2、结账
        3、查询
        4、退出
4、退出前保存信息到文件
5、释放内存

这个问题:

  1. 读取访问权限冲突
复制代码

因为结构体成员变量顺序问题导致的对齐错误。

根据错误信息可以看出,问题出现在读取history成员时。

尝试修改:

  1. struct Node3{
  2. char name[50];
  3. char idnumber[20];
  4. char telenumber[20];
  5. char address[100];
  6. int roomnumber;
  7. int price;
  8. time_t intime;
  9. time_t outtime;
  10. Node3* next;
  11. };
复制代码



问题如图

问题如图
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-11-8 14:18:42 | 显示全部楼层    本楼为最佳答案   
代码主体设计的流程基本没问题。

主函数:

1、初始化房间
2、读取文件中原有信息
3、主菜单循环
        1、录入新客人
        2、结账
        3、查询
        4、退出
4、退出前保存信息到文件
5、释放内存

这个问题:

  1. 读取访问权限冲突
复制代码

因为结构体成员变量顺序问题导致的对齐错误。

根据错误信息可以看出,问题出现在读取history成员时。

尝试修改:

  1. struct Node3{
  2. char name[50];
  3. char idnumber[20];
  4. char telenumber[20];
  5. char address[100];
  6. int roomnumber;
  7. int price;
  8. time_t intime;
  9. time_t outtime;
  10. Node3* next;
  11. };
复制代码



小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-21 15:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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