Written in front
It's hard to write ~ but teacher fan didn't even let me run ~ I'm dry!
It adopts the storage mode of structure array.
Title: bed allocation
A hotel has N rooms, K rooms have a (k), and each room has B (k) single beds. The program of allocating beds for single passengers and withdrawing beds when leaving the hotel is designed by menu calling. If the assignment is successful, the passenger's name, age, gender, arrival date, room grade, room number and bed number will be printed; if the assignment is unsuccessful, the room grade can be changed; if the grade is not changed, the "full passenger" prompt will be printed.
Tucao and explanation
Because of laziness, there is no comment.
In addition to the structures in the time library, there are four types of structures: Guest, Bed, Room and Level. Write out the object-oriented feeling.
The number of beds in each room is not the same as required by the topic ~ I think it's too bloated.
And the reason that "no more than 10 room levels can be built" is just because my room number "X0X" is not well designed (.
The check-out operation just sets the name to NULL.
In the aspect of obtaining system time, we mainly perform various operations on strings.
Complete code
#include <stdio.h> #include <time.h> int levelNum, roomNum, bedNum; typedef struct tm *tim; typedef struct Guest{ int level, room, bed; char name[20],date[20]; int age; int sex; }Guest; typedef struct Bed{ int bedNumber; Guest guest; }Bed; typedef struct Room{ int sex; int bedNum; int peopleNum; int roomNumber; Bed *bed; }Room; typedef struct Level{ Room *room; int roomNum; }Level; Level* BuildHotel(int levelNum, int roomNum, int bedNum){ Level* level; int i, j, k; level = (Level*)malloc(sizeof(Level)*levelNum); for(i=0;i<levelNum;i++){ level[i].room = (Room*)malloc(sizeof(Room)*roomNum); for(j=0;j<roomNum;j++){ level[i].room[j].roomNumber = j + 1; level[i].room[j].peopleNum = 0; level[i].room[j].sex = 3; level[i].room[j].bed = (Bed*)malloc(sizeof(struct Bed)*bedNum); for(k=0;k<bedNum;k++){ level[i].room[j].bed[k].bedNumber = k; strcpy(level[i].room[j].bed[k].guest.name, ""); } } } return level; } int CheckIn(Level* hotel){ int i, j, level, room, bed; int sex, flag = 0; printf("\n*1 in total-%d Class room, please enter the class you want to stay in:",levelNum); scanf("%d",&i); level = i - 1; printf("\n*Please register your gender( F=0 / M=1 ): "); scanf("%d",&sex); if(flag == 0){ for(i=0;i<roomNum;i++){ for(j=0;j<bedNum;j++){ if( hotel[level].room[i].sex==sex && strcmp(hotel[level].room[i].bed[j].guest.name, "")==0 ){ room = i; bed = j; hotel[level].room[i].peopleNum += 1; flag = 1; break; } } if(flag == 1) break; } } if(flag == 0){ for(i=0;i<roomNum;i++){ if( hotel[level].room[i].sex==3 ){ room = i; bed = 0; hotel[level].room[i].sex = sex; hotel[level].room[i].peopleNum += 1; flag = 1; break; } } } if(flag == 0){ return 0; } hotel[level].room[room].bed[bed].guest.room = room; hotel[level].room[room].bed[bed].guest.bed = bed; hotel[level].room[room].bed[bed].guest.sex = sex; char name[20],date[20]; int age; printf("\n**Please register your other personal information:\n"); printf("*Full name:"); scanf("%s",&name); strcpy(hotel[level].room[room].bed[bed].guest.name, name); printf("*Age:"); scanf("%d",&age); hotel[level].room[room].bed[bed].guest.age = age; time_t t; struct tm * lt; char y[20],m[20],d[20],h[20],f[20],s[20]; time (&t);//Get Unix timestamp lt = localtime (&t);//Turn to time structure ltoa(lt->tm_year+1900,y,10); ltoa(lt->tm_mon+1,m,10); ltoa(lt->tm_mday,d,10); ltoa(lt->tm_hour,h,10); ltoa(lt->tm_min,f,10); ltoa(lt->tm_sec,s,10); strcat(y,"/");strcat(y,m);strcat(y,"/");strcat(y,d);strcat(y," "); strcat(y,h);strcat(y,":");strcat(y,f);strcat(y,":");strcat(y,s); strcpy(hotel[level].room[room].bed[bed].guest.date, y); printf("\n**Here is your personal information:\n"); PrintGuest(hotel, level, room, bed); return 1; } void PrintGuest(Level* hotel, int level, int room, int bed){ printf("***************************************************\n"); printf("*\n"); printf("** Grade:%d\t\t Room number:%d0%d\t Bed number: No.%d\n",level+1, level+1, room+1, bed+1); printf("** Full name:%s\t", hotel[level].room[room].bed[bed].guest.name); if(hotel[level].room[room].bed[bed].guest.sex==0) printf("Gender: Female\t"); else printf("Gender: Male\t"); printf("Age:%d\n", hotel[level].room[room].bed[bed].guest.age); printf("** Check in time:%s\n", hotel[level].room[room].bed[bed].guest.date); printf("*\n"); printf("***************************************************\n\n"); } int CheckOut(Level* hotel){ int i, j, level, room, bed; char name[20]; printf("*Please enter your room number:"); scanf("%d", &i); level = i/100 -1; room = i%10 -1; printf("*Please enter your bed number:"); scanf("%d", &j); bed = j - 1; if( strcmp(hotel[level].room[room].bed[bed].guest.name, "")!=0 ){ printf("*Please enter your name:"); scanf("%s", &name); if( strcmp(hotel[level].room[room].bed[bed].guest.name, name)==0 ){ printf("\n**Here is your personal information:\n"); PrintGuest(hotel, level, room, bed); strcpy(hotel[level].room[room].bed[bed].guest.name, ""); hotel[level].room[room].peopleNum -= 1; return 1; } else{ printf("\n*Your name does not match the room number!\n"); return 0; } } else{ printf("\n*The room number or bed number you entered is empty!\n"); return 0; } } int Query(Level* hotel){ int i, j, level, room, bed; char name[20]; printf("*Please enter your room number:"); scanf("%d", &i); level = i/100 -1; room = i%10 -1; printf("*Please enter your bed number:"); scanf("%d", &j); bed = j - 1; if( strcmp(hotel[level].room[room].bed[bed].guest.name, "")==0 ){ printf("\n*The room number or bed number you inquired is empty!\n"); return 0; } else { printf("\n**Here is the information you queried:\n"); PrintGuest(hotel, level, room, bed); return 1; } } void PrintAll(Level* hotel){ int i, j, k, sum = 0; for(i=0;i<levelNum;i++){ for(j=0;j<roomNum;j++){ for(k=0;k<bedNum;k++){ if( strcmp(hotel[i].room[j].bed[k].guest.name, "")!=0 ){ PrintGuest(hotel, i, j, k); sum++; } } } } printf("***************************************************\n\n"); printf("*CK Hotel occupancy:\n"); for(i=0;i<levelNum;i++){ printf("\n******Grade%d******\n", i+1); for(j=0;j<roomNum;j++){ printf("*Room%d0%d: \t", i+1, j+1); if(hotel[i].room[j].sex==0) printf("Female room\t"); else if(hotel[i].room[j].sex==1) printf("Male room\t"); else printf("Unused room\t"); printf("Surplus%d Beds\n", bedNum - hotel[i].room[j].peopleNum); } } printf("\n*CK The hotel is currently occupied%d People, number of beds left%d. \n", sum, levelNum*roomNum*bedNum-sum); printf("***************************************************\n\n"); } /********************************************/ int main(){ int select, level, room, bed, sum; int age; char sex; char name[20]; printf("CK How many rooms are there in the inn?"); scanf("%d",&levelNum); printf("CK How many rooms are there in each level of the inn?"); scanf("%d",&roomNum); printf("CK How many beds are there in each room of the inn?"); scanf("%d",&bedNum); if(levelNum>10 || roomNum>10) { printf("Due to limited funds and space, we can't build more than 10 levels and rooms!"); levelNum = 9; roomNum = 9; } Level* hotel = BuildHotel(levelNum, roomNum, bedNum); while(1){ printf("\n\n*******Welcome to CK Inn*******"); printf("\n***----1>Check in------------***"); printf("\n***----2>Check out------------***"); printf("\n***----3>Enquire lodger--------***"); printf("\n***----4>Print room conditions----***"); printf("\n***----5>Exit system--------***"); printf("\n****************************"); printf("\n*Please enter a number:"); scanf("%d",&select); printf("\n"); switch(select){ case 1: if( CheckIn(hotel) ){ printf("*Check in successful!\n"); } else printf("\n*Room of this level is full! Please reselect!\n"); break; case 2: if( CheckOut(hotel) ){ printf("*Check out successful!\n"); } else printf("*Check out failed!\n"); break; case 3: if( Query(hotel) ){ printf("*Query succeeded!\n"); } else printf("*Query failed!\n"); break; case 4: PrintAll(hotel); break; case 5: printf("*looking forward to your next visit CK Tavern! Have a good trip!"); return 0; default: printf("*Input error! Please re-enter!\n"); break; } } }