已在CodeBlocks 17.12上测试
hotel.h
#ifndef HOTEL_H_INCLUDED
#define HOTEL_H_INCLUDED
#define QUIT 5
#define HOTEL1 80.00
#define HOTEL2 125.00
#define HOTEL3 155.00
#define HOTEL4 200.00
#define DISCOUNT 0.95
#define STARS "*******************************"
//给出选项列表
int menu(void);
//返回预定的天数
int getnights(void);
//按饭店的星级和预定的天数计算价格并显示出来
void showprice(double, int);
#endif // HOTEL_H_INCLUDED
hotel.c
#include <stdio.h>
#include "hotel.h"
int menu(void)
{
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("请问你需要住多少晚?\n");
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("此次入住将要花费 %0.2f元。\n", total);
}
usehotel.c
#include <stdio.h>
#include "hotel.h"
int main(void)
{
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("谢谢光临,再见。\n ");
system("pause");
return 0;
}
原文地址:https://www.cnblogs.com/zonkidd/p/12249524.html
时间: 2024-10-07 15:17:15