C語言實現(xiàn)客房管理系統(tǒng)
本文實例為大家分享了C語言實現(xiàn)客房管理系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
總體設(shè)計
下面來分析一下整個系統(tǒng),根據(jù)上面的要求分析,可將這個系統(tǒng)設(shè)計為五大模塊(見下圖):登記模塊,統(tǒng)計模塊,查詢模塊,修改模塊,退出模塊。其主要功能為:注冊,統(tǒng)計,查詢,修改,退房功能。
一、頭文件以及結(jié)構(gòu)體
#include<stdio.h> #include<conio.h> #include<stdlib.h> #include<string.h> #define NUM 50? ? //定義常量NUM? void input();? void record(); void find(); void modify(); //建立如下結(jié)構(gòu)體:? struct Client ? { ?? ? char name[20]; ?? ? char sex[10];? ? int age;? ? char ID_card[20]; ??int year;? ? int month; ?? ? int date; ? ? ??? ? int Room_ID; ?? ? char Tel[15]; ? ??? ? int Price; ??? ? char Sign[10]; ??? ? }Room[NUM];
二、保存和讀取函數(shù)
1、保存函數(shù)用于將輸入的數(shù)據(jù)保存到文本文件客戶信息.txt文件中,實現(xiàn)數(shù)據(jù)的保存
void save() { ?? ?FILE *fp; ?? ?int i; ?? ?if ((fp=fopen("客戶信息.txt","a+"))==NULL)?? ?/*a+表示以讀和追加方式打開文件客戶信息.txt */ ?? ?{printf("cannot open file\n"); ?? ?return;} ?? ?for(i=0;i<NUM&&Room[i].age!=0;i++) ?? ?fprintf(fp,"%s %s %d %s %d %d %d %d %s %d %s\n",Room[i].name,Room[i].sex,Room[i].age,Room[i].ID_card,Room[i].year,Room[i].month,Room[i].date,Room[i].Room_ID,Room[i].Tel,Room[i].Price,Room[i].Sign); //按照規(guī)定的格式保存客戶信息 ?? ?fclose(fp);? }
2、讀取函數(shù)將文本文件客戶信息.txt中的客戶個人信息讀取出來,從而實現(xiàn)統(tǒng)計、查詢以及修改的功能。
void read() { FILE *fp; int i; if((fp=fopen("客戶信息.txt","r"))==NULL) ? {printf("\nCannot open file\n"); ?? ?return;} ? for(i=0;!feof(fp);i++)?? ??? ??? ?//!feof(fp)文件讀到結(jié)束時讀取停止 fscanf(fp,"%s %s %d %s %d %d %d %d %s %d %s\n",&Room[i].name,&Room[i].sex, &Room[i].age,&Room[i].ID_card,&Room[i].year,&Room[i].month,&Room[i]. date,&Room[i].Room_ID,&Room[i].Tel,&Room[i].Price,&Room[i].Sign,&now_no);? ?? ?fclose(fp); }
三、按照房間號查找函數(shù)
int search(int num) { ?? ?int i,k=1; ?? ?read(); ?? ?for(i=0;i<NUM;i++) ?? ?{ ?? ??? ?if(Room[i].Room_ID==num) ?? ??? ?{ ?? ??? ??? ?return 1; ?? ??? ??? ?k=0; ?? ??? ?} ?? ?} ?? ??? ?if(k) ?? ??? ??? ?return 0; ?? ??? ?return 0; }
四、登記模塊
用于完成客戶個人信息的輸入功能并保存。本函數(shù)的功能為先要在文件中查找房間狀態(tài),若可以出借,則允許入住,把房客信息(房間號、價格、房間狀態(tài)、入住人姓名、入住人身份證號等)添加到記錄中,并把房間狀態(tài)改為已住。
void input() { ?? ?int i,right,index; ?? ?char as; ?? ?do ?? ?{ ?? ??? ?printf("\t\t\t\t1.賓客入住登記\n"); ?? ??? ?do ?? ??? ?{ ?? ??? ??? ?printf("請輸入房間號:\n"); ?? ??? ??? ?scanf("%d",&i); ?? ??? ??? ?index=search(i); ?? ??? ??? ?if(index) ?? ??? ??? ?{printf("已有賓客居住,請重新輸入!\n"); ?? ??? ??? ?right=1; ?? ??? ??? ?getchar();} ?? ??? ??? ?else ?? ??? ??? ??? ?right=0; ?? ??? ??? ?Room[i-1].Room_ID=i; ?? ??? ?} ?? ??? ?while(right==1); ?? ??? ?printf("\n輸入賓客的姓名:"); ?? ??? ?scanf("%s",Room[i-1].name); ?? ??? ?printf("\n輸入賓客性別:"); ?? ??? ?scanf("%s",Room[i-1].sex); ?? ?printf("\n輸入賓客的年齡:"); ?? ?scanf("%d",&Room[i-1].age); ?? ?printf("\n輸入賓客身份證號:"); ?? ?scanf("%s",Room[i-1].ID_card); ?? ?printf("\n輸入賓客入住時間:"); ?? ?scanf("%d",&Room[i-1].year); ?? ?scanf("%d",&Room[i-1].month); ?? ?scanf("%d",&Room[i-1].date); ?? ?printf("\n輸入賓客電話號:"); ?? ?scanf("%s",Room[i-1].Tel); ?? ?printf("\n輸入賓客房間價格:"); ?? ?scanf("%d",&Room[i-1].Price); ?? ?printf("\n輸入該房間是否入住(yes/no):"); ?? ?scanf("%s",Room[i-1].Sign);//輸入客戶的個人信息 ?? ?Room[i-1].Room_ID=i; ?? ?printf("\n\n"); ?? ?printf("是否繼續(xù)輸入?(Y/N)");?? ??? ?//判斷是否繼續(xù)輸入 ?? ?as=getch(); ?? ?system("cls"); ? ? save(); ?? ?} ?? ?while(as!='n'&&as!='N'); ?? ?system("cls");?? ??? ??? ??? ?//清屏 }
五、統(tǒng)計模塊
用于統(tǒng)計總共入住的人數(shù),并且可以統(tǒng)計某一天住了多少人以及收入多少。
void record() { ?? ?int i,j,y,m,d,s=0,money=0; ?? ?char as; ?? ?int n=0; ?? ??? ?read(); ?? ?do ?? ?{printf("\n客房入住總數(shù): "); ?? ?fflush(stdin);?? ? ?? ?for(j=0;j<NUM;j++) ?? ??? ?if(!strcmp(Room[j].Sign,"yes")) ?? ??? ?n=n+1; ?? ?if(n==0) ?? ?{ ?? ?printf("%d間\n",n);?? ?//統(tǒng)計一共入住的人數(shù) ?? ??? ?break; ?? ?} ?? ?printf("%d間\n",n);?? ?//統(tǒng)計一共入住的人數(shù) ?? ?printf("輸入查詢?nèi)掌? "); ?? ??? ?//按照日期統(tǒng)計入住人數(shù)及收入 ?? ?scanf("%d%d%d",&y,&m,&d); ?? ?fflush(stdin); ?? ?for(i=0;i<=NUM;i++) ?? ??? ?if(((Room[i].year==y)&&(Room[i].month==m)&&(Room[i].date==d))) //判斷輸入的日期與數(shù)據(jù)庫中的哪些日期相同 ?? ??? ?{s=s+1; ?? ??? ?money=money+Room[i].Price; ?? ??? ?printf(" 姓名:%s\n 性別:%s\n 年齡:%d\n 身份證號:%s\n 入住時間:%d年%d月%d日\n 房間號:%d\n 電話號:%s\n 價格:%d\n 是否入?。?s\n",Room[i].name,Room[i].sex,Room[i].age,Room[i].ID_card,Room[i].year,Room[i].month,Room[i].date,Room[i].Room_ID,Room[i].Tel,Room[i].Price,Room[i].Sign); ?? ??? ?} ?? ??? ?printf("\n該天客房入住總數(shù): %d人\n",s);? ?? ??? ?printf("該天共收入%d元\n",money); ?? ??? ?printf("\t按任意鍵返回主菜單."); ?? ??? ?as=getch(); ?? ??? ?system("cls"); ?? ?} ?? ?while(!as); ?? ?system("cls"); }
六、查詢模塊
本函數(shù)的功能為輸出查詢功能菜單選項,包括房間號、價格、房間標(biāo)記、用戶姓名、居份證查詢等功能。系統(tǒng)根據(jù)輸入的選項執(zhí)行相應(yīng)的函數(shù)。
void find() { ?? ?int i,a,b,c; ?? ?char str[20],as,d[20],e[10]; ?? ?read (); ?? ?do ?? ?{ ?? ??? ?printf("\n按以下條件查詢:1.賓客姓名 2.房間號 3.房間價格 4.身份證號 5.房間標(biāo)記 6.返回主菜單\n請選擇:"); ?? ??? ?scanf("%d",&a); ?? ??? ?switch(a) ?? ??? ?{ ?? ??? ?case 1: ?? ??? ??? ?do ?? ??? ??? ?{printf("輸入要查詢的賓客:"); ?? ??? ??? ?fflush(stdin); ?? ??? ??? ?gets(str); ?? ??? ??? ?for(i=0;i<NUM;i++) ?? ??? ??? ?if(!strcmp(Room[i].name,str)) printf(" 姓名:%s\n 性別:%s\n 年齡:%d\n 身份證號:%s\n 入住時間:%d年%d月%d日\n 房間號:%d\n 電話號:%s\n 房間價格:%d\n 該房間是否入住(yes/no)%s\n\n",Room[i].name,Room[i].sex,Room[i].age, Room[i].ID_card,Room[i].year,Room[i].month,Room[i].date,Room[i].Room_ID,Room[i].Tel,Room[i].Price,Room[i].Sign); ?? ??? ??? ?else; ?? ??? ??? ?} ?? ??? ??? ?while(!as); ?? ??? ??? ?break; ?? ??? ?case 2: ?? ??? ?do ?? ??? ?{ ?? ??? ??? ?printf("輸入要查詢的房間號:"); ?? ??? ??? ?scanf("%d",&b); ?? ??? ?for(i=0;i<NUM;i++) ?? ??? ??? ?if(Room[i].Room_ID==b) printf(" 姓名:%s\n 性別:%s\n 年齡:%d\n 身份證號:%s\n 入住時間:%d年%d月%d日\n 房間號:%d\n 電話號:%s\n 房間價格:%d\n 該房間是否入住(yes/no)%s\n\n",Room[i].name,Room[i].sex,Room[i].age,Room[i].ID_card,Room[i].year,Room[i].month,Room[i].date,Room[i].Room_ID,Room[i].Tel,Room[i].Price,Room[i].Sign); ?? ??? ??? ??? ?else; ?? ??? ?} ?? ??? ?while(!as); ?? ??? ?break; ?? ??? ?case 3: ?? ??? ??? ?do ?? ??? ??? ?{ ?? ??? ??? ??? ?printf("輸入要查詢的房間價格:"); ?? ??? ??? ??? ?scanf("%d",&c); ?? ??? ??? ??? ?for(i=0;i<NUM;i++) ?? ??? ??? ??? ??? ?if(Room[i].Price==c) ?? ??? ??? ??? ??? ??? ?printf(" 姓名:%s\n 性別:%s\n 年齡:%d\n 身份證號:%s\n 入住時間:%d年%d月%d日\n 房間號:%d\n 電話號:%s\n 房間價格:%d\n 該房間是否入住(yes/no)%s\n\n",Room[i].name,Room[i].sex,Room[i].age,Room[i].ID_card,Room[i].year,Room[i].month,Room[i].date,Room[i].Room_ID,Room[i].Tel,Room[i].Price,Room[i].Sign); ?? ??? ??? ??? ??? ?else; ?? ??? ??? ?} ?? ??? ??? ?while(!as); ?? ??? ??? ?break; ?? ??? ??? ?case 4: ?? ??? ??? ?do ?? ??? ??? ?{ ?? ??? ??? ??? ?printf("輸入要查詢的身份證號:"); ?? ??? ??? ??? ?scanf("%s",d); ?? ??? ??? ??? ?for(i=0;i<NUM;i++) ?? ??? ??? ??? ??? ?if(!strcmp(Room[i].ID_card,d)) ?? ??? ??? ??? ??? ??? ?printf(" 姓名:%s\n 性別:%s\n 年齡:%d\n 身份證號:%s\n 入住時間:%d年%d月%d日\n 房間號:%d\n 電話號:%s\n 房間價格:%d\n 該房間是否入住(yes/no)%s\n\n",Room[i].name,Room[i].sex,Room[i].age,Room[i].ID_card,Room[i].year,Room[i].month,Room[i].date,Room[i].Room_ID,Room[i].Tel,Room[i].Price,Room[i].Sign); ?? ??? ??? ??? ??? ?else; ?? ??? ??? ?} ?? ??? ??? ?while(!as); ?? ??? ??? ?break; ?? ??? ??? ?case 5: ?? ??? ??? ?do ?? ??? ??? ?{ ?? ??? ??? ??? ?printf("輸入要查詢的房間標(biāo)志:"); ?? ??? ??? ??? ?scanf("%s",e); ?? ??? ??? ??? ?for(i=0;i<NUM;i++) ?? ??? ??? ??? ??? ?if(!strcmp(Room[i].Sign,e)) ?? ??? ??? ??? ??? ??? ?printf(" 姓名:%s\n 性別:%s\n 年齡:%d\n 身份證號:%s\n 入住時間:%d年%d月%d日\n 房間號:%d\n 電話號:%s\n 房間價格:%d\n 該房間是否入住(yes/no)%s\n\n",Room[i].name,Room[i].sex,Room[i].age,Room[i].ID_card,Room[i].year,Room[i].month,Room[i].date,Room[i].Room_ID,Room[i].Tel,Room[i].Price,Room[i].Sign); ?? ??? ??? ??? ??? ?else; ?? ??? ??? ?} ?? ??? ??? ?while(!as); ?? ??? ??? ?break; ?? ??? ?case 6:system("exit"); ?? ??? ?default: ?? ??? ?break; ?? ??? ?} ?? ??? ?printf("是否繼續(xù)查詢?(Y/N)\n"); ?? ??? ?as=getch(); ?? ?} ?? ?while(as!='n'&&as!='N'); ?? ??? ?system("cls"); }
七、修改模塊
本函數(shù)的功能為系統(tǒng)根據(jù)輸入的調(diào)出該用戶及所在房間信息,同時輸出修改功能菜單選項,包括修改姓名、修改性別、修改年齡、修改身份證號、修改日期、修改電話號碼修改房間價格。系統(tǒng)根據(jù)輸入的選項修改用戶相應(yīng)的信息。
void modify() { ?? ?int i,x,a; ?? ?char as; ?? ?printf("輸入要修改的房間號:"); ?? ?scanf("%d",&a); ?? ?read (); ?? ?do? ?? ?{ ?? ?for(i=0;i<NUM;i++) ?? ??? ?if(Room[i].Room_ID==a) ?? ??? ?{system("cls"); ?? ??? ?printf(" 姓名:%s\n 性別:%s\n 年齡:%d\n 身份證號:%s\n 入住時間:%d年%d月%d日\n 電話號:%s\n 價格:%d\n 是否入?。?s\n\n",Room[i].name,Room[i].sex,Room[i].age,Room[i].ID_card,Room[i].year,Room[i].month,Room[i].date,Room[i].Tel,Room[i].Price,Room[i].Sign); ?? ?printf("請選擇要修改的容:\n1.姓名 \t\t2.性別 \t\t3.年齡 \t\t4.身份證號 \n5.入住時間 \t6.電話號\t7.價格\n請選擇:"); //按照不同的選擇修改相應(yīng)的容 ?? ??? ??? ?scanf("%d",&x); ?? ??? ??? ?switch(x) ?? ??? ??? ?{ ?? ??? ??? ?case 1:?? ? ?? ??? ??? ??? ?printf("\n\t\t輸入新插入賓客信息\n"); ?? ??? ??? ??? ?printf("\n輸入賓客姓名:"); ?? ??? ??? ??? ?scanf("%s",Room[i].name);break; ?? ??? ??? ?case 2: ?? ??? ??? ??? ?printf("\n輸入賓客性別:"); ?? ??? ??? ??? ?scanf("%s",&Room[i].sex);break; ?? ??? ??? ?case 3: ?? ??? ??? ??? ?printf("\n輸入賓客的年齡:"); ?? ??? ??? ??? ?scanf("%d",&Room[i].age);break; ?? ??? ??? ?case 4: ?? ??? ??? ??? ?printf("\n輸入賓客身份證號:"); ?? ??? ??? ??? ?scanf("%s",&Room[i].ID_card);break; ?? ??? ??? ?case 5: ?? ??? ??? ??? ?printf("\n輸入賓客入住時間:"); ?? ??? ??? ??? ?scanf("%d",&Room[i].year); ?? ??? ??? ??? ?scanf("%d",&Room[i].month); ?? ??? ??? ??? ?scanf("%d",&Room[i].date);break; ?? ??? ??? ?case 6: ?? ??? ??? ??? ?printf("\n輸入賓客電話號:"); ?? ??? ??? ??? ?scanf("%s",&Room[i].Tel);break; ?? ??? ??? ?case 7: ?? ??? ??? ??? ?printf("\n輸入賓客房間價格:"); ?? ??? ??? ??? ?scanf("%d",&Room[i].Price); ?? ??? ??? ?case 8:system("exit"); ?? ??? ??? ?default:system("cls"); ?? ??? ??? ?} ?? ??? ?printf("\n"); ?? ??? ?save(); printf("\n 姓名:%s\n 性別:%s\n 年齡:%d\n 身份證號:%s\n 入住時間:%d年%d月%d日\n 手機號:%s\n 價格:%d\n 是否入?。?s\n", Room[i].name,Room[i].sex,Room[i].age,Room[i].ID_card,Room[i].year,Room[i].month,Room[i].date,Room[i].Tel,Room[i].Price,Room[i].Sign); ?? ??? ?break; ?? ??? ?}as=getch(); ?? ?}while(!as); ?? ?system("cls"); }
八、退房模塊
本函數(shù)的功能根據(jù)輸入的房間號判斷該房間入住狀態(tài),若有賓客居住把房間狀態(tài)改為空置,把房客信息刪除。
?void OutRoom() ? ? ?//退房函數(shù) { ? ? int i,j; ? ? system("cls"); ? ? printf(" ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?退房管理\n"); ? ? printf("請輸入房間號:\n"); ? ? scanf("%d",&i); ? ? printf("確認(rèn)要退%d號房嗎?(1:Y/0:N)",i); ? ? scanf("%d",&j); ? ? if(j==1) ?? ?{ ? ? ? ? if(Room[i-1].Room_ID==i) ?? ??? ?{ ?? ??? ??? ?Room[i-1].Room_ID=0; strcpy(Room[i-1].name," "); strcpy(Room[i-1].sex," "); Room[i-1].age=0; strcpy(Room[i-1].ID_card," "); Room[i-1].year=0; Room[i-1].month=0; Room[i-1].date=0; strcpy(Room[i-1].Tel," "); strcpy(Room[i-1].Sign,"no"); ? ? ? ? ? ? save(); ? ? ? ? ? ? system("cls"); ? ? ? ? ? ? printf("退房成功\n"); ?? ??? ?} ? ? ? ? else ?? ??? ?{ ? ? ? ? ? ? system("cls"); ? ? ? ? ? ? printf("該房間為空房\n"); ?? ??? ?} ?? ?} ? ? printf("要返回首頁嗎?(1:Y,0:N)"); ? ? scanf("%d",&j); ? ? if(j==0) ?? ?{ ? ? ? ? OutRoom(); ?? ?} ? ? system("cls"); }
九、主函數(shù)?
int main() { ?? ?int as; start: ?? ?do ?? ?{ ?? ??? ?printf("\t***********************************"); ?? ??? ?printf("\n\t\t歡迎使用客房管理系統(tǒng)\n"); ?? ??? ?printf("\n\t\t| ? 1.賓客入住登記 ? |\n\t\t| ? 2.統(tǒng)計賓客信息 ? |\n\t\t| ? 3.查詢賓客信息 ? |\n\t\t| ? 4.修改賓客信息 ? |\n\t\t| ? 5.退房系統(tǒng) ? ? ? |\n\t\t| ? 6.退出 ? ? ? ? ? |\n"); ?? ??? ?printf("\t***********************************\n"); ?? ??? ?printf("\t\t選擇功能選項: ");?? ?//選擇語句用于選擇不同的模塊 ?? ??? ?fflush(stdin); ?? ??? ?scanf("%d",&as); ?? ??? ?switch(as) ?? ??? ?{case 1:system("cls");input();break; ?? ??? ?case 2:system("cls");record();break; ?? ??? ?case 3:system("cls");find();break; ?? ??? ?case 4:system("cls");modify();break; ?? ??? ?case 5:system("cls");OutRoom();break; ?? ??? ?case 6:system("exit");printf("成功退出系統(tǒng)\n");exit(0); ?? ??? ?default:system("cls");goto start;} ?? ?}while(1); ?}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C語言驅(qū)動開發(fā)之內(nèi)核使用IO/DPC定時器詳解
本章將繼續(xù)探索驅(qū)動開發(fā)中的基礎(chǔ)部分,定時器在內(nèi)核中同樣很常用,在內(nèi)核中定時器可以使用兩種,即IO定時器,以及DPC定時器,感興趣的可以了解一下2023-04-04C++用read()和write()讀寫二進制文件的超詳細(xì)教程
二進制的文件肉眼我們是讀不懂的,如果通過二進制的讀寫操作就可以讀懂,下面這篇文章主要給大家介紹了關(guān)于C++用read()和write()讀寫二進制文件的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06