多文件编译程式--计算酒店费用
//---------------------main.cpp---------------------
// 房费率的问题
//
#include <iostream>
#include <stdio.h>
#include "hotel.h"
using namespace std;
int main()
{
int nights;
double hotel_rate;
int code=0;
while ((code = menu()) != QUIT)
{
switch (code)
{
case 1:
hotel_rate = HOTEL1;
break;
case 2:
hotel_rate = HOTEL2;
case 3:
hotel_rate = HOTEL3;
break;
case 4:
hotel_rate = HOTEL4;
break;
default:
hotel_rate = 0.0;
printf_s("OOPs!\n");
break;
}
nights = getnights();
showprice(hotel_rate, nights);
}
printf_s("Thank you and goodbye.\n");
return 0;
}
---------------------------------------------------------------
hotel.cpp
//酒店管理函数
#include <stdio.h>
#include "hotel.h"
int menu(void) {
int code=1;
int statues=1 ;
printf("\n%s%s\n", STARTS, STARTS);
printf("Enter the number of the desired hotel:\n");
printf("1) Fairfield Arms");
printf("\t2) Hotel Olympic.\n");
printf("3) Chertworty Plaza");
printf("\t4)The Stockton.\n");
printf("5)quit.\n");
printf("%s%s\n", STARTS, STARTS);
while ((statues=scanf_s("%d",&code))!=1|| (code <1||code>5))
{
if (statues!=1)
scanf_s("%*s");
printf("Enter an integer from 1 to 5,please.\n");
}
return code;
}
int getnights(void) {
int nights;
printf("How many nights are need?\n");
while (scanf_s("%d",&nights) !=1)
{
scanf_s("%*s");
printf("please enter an integer,suche as 3.\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("The total cost will be $%0.2f.\n", total);
}
------------------------------------------------------
hotel.h
#pragma once
#define QUIT 5
#define HOTEL1 180.00
#define HOTEL2 225.00
#define HOTEL3 255.00
#define HOTEL4 355.00
#define DISCOUNT 0.95
#define STARTS "************************"
//显示选择列表
int menu(void);
//返回预定天数
int getnights(void);
//根据费率 入住天数计算费用 并显示结果
void showprice(double rate, int nights);
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~