|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- #pragma warning(disable: 4996)
- struct Node1 //显示房间信息Room
- {
- int roomnumber;
- int price;
- int available; // 0表示不可用,1表示可用
- int roomtype;
- Node1* next;
- };
- struct Node2 //显示当前顾客信息Guest
- {
- char name[50];
- char idnumber[20];
- char telenumber[20];
- char address[100];
- int roomnumber;
- time_t intime; // 入住时间
- Node2* next;
- };
- struct Node3 //过往账单信息
- {
- char name[50];
- char idnumber[20];
- char telenumber[20];
- char address[100];
- int roomnumber;
- int price;
- time_t intime; // 入住时间
- time_t outtime; // 结账时间
- Node3* next;
- };
- Node1* createrooom(int roomnumber, int price, int available,int roomtype)//为initilize创建每个房间所准备
- {
- Node1* newRoom = (Node1*)malloc(sizeof(Node1));
- newRoom->roomnumber = roomnumber;
- newRoom->price = price;
- newRoom->available = available;
- newRoom->roomtype = roomtype;
- newRoom->next = NULL;
- return newRoom;
- }
- Node1* initialize()
- {
- Node1* rooms = NULL;
- int roomcount;
- printf("\n请输入总房间数目:");
- scanf_s("%d", &roomcount);
- int type1number, type2number;
- printf("\n请输入单人间数:");
- scanf_s("%d", &type1number);
- printf("\n请输入双人间数:");
- scanf_s("%d", &type2number);
- printf("\n请输入单人间价格:");
- int type1price, type2price, type3price;
- scanf_s("%d", &type1price);
- printf("\n请输入双人间价格:");
- scanf_s("%d", &type2price);
- printf("\n请输入三人间价格:");
- scanf_s("%d", &type3price);
- int roomtype1;
- int price1;
- int roomtype2;
- int price2;
- int roomtype3;
- int price3;
- for (int i = 1; i <= type1number; i++)
- {
- price1 = type1price;
- int available = 1;//把所有房间初始状态设置为可用
- roomtype1 = 1;
- Node1* newroom1 = createrooom(i, price1, available,roomtype1);
- newroom1->next = rooms;
- rooms = newroom1; //前移,到表头;
- }
- for (int j = type1number + 1; j <= type2number+type1number; j++)
- {
- price2 = type2price;
- int available = 1;//把所有房间初始状态设置为可用
- roomtype2 = 2;
- Node1* newroom2 = createrooom(j, price2, available, roomtype2);
- newroom2->next = rooms;
- rooms = newroom2; //前移,到表头;
- }
- for (int k = type2number+type1number + 1; k <= roomcount; k++)
- {
- price3 = type3price;
- int available = 1;//把所有房间初始状态设置为可用
- roomtype3 =3;
- Node1* newroom3 = createrooom(k, price3, available,roomtype3);
- newroom3->next = rooms;
- rooms = newroom3; //前移,到表头;
- }
- Node1* p1 = rooms;
- while (p1)
- {
- printf("%d %d %d \n", p1->roomtype, p1->roomnumber,p1->price);
- p1 = p1->next;
- }
- return rooms;
-
- }
- void update1( Node1* rooms, Node2* guests)//为录入新用户做准备
- {
- while (guests)
- {
- int roomnumber = guests->roomnumber;
- Node1* room = rooms;
- while (room)
- {
- if (room->roomnumber == roomnumber)//
- {
- room->available = 0;
- break;
- }
- room = room->next;
- }
- guests = guests->next;
- }
- }
- void update2(Node1* rooms)//为录入新用户做准备
- {
- Node1* p1 = rooms;
- while (p1)
- {
- p1->available = 1;
- p1 = p1->next;
- }
-
- }
- void distribute(Node1*rooms, Node2** guests)//为新用户分配房间
- {
- Node2* guest = (Node2*)malloc(sizeof(Node2));
- printf("\n请输入住户姓名: ");
- scanf_s("%s",guest->name,50);
- printf("\n请输入证件号码: ");
- scanf_s("%s", guest->idnumber,20);
- printf("\n请输入电话号码: ");
- scanf_s("%s", guest->telenumber,20);
- printf("\n请输入居住地: ");
- scanf_s("%s", guest->address,100);
- int roomtype;
- printf("\n请选择房间类型:\n1. 单人间;\n 2. 双人间\n 3. 三人间 ");
- scanf_s("%d", &roomtype);
- if (roomtype < 1 || roomtype > 3)
- {
- printf("请问大哥你想住几人间?\n");
- return;
- }
- Node1* selectedroom = NULL;
- Node1* room = rooms;
- while (room != NULL)
- {
- if (roomtype == room->roomtype&&room->available==1)
- {
- selectedroom = room;
- room->available = 0;
- break;
- }
- room = room->next;
- }
- guest->roomnumber = selectedroom->roomnumber;
- time(&guest->intime);//从计算机获取时间
- struct tm Tm;
- guest->next = *guests;
- *guests = guest;
- printf("\n入住办理成功!");
- return;
- }
- void Seller(Node2**guests,Node1*rooms,Node3**histories)
- {
- if (*guests == NULL) {
- printf("当前没有客人入住。\n");
- return;
- }
- int roomnumber;
- printf("请输入要退房的房间号: ");
- scanf("%d", &roomnumber);
- Node2* p1 = *guests;
- Node2* p2 = NULL;
- struct tm Tm1;
- tm* p = &Tm1;
- time_t outtime;
- int price;
- int totalprice;
- int livedtime;
- while (p1 != NULL)
- {
- if (p1->roomnumber == roomnumber)
- {
- Node1* room = rooms;
- while (room!=NULL)
- {
- if (room->roomnumber == roomnumber)
- {
- price = room->price;
- break;
- }
- room = room->next;
- }
- time(&outtime);
- livedtime = difftime(outtime, p1->intime)/(3600*24);
- if (livedtime < 1) livedtime = 1;
- totalprice = livedtime * price;
- Node3* history = (Node3*)malloc(sizeof(history));
- strcpy(history->name, p1->name);
- strcpy(history->idnumber, p1->idnumber);
- history->roomnumber = p1->roomnumber;
- history->price = price;
- history->intime = p1->intime;
- history->outtime = outtime;
-
- history->next = *histories;
- *histories = history;
- printf(" 退房成功。\n");
- printf("入住时间: %s", ctime(&p1->intime));
- printf("退房时间: %s", ctime(&outtime));
- printf("应付: %d 元\n", totalprice);
- if (p2 == NULL) {
- //移动指针尾部添加
- *guests = p1->next;
- }
- else {
- p2->next = p1->next;
- }
- // 更新状态
- Node1* room2 = rooms;
- while (room2 != NULL) {
- if (room2->roomnumber == roomnumber) {
- room2->available = 1; // 设置为可用
- break;
- }
- room2 = room2->next;
- }
-
- return;
- }
- p2 = p1;
- p1 = p1->next;
- }
- printf("未找到该房间号的客人。请检查输入的房间号。\n");
-
-
- }
- void saveguests(Node2* guests)
- {
- FILE* fp;
- char fn[50];
- printf("\n请输入保存当前住户信息的文件名:(含路径)");
- scanf_s("%s", fn, 50);
- fopen_s(&fp, fn, "w+");
- if (!fp)
- {
- printf("打开文件出错,请检查文件名是否正确。");
- }
- while (guests)
- {
- fprintf(fp, "%s %s %s %s %d %ld \n", guests->name, guests->idnumber, guests->telenumber, guests->address, guests->roomnumber, guests->intime);
- guests = guests->next;
- }
- fclose(fp);
- }
- void takeguests(Node2** guests)
- {
- FILE* fp;
- char fn[50];
- printf("\n请输入保存当前住户信息的文件名:(含路径)");
- scanf_s("%s", fn, 50);
- fopen_s(&fp, fn, "r");
- if (!fp)
- {
- printf("打开文件出错,请检查文件名是否正确。");
- return;
- }
-
- while (1)//一直读,读到文件结尾
- {
- Node2* guest = (Node2*)malloc(sizeof(Node2));
- int result = fscanf(fp, "%s %s %s %s %d %ld ", guest->name, guest->idnumber, guest->telenumber, guest->address, &guest->roomnumber, &guest->intime);
- if (result != 6)
- {
- free(guest);
- break;
- }
- guest->next = *guests;//guests本身就是NULL
- *guests = guest;//前移,做表头
- }
- fclose(fp);
- }
- void savehistory(Node3* history)
- {
- FILE* fp;
- char fn[50];
- printf("请输入保存历史客户信息的文件名:(含路径)");
- scanf_s("%s", fn, 50);
- fopen_s(&fp, fn, "w+");
- if (!fp)
- {
- printf("打开文件出错,请检查文件名是否正确。");
- return;
- }
- while (history)
- {
- fprintf(fp, "%s %s %s %d %d %ld %ld\n", history->name, history->idnumber, history->address, history->roomnumber, history->price, history->intime, history->outtime);
- history = history->next;
- }
- fclose(fp);
- }
- void takehistory(Node3** histories)
- {
- FILE* fp;
- char fn[50];
- printf("\n请输入保存历史客户信息的文件名:(含路径)");
- scanf_s("%s", fn, 50);
- fopen_s(&fp, fn, "r");
- if (!fp)
- {
- printf("打开文件出错,请检查文件名是否正确。");
- return;
- }
- while (1)
- {
- Node3* history = (Node3*)malloc(sizeof(Node3));
- 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);
- if (result != 7)
- {
- free(history);
- break;
- }
- history->next = *histories;
- *histories = history;
- }
- fclose(fp);
- }
- void namesearch(Node2* guests)
- {
- char name[50];
- printf("请输入住户姓名: ");
- scanf_s("%s", name,50);
- while (guests) {
- if (strcmp(guests->name, name) == 0)
- {
- printf("姓名: %s\n", guests->name);
- printf("证件号码: %s\n", guests->idnumber);
- printf("联系电话: %s\n", guests->telenumber);
- printf("居住地: %s\n", guests->address);
- printf("房间号: %d\n", guests->roomnumber);
- printf("入住时间: %s", ctime(&guests->intime));
- }
- guests = guests->next;
- }
- }
- void historynamesearch(Node3* histories)
- {
- char name[50];
- printf("请输入住户姓名: ");
- scanf_s("%s", name,50);
- while (histories) {
- if (strcmp(histories->name, name) == 0)
- {
- printf("姓名: %s\n",histories->name);
- printf("证件号码: %s\n", histories->idnumber);
- printf("联系电话: %s\n", histories->telenumber);
- printf("居住地: %s\n", histories->address);
- printf("房间号: %d\n", histories->roomnumber);
- printf("入住时间: %s", ctime(&histories->intime));
- printf("退房时间:%s",ctime(&histories->outtime));
- }
- histories = histories->next;
- }
- }
- void roomnumbersearch(Node2* guests)
- {
- int roomnumber;
- printf("请输入房间号: ");
- scanf_s("%d", &roomnumber);
- while (guests)
- {
- if (guests->roomnumber == roomnumber)
- {
- printf("姓名: %s\n", guests->name);
- printf("证件号码: %s\n", guests->idnumber);
- printf("电话: %s\n", guests->telenumber);
- printf("居住地: %s\n", guests->address);
- printf("房间号: %d\n", guests->roomnumber);
- printf("入住时间: %s", ctime(&guests->intime));
-
- }
- guests = guests->next;
- }
- }
- void historyroomnumbersearch(Node3* histories)
- {
- int roomnumber;
- printf("请输入房间号: ");
- scanf_s("%d", &roomnumber);
- while (histories)
- {
- if (histories->roomnumber == roomnumber)
- {
- printf("姓名: %s\n", histories->name);
- printf("证件号码: %s\n", histories->idnumber);
- printf("电话: %s\n", histories->telenumber);
- printf("居住地: %s\n", histories->address);
- printf("房间号: %d\n", histories->roomnumber);
- printf("入住时间: %s", ctime(&histories->intime));
- printf("退房时间:%s", ctime(&histories->outtime));
- }
- histories = histories->next;
- }
- }
- void historytimesearch(Node3* histories)
- {
- time_t time1, time2;
- struct tm tm1, tm2;;
- printf("\n请输入查询起始时间:");
- printf("\n年:");
- scanf_s("%d", &tm1.tm_year);
- tm1.tm_year = tm1.tm_year - 1900;//tm结构体中的tm_year是从1900年开始计算的
- printf("\n月:");
- scanf_s("%d", &tm1.tm_mon);
- tm1.tm_mon = tm1.tm_mon - 1;//tm结构体中的tm_mon的范围是0-11
- printf("\n日:");
- scanf_s("%d", &tm1.tm_mday);
- printf("\n时:");
- scanf_s("%d", &tm1.tm_hour);
- printf("\n分:");
- scanf_s("%d", &tm1.tm_min);
- printf("\n秒:");
- scanf_s("%d", &tm1.tm_sec);
- time1 = mktime(&tm1);//将time_t结构体中的本地时间换算为自1970年1月1日起到现在的秒数,也叫UTC时间
- printf("\n请输入查询结束时间:");
- printf("\n年:");
- scanf_s("%d", &tm2.tm_year);
- tm1.tm_year = tm2.tm_year - 1900;//tm结构体中的tm_year是从1900年开始计算的
- printf("\n月:");
- scanf_s("%d", &tm2.tm_mon);
- tm1.tm_mon = tm2.tm_mon - 1;//tm结构体中的tm_mon的范围是0-11
- printf("\n日:");
- scanf_s("%d", &tm2.tm_mday);
- printf("\n时:");
- scanf_s("%d", &tm2.tm_hour);
- printf("\n分:");
- scanf_s("%d", &tm2.tm_min);
- printf("\n秒:");
- scanf_s("%d", &tm2.tm_sec);
- time2 = mktime(&tm2);//将time_t结构体中的本地时间换算为自1970年1月1日起到现在的秒数,也叫UTC时间
- printf("\n查询结果如下:");
- int a = 0;//用a来表示查找状态
- while (histories)
- {
- if (histories->intime > time1 && histories->outtime < time2)
- {
- printf("姓名: %s\n", histories->name);
- printf("证件号码: %s\n", histories->idnumber);
- printf("联系电话: %s\n", histories->telenumber);
- printf("居住地: %s\n", histories->address);
- printf("房间号: %d\n", histories->roomnumber);
- printf("入住时间: %s", ctime(&histories->intime));
- printf("退房时间:%s", ctime(&histories->outtime));
- a = 1;
- }
- histories = histories->next;
- }
- if (a == 0)
- {
- printf("\n该时间段内没有人入住");
- }
- }
- void search(Node2* guests, Node3* histories)
- {
- printf("\n****************************************");
- printf("\n****************************************");
- printf("\n**------------查询---------------------**");
- printf("\n**--------1.当前客户姓名查询------------**");
- printf("\n**--------2.当前客户房号查询------------**");
- printf("\n**--------3.历史客户姓名查询------------**");
- printf("\n**--------4.历史客户房号查询------------**");
- printf("\n**--------5.历史客户时段查询------------**");
- printf("\n****************************************");
- printf("\n****************************************\n");
- int a;
- scanf_s("%d", &a);
- switch (a)
- {
- case 1:namesearch(guests); break;
- case 2:roomnumbersearch(guests); break;
- case 3:historynamesearch(histories); break;
- case 4:historyroomnumbersearch(histories); break;
- case 5:historytimesearch(histories); break;
- default:
- {
- printf("暂无此功能 请重试");
- }
- break;
- }
- }
- void Clearguests(Node2* phead)
- {
- Node2* ptemp = phead->next;
- while (phead)
- {
- free(phead); //释放指向的节点内存
- phead = ptemp;
- if (ptemp)
- ptemp = ptemp->next;
- }
- }
- void Clearrooms(Node1* phead)
- {
- Node1* ptemp = phead->next;
- while (phead)
- {
- free(phead); //释放指向的节点内存
- phead = ptemp;
- if (ptemp)
- ptemp = ptemp->next;
- }
- }
- void Clearhistories(Node3* phead)
- {
- Node3* ptemp = phead->next;
- while (phead)
- {
- free(phead); //释放指向的节点内存
- phead = ptemp;
- if (ptemp)
- ptemp = ptemp->next;
- }
- }
- int main()
- {
- printf("\n**--------欢迎使用酒店管理系统--------**");
-
- Node2* guests = NULL;
- Node3* histories = NULL;
-
- Node1* rooms = initialize();
-
- takeguests(&guests);
- printf("\n是否为首次使用该程序?\n1.是\n2.否\n");
- int q;
- scanf_s("%d", &q);
- if (q == 1)update2(rooms);
- else update1(rooms, guests);//更新房间状态
- printf("\n是否结过账?\n1.是\n2.否\n");
- int p;
- scanf_s("%d", &p);
- if(p==1)takehistory(&histories);
-
-
- int a;
-
- do {
- printf("\n****************************************");
- printf("\n****************************************");
- printf("\n**--------欢迎使用酒店管理系统--------**");
- printf("\n**--------1.办理入住--------------------**");
- printf("\n**--------2.退房结账------------------**");
- printf("\n**--------3.查询信息------------------**");
- printf("\n**--------4.退出---------------------**");
- printf("\n**--------输入序号使用功能------------**");
- printf("\n****************************************");
- printf("\n****************************************\n");
-
- scanf_s("%d", &a);
- switch (a)
- {
- case 1:distribute(rooms, &guests); break;
- case 2:Seller(&guests, rooms,&histories); break;
- case 3:search(guests, histories); break;
- case 4: {saveguests(guests); savehistory(histories); printf("\n下班愉快,感谢使用"); break; }
- default:
- {
- printf("暂无此功能 请重试");
- }
- }
- } while (a != 4);
- Clearguests(guests);
- Clearrooms(rooms);
- Clearhistories(histories);
- return 0;
-
- }
- /*使用说明:1.初始化各房间数目不可轻易改动,若改动必须保证当前住户全部结账情况下。这里假定房间数60、单人间20、双人间30、三人间10*/
复制代码
代码主体设计的流程基本没问题。
主函数:
1、初始化房间
2、读取文件中原有信息
3、主菜单循环
1、录入新客人
2、结账
3、查询
4、退出
4、退出前保存信息到文件
5、释放内存
这个问题:
因为结构体成员变量顺序问题导致的对齐错误。
根据错误信息可以看出,问题出现在读取history成员时。
尝试修改:
- struct Node3{
- char name[50];
- char idnumber[20];
- char telenumber[20];
- char address[100];
- int roomnumber;
- int price;
- time_t intime;
- time_t outtime;
- Node3* next;
- };
复制代码
|
-
问题如图
|