寫的好辛苦~可是範老師卻連運行都沒讓我運行~我枯了!
採用的是結構體數組的存儲方式。數組
某客店有N個等級的房間,第k級客房有A(k)個,每一個房間有B(k)個單人牀,以菜單調用方式設計爲單身旅客分配牀位以及離店時收回牀位的程序。要求分配成功時,印出旅客姓名、年齡、性別、到達日期、客房等級、房間號及牀位號;分配不成功時,容許更改房間等級,若不更改等級,印出「滿客」提示。
由於懶,就不註釋了。
除了time庫裏面的結構體以外,有Guest、Bed、Room和Level四種結構體。寫出了面向對象的感受。
沒有按照題目要求那樣每一個房間牀位數還不同~我以爲那樣太臃腫了。
以及「不可建造多餘10個房間等級」的緣由僅僅是由於個人房間號「X0X」很差設計……(。
退房操做僅僅是將名字置NULL。
獲取系統時間方面主要就是對字符串進行了各類操做。ui
#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-%d等級的房間,請輸入您要入住的等級:",levelNum); scanf("%d",&i); level = i - 1; printf("\n*請登記您的性別( 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**請登記您的其餘我的信息:\n"); printf("*姓名:"); scanf("%s",&name); strcpy(hotel[level].room[room].bed[bed].guest.name, name); printf("*年齡:"); 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);//獲取Unix時間戳 lt = localtime (&t);//轉爲時間結構 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**如下是您的我的信息:\n"); PrintGuest(hotel, level, room, bed); return 1; } void PrintGuest(Level* hotel, int level, int room, int bed){ printf("***************************************************\n"); printf("*\n"); printf("** 等級:%d\t\t房號:%d0%d\t牀號:No.%d\n",level+1, level+1, room+1, bed+1); printf("** 姓名:%s\t", hotel[level].room[room].bed[bed].guest.name); if(hotel[level].room[room].bed[bed].guest.sex==0) printf("性別:女\t"); else printf("性別:男\t"); printf("年齡:%d\n", hotel[level].room[room].bed[bed].guest.age); printf("** 入住時間:%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("*請輸入您的房間號:"); scanf("%d", &i); level = i/100 -1; room = i%10 -1; printf("*請輸入您的牀號:"); scanf("%d", &j); bed = j - 1; if( strcmp(hotel[level].room[room].bed[bed].guest.name, "")!=0 ){ printf("*請輸入您的名字:"); scanf("%s", &name); if( strcmp(hotel[level].room[room].bed[bed].guest.name, name)==0 ){ printf("\n**如下是您的我的信息:\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*您的名字與房號不匹配!\n"); return 0; } } else{ printf("\n*您輸入的房號或牀號爲空!\n"); return 0; } } int Query(Level* hotel){ int i, j, level, room, bed; char name[20]; printf("*請輸入您查詢的房間號:"); scanf("%d", &i); level = i/100 -1; room = i%10 -1; printf("*請輸入您查詢的牀號:"); scanf("%d", &j); bed = j - 1; if( strcmp(hotel[level].room[room].bed[bed].guest.name, "")==0 ){ printf("\n*您查詢的房號或牀號爲空!\n"); return 0; } else { printf("\n**如下是您查詢的信息:\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客棧入住狀況:\n"); for(i=0;i<levelNum;i++){ printf("\n******等級%d******\n", i+1); for(j=0;j<roomNum;j++){ printf("*房間%d0%d:\t", i+1, j+1); if(hotel[i].room[j].sex==0) printf("女性房間\t"); else if(hotel[i].room[j].sex==1) printf("男性房間\t"); else printf("未入住房間\t"); printf("剩餘%d個牀位\n", bedNum - hotel[i].room[j].peopleNum); } } printf("\n*CK客棧目前共入住%d人,剩餘牀位數%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客棧一共有幾個房間等級?"); scanf("%d",&levelNum); printf("CK客棧每一個等級有幾個房間?"); scanf("%d",&roomNum); printf("CK客棧每一個房間有幾個牀位?"); scanf("%d",&bedNum); if(levelNum>10 || roomNum>10) { printf("因爲資金和場地有限,本旅館不可建造多餘10個等級及房間!"); levelNum = 9; roomNum = 9; } Level* hotel = BuildHotel(levelNum, roomNum, bedNum); while(1){ printf("\n\n*******歡迎來到CK客棧*******"); printf("\n***----1>入住------------***"); printf("\n***----2>退房------------***"); printf("\n***----3>查詢房客--------***"); printf("\n***----4>打印房間狀況----***"); printf("\n***----5>退出系統--------***"); printf("\n****************************"); printf("\n*請輸入數字:"); scanf("%d",&select); printf("\n"); switch(select){ case 1: if( CheckIn(hotel) ){ printf("*入住成功!\n"); } else printf("\n*該等級房間已滿!請從新選擇!\n"); break; case 2: if( CheckOut(hotel) ){ printf("*退房成功!\n"); } else printf("*退房失敗!\n"); break; case 3: if( Query(hotel) ){ printf("*查詢成功!\n"); } else printf("*查詢失敗!\n"); break; case 4: PrintAll(hotel); break; case 5: printf("*歡迎下次光臨CK客棧!祝您一帆風順!"); return 0; default: printf("*輸入錯誤!請從新輸入!\n"); break; } } }