main.c——房間費率程序
#include <stdio.h>
#include <stdlib.h>
#include "hotel.h"
int main(int argc, char *argv[]) {
int nights;
double hotel_rate;
int code;
while ((code=menu())!=QUIT)
{
switch(code)
{case 1:hotel_rate=HOTEL1;
break;
case 2:hotel_rate=HOTEL2;
break;
case 5:hotel_rate=HOTEL3;
break;
case 4:hotel_rate=HOTEL4;
break;
default :hotel_rate=0.0;
printf("Oops!\n");
break;
}
nights=getnights();
showprice(hotel_rate,nights);
}
printf("Thank you and goodbye.\n");
return 0;
}
hotel.c——酒店管理函數(shù)
#include <stdio.h>
#include "hotel.h"
int menu(void)
{int code,status;
printf("\n%s%s\n",STARS,STARS);
printf("請輸入各類房間前的號碼\n");
printf("1.Fairfield Arms 2.Hotel Olympic\n");
printf("3.Chertworthy Plaza 4.The Stockton\n");
printf("5.quit\n");
printf("%s%s\n",STARS,STARS);
while((status=scanf("%d",&code))!=1||(code<1||code>5))
{if(status!=1)
{scanf("%*s");
}
printf("請輸入各類房間前的號碼\n");
}
return code;
}
int getnights(void)
{
int nights;
printf("你要住幾天晚上\n");
while(scanf("%d",&nights)!=1)
{scanf("%*s");
printf("請輸入數(shù)字,比如2\n");
}
return nights;
}
void showprice(double rate,int nights)
{int n;
double total=0.0;
double factor=1.0;
for(n=1;n<=nights;n++,factor*=DISCOUNT)
total+=rate*factor;
printf("您的消費是%0.2f.\n",total);
}
hotel.h——頭文件
#define QUIT 5
#define HOTEL1 50.00
#define HOTEL2 60.00
#define HOTEL3 70.00
#define HOTEL4 80.00
#define DISCOUNT 0.95
#define STARS"******************"
int menu(void);
int getnights(void);
void showprice(double rate,int night);
捕獲.PNG
捕獲.PNG
scanf("%s")*
scanf("%s")表示跳至下一空白字符二打,這里主要是中間的字符起的作用赴涵。比如:
scanf("%s")表示跳至下一空白字符,這里主要是中間的字符起的作用狂打。比如:
int n;
scanf("%*d %*d %d",&n);
printf("%d",n);
return 0;
如果輸入的是1 2 3泥技,那么輸出的是3贸宏,因為前兩個已經(jīng)忽略啦鞋仍。