#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<string.h>
#define M 100//定義房間個數//
//函式定義//
void Stay();//錄入入住函式//*
void Information();//資訊查詢函式// *
void Browse();//瀏覽資訊函式//*
void Save(int);//資訊保存函式//*
void read(int) ;//讀取資訊函式//*
void Checkout();//結賬退房函式//
void Modify();//資訊修改函式//
void Current();//當前費用// *
int judge_year(int x);
//結構體定義//
struct time
{
int year;
int mon;
int date;
int hour;
};//時間結構體//
struct room
{
char name[20];
char ID[18];
int flag;//0-無人入住;1-按小時收費;2-按天收費//
int prepaid;
time t[2];//0-入住時間;1-當前時間//
}room[M];//房間個數//
char c1[50]={0};//單人間//
char c2[50]={0};//雙人間//
//主函式//
int main()
{
system("color 3e");
int n;
printf("welcome to use\n");
do{
printf("\t\t\t\t\t[1]錄入資訊\t\t\t\t\t\n");
printf("\t\t\t\t\t[2]資訊瀏覽\t\t\t\t\t\n");
printf("\t\t\t\t\t[3]資訊查詢\t\t\t\t\t\n");
printf("\t\t\t\t\t[4]資訊修改\t\t\t\t\t\n");
printf("\t\t\t\t\t[5]當前費用\t\t\t\t\t\n");
printf("\t\t\t\t\t[6]結賬退房\t\t\t\t\t\n");
printf("\t\t\t\t\t[0]退出程式\t\t\t\t\t\n");
printf("請輸入你想要執行的操作\n");
scanf("%d",&n);
if(n<0||n>7)
{
printf("輸入的操作代碼錯誤,請重新輸入\n");
scanf("%d",&n);
}
switch(n)
{
case 0:exit(0);
case 1:Stay();break;
case 2:Browse();break;
case 3:Information();break;
case 4:Modify();break;
case 5:Current();break;
case 6:Checkout();break;
}
}while(n!=0);
}
void Stay()//錄入入住函式//
{
system("cls");
system("color 3e");
printf("提示:本賓館1-50號房間為單人間;51-100為雙人間\n輸入時間格式:2010-2-23");
int i,j,l,k;
int tab[2][12]={
{31,28,31,30,31,30,31,31,30,31,30,31},
{31,29,31,30,31,30,31,31,30,31,30,31}
};
printf("請輸入您將入住的房間號:");
scanf("%d",&i);
do{
printf("您輸入的房間號有誤,請重新輸入:");
scanf("%d",&i);
}while(i<0||i>100);
read(i);
while(room[i-1].flag!=0)
{
printf("對不起,該房間已有客人入住,請輸入其他房間號:");
scanf("%d",&i);
read(i);
}
printf("請輸入你的姓名:");
scanf("%s",&room[i-1].name);
j=strlen(room[i-1].name);
while(j<0||j>20)
{
printf("您的姓名輸入有誤,請重新輸入:");
scanf("%s",&room[i-1].name);
j=strlen(room[i-1].name);
}
printf("請輸入你的證件號碼:");
scanf("%s",&room[i-1].ID);
j=strlen(room[i-1].ID);
while(j!=18)
{
printf("您輸入的證件號碼有誤,請重新輸入:");
scanf("%s",room[i-1].ID);
j=strlen(room[i-1].ID);
}
if(i<=50)
{
printf("您選擇的是單人間,該房間的計費方式為:148元/天;8元/小時");
printf("請選擇計費方式:【1】按小時計費;【2】按天收費");
}
else
{
printf("您選擇的是雙人間,該房間的計費方式是:188元/天;10元/小時");
printf("請選擇計費方式:【1】按小時計費;【2】按天收費");
}
scanf("%d",&room[i-1].flag);
while(room[i-1].flag<=0||room[i-1].flag>2)
{
printf("您輸入的計費方式有誤,請重新輸入:");
scanf("%d",room[i-1].flag);
}
printf("輸入您的入住時間:");
scanf("%d-%d-%d-%d",&room[i-1].t[0].year,&room[i-1].t[0].mon,&room[i-1].t[0].date,&room[i-1].t[0].hour);
l=judge_year(room[i-1].t[0].year);
while((room[i-1].t[0].year<=2020)||
(room[i-1].t[0].mon<=0||room[i-1].t[0].mon>12)
||(room[i-1].t[0].date<=0||room[i-1].t[0].date>tab[l][room[i-1].t[0].mon-1])||
(room[i-1].t[0].hour<0||room[i-1].t[0].hour>=24))
{
printf("您輸入的入住時間有誤,請重新輸入:");
scanf("%d-%d-%d-%d",&room[i-1].t[0].year,&room[i-1].t[0].mon,&room[i-1].t[0].date,&room[i-1].t[0].hour);
l=judge_year(room[i-1].t[0].year);
}
printf("請輸入您的押金數額:(押金為整數)");
scanf("%d",&room[i-1].prepaid);
while(room[i-1].prepaid<=0)
{
printf("您的押金數額有誤,請重新輸入:");
scanf("%d",&room[i-1].prepaid);
}
printf("請檢驗您的資訊是否輸入正確!");
printf("輸入1說明資訊正確,否則輸入2");
scanf("%d",&k);
if(k==1)
{
printf("您已成登記入住,祝您入住愉快!");
Save(i);
}
else
{
Stay();
}
}
void Current()//目前費用函式//
{
system("cls");
system("color 3e");
int i,j,k;
int tab[2][12]={
{31,28,31,30,31,30,31,31,30,31,30,31},
{31,29,31,30,31,30,31,31,30,31,30,31}
};
int day=0;
int hr=0;
int total_hour;
int total_day;
long int price;
printf("請輸入您的房間號:");
scanf("%d",&i);
while(i<0||i>100)
{
printf("您輸入的房間號有誤,請重新輸入:");
scanf("%d",&i);
}
read(i);
while(room[i-1].flag==0)
{
printf("該房間暫時無人居住,請重新輸入房間號:");
scanf("%d",&i);
read(i);
}
printf("請輸入當前時間:\n輸入時間格式:2010-2-23");
shuru:
scanf("%d-%d-%d-%d",&room[i-1].t[1].year,&room[i-1].t[1].mon,&room[i-1].t[1].date,&room[i-1].t[1].hour);
j=judge_year(room[i-1].t[1].year);
while((room[i-1].t[1].year<=2020)||
(room[i-1].t[1].mon<=1||room[i-1].t[1].mon>12)
||(room[i-1].t[1].date<=0||room[i-1].t[1].date>tab[j][room[i-1].t[1].mon-1])||
(room[i-1].t[1].hour<0||room[i-1].t[1].hour>=24))
{
printf("您輸入的入住時間有誤,請重新輸入:");
scanf("%d-%d-%d-%d",&room[i-1].t[1].year,&room[i-1].t[1].mon,&room[i-1].t[1].date,&room[i-1].t[1].hour);
j=judge_year(room[i-1].t[0].year);
}
if(room[i-1].t[0].mon!=room[i-1].t[1].mon)
{
for(k=room[i-1].t[0].mon+1;k<room[i-1].t[1].mon;k++)
{
day=day+tab[j][k-1];
}
day=day+tab[j][room[i-1].t[0].mon-1]-room[i-1].t[0].date+room[i-1].t[1].date;
}
else
{
day=day+room[i-1].t[1].date-room[i-1].t[0].date;}
hr=room[i-1].t[1].hour-room[i-1].t[0].hour;
if(room[i-1].flag==1) /*根據房間型別和計費方式計算費用*/
{
if(hr<0)
{
total_hour=day*24-hr;
}
else
{
total_hour=day*24+hr;
}
if(i<=50)
price=8*total_hour;
else
price=10*total_hour;
}
if(room[i-1].flag==2)
{
while(hr<0){
day=day-1;
hr=hr+24;
}
if(hr<8)
{
total_day=day;
}
else
{
total_day=day+1;
}
if(i<=50)
price=148*total_day;
else
price=188*total_day;
}
while(price<0)
{
printf("您的時間輸入有誤,請重新輸入:");
goto shuru;
}
printf("\n\n當前費用為:%ld元.\n\n",price); /*輸出當前費用*/
}
void Information()//資訊查詢函式//
{
system("cls");
system("color 3e");
int i;
printf("請輸入您要查詢的房間號:");
scanf("%d",&i);
do{
printf("您輸入的房間號有誤,請重新輸入:");
scanf("%d",&i);
}while(i<0||i>100);
read(i);
while(room[i-1].flag==0)
{
printf("該房間無人入住,請重新輸入您的房間號:");
scanf("%d",&i);
read(i);
}
printf("您要查詢的資訊如下:");
printf("姓名:");
printf("%s",room[i-1].name);
printf("\n證件號碼:");
printf("%s",room[i-1].ID);
printf("\n房間型別:");
if(i<=50)
{
printf("單人間");
}
else
{
printf("雙人間");
}
printf("\n入住時間:");
printf("%d年%d月%d日%d時",room[i-1].t[0].year,room[i-1].t[0].mon,room[i-1].t[0].date,room[i-1].t[0].hour);
printf("\n押金數額:");
printf("%d",room[i-1].prepaid);
printf("\n計費方式:");
if(room[i-1].flag==1)
{
printf("按小時計費");
}
else
{
printf("按天計費");
}
}
int judge_year(int x)
{
if((x%4==0)&&(x%100!=0)||(x%400==0))
{
return 1;
}
else
{
return 0;
}
}
void Browse()//資訊溜藍函式//
{
system("cls");
system("color 3e");
int i,j,k;
int year;
int month;
int date;
int hour;
int year1;
int month1;
int date1;
int hour1;
int tab[2][12]={
{31,28,31,30,31,30,31,31,30,31,30,31},
{31,29,31,30,31,30,31,31,30,31,30,31}
};
int day=0;
int hr=0;
int total_hour;
int total_day;
int price;
printf("請按格式輸入當前時間:");
scanf("%d-%d-%d-%d",&year,&month,&date,&hour);
j=judge_year(year);
for(i=1;i<=100;i++)
{
read(i);
if(room[i-1].flag==0)
{
printf("%d房間無人入住\n",i);
if(i<=50)
{
printf("房間型別:單人間\n");
printf("房間單價:148元/天,8元/小時\n");
}
else
{
printf("房間型別:雙人間\n");
printf("房間單價:188元/天,10元/小時\n");
}
}
else
{
year1=year-room[i-1].t[0].year;
month1=month-room[i-1].t[0].mon;
date1=date-room[i-1].t[0].date;
hour1=hour-room[i-1].t[0].hour;
while(hour1<0)
{
date1=date1-1;
hour1=hour1+24;
}
printf("房間%d客戶資訊如下:\n",i);
printf("姓名:");
puts(room[i-1].name);
printf("證件號碼:");
puts(room[i-1].ID);
printf("房間型別:");
if(i<=50)
{
printf("單人間\n");
printf("單價:148元/天,8元/小時。\n");
}
else
{
printf("雙人間\n");
printf("單價:188元/天,10元/小時。\n");
}
printf("計費方式:");
if(room[i-1].flag==1)
{
printf("按小時計費\n");
}
else
{
printf("按天計費\n");
}
printf("入住時間:");
printf("%d年%d%月%d日%d時\n",room[i-1].t[0].year,room[i-1].t[0].mon,room[i-1].t[0].date,room[i-1].t[0].hour);
printf("已入住時間:%d年%d月%d天%d小時。\n",year1,month1,date1,hour1);
printf("押金數額:");
printf("%d元",room[i-1].prepaid);
if(room[i-1].t[0].mon!=month)
{
for(k=room[i-1].t[0].mon+1;k<month;k++)
{
day=day+tab[j][k-1];
}
day=day+tab[j][room[i-1].t[0].mon-1]-room[i-1].t[0].date+date;
}
else
{
day=day+date-room[i-1].t[0].date;
}
hr=hour-room[i-1].t[0].hour;
if(room[i-1].flag==1) /*根據房間型別和計費方式計算費用*/
{if(hr<0)
{
total_hour=day*24-hr;
}
else
{
total_hour=day*24+hr;
}
if(i<=50)
price=8*total_hour;
else
price=10*total_hour;
}
else if(room[i-1].flag==2)
{
while(hr<0)
{
day=day-1;
hr=hr+24;
}
if(hr<8)
{
total_day=day;
}
else
{
total_day=day+1;
}
if(i<=50)
price=148*total_day;
else
price=128*total_day;
}
printf("\n當前費用為:%ld元.\n",price);
}
day=0;hr=0;/*時間清零*/
}
}
void Save(int x)//保存檔案函式//
{
int i;
char c;
FILE*fp;
if((fp=fopen("賓館資訊.txt","wb+"))==NULL);
{
printf("無法打開檔案\n");
scanf("%s",&c);
main();
}
for(i=1;i<x;i++)
{
if(fwrite(&room[i-1],sizeof(struct room),1,fp)!=1)
{
printf("檔案寫入錯誤\n");
fclose(fp);
}
}
}
void read(int y)//讀取函式//
{
int i=1;
char c;
FILE *fp;
if((fp=fopen("賓館資訊.txt","rb+"))==NULL)
{
printf("無法打開檔案\n");
scanf("%s",&c);
m
uj5u.com熱心網友回復:
兄弟,你這個C語言程式復雜呀!uj5u.com熱心網友回復:
代碼還沒貼全,你的問題是什么。轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/131800.html
標籤:C語言
