多文件编译程式--计算酒店费用

//---------------------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);

 

posted @   江南王小帅  阅读(34)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示