• 第 9 章 函数


    /*--------------------------
        hotel.c -- 酒店管理函数
    --------------------------*/
    
    #include <stdio.h>
    #include "hotel.h"
    
    int menu(void)
    {
        int code, status;
    
        printf("
    %s%s
    ", STARS, STARS);
        printf("Enter the number of the desired hotel:
    ");
        printf("1) Fairfield Arms       2) Hotel Olympic
    ");
        printf("3) Chertworthy Plaza    4) The Stockton
    ");
        printf("5) quit
    ");
        printf("%s%s
    ", STARS, STARS);
    
        while ((status = scanf("%d", &code)) != 1 || code < 1 || code > 5)
        {
            if (1 != status) scanf("%*s");
            printf("Enter an integer from 1 to 5, please.
    ");
        }
    
        return code;
    }
    
    int getnights(void)
    {
        int nights;
    
        printf("How many nights are needed? ");
        while (1 != scanf("%d", &nights))
        {
            scanf("%*s");
            printf("Please enter an integer, such as 2.
    ");
        }
    
        return nights;
    }
    
    void showprice(double rate, int nights)
    {
        double total = 0.0;
        double factor = 1.0;
    
        for (int n(1); n <= nights; ++n, factor *= DISCOUNT)
            total += rate * factor;
        printf("The total cost will be $%0.2f.
    ", total);
    }
    /*---------------------------------------------
        hotel.h -- 符号常量和 hotel.c 中所有函数原型
    ---------------------------------------------*/
    
    #define QUIT 5
    #define HOTEL1 180.00
    #define HOTEL2 225.00
    #define HOTEL3 255.00
    #define HOTEL4 355.00
    #define DISCOUNT 0.95
    #define STARS "*************************"
    
    //显示选择列表
    int menu(void);
    
    //返回预订天数
    int getnights(void);
    
    //根据费率、入住天数计算费用;并显示结果
    void showprice(double rate, int nights);
    /*-----------------------------
        usehotel.c -- 房间费率程序
    -----------------------------*/
    
    #include <stdio.h>
    #include "hotel.h"    //声明函数,定义符号常量
    
    int main()
    {
        int nights, code;
        double hotel_rate;
    
        while (QUIT != (code = menu()))
        {
            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!
    ");
                break;
            }
    
            nights = getnights();
            showprice(hotel_rate, nights);
        }
    
        printf("Thank you and goodbye.
    ");
        return 0;
    }
  • 相关阅读:
    Jquery基础入门-3-jQuery进行Ajax操作的方法
    Jquery基础入门-3-JQuery实现动画与特效
    Jquery基础入门-3-JQuery事件相关介绍
    Jquery基础入门-3-JQuery DOM操作-3、4、5
    Jquery基础入门-3-JQuery DOM操作-1、2
    解决eclipse启动时出现“failed to load the jni shared library”
    Eclipse中servlet简易模版
    Unknown character set: 'utf8mb4'
    Windows下chm转换为html的超简单方法
    MySql学习笔记(一)之DQL常用查询
  • 原文地址:https://www.cnblogs.com/web1013/p/9089758.html
Copyright © 2020-2023  润新知