模块化编程之酒店管理

/* usehotel.c -- 房间费率程序 */
/* 与程序清单hotel.c一起编译 */
#include <stdio.h>
#include "hotel.h" /* 定义符号常量,声明函数 */


int main(int argc, const 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 3: 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 -- 酒店管理函数 */
#include <stdio.h>
#include "hotel.h" 
int menu ()
{
int code, status;

printf("\n%s%s\n", STARS, STARS);
printf("请输入您需要入住的酒店标号:\n");
printf("1) 花间堂         2) 碧桂园\n");
printf("3) 翰林府         4) 照澜院\n");
printf("5) 离开\n");
printf("%s%s\n", STARS,STARS);
while ((status = scanf("%d",&code)) != 1 || (code < 1 || code > 5))
{
if (status != 1)
scanf("%*s");   // 处理非整数输入
printf("请输入一个1到5之间的整数. \n");
}
return code;
}


int getnights(void)
{
int nights;


printf("请问您需要入住几晚? ");
while (scanf ("%d",&nights) != 1)
{
scanf("%*s");     // 处理非整数输入
printf("请输入一个整数, 例如 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("您的总计费用为: RMB%0.2f .\n",total);
}

/*hotel.h  -- 符号常量 和 hotel.c 中所有函数的原型*/
#define  QUIT   5
#define   HOTEL1 80.00
#define   HOTEL2 90.00
#define   HOTEL3 100.00
#define   HOTEL4 110.00
#define   DISCOUNT  0.95
#define   STARS  "******************************"


//显示选择列表
int menu(void);


//返回预订天数
int getnights(void);


// 根据费率,入住天数计算费用并显示结果
void showprice (double rate, int nights);






















  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值