C语言多源代码文件

编译多源代码文件程序

在进行大型项目时,为避免在main()中写出函数原型,可将函数原型放在其他文件中,通过头文件进行管理。另外,预处理定义的符号常量放在头文件中便于后续维护。

这里通过一个案例来理解,假设要管理 4 家酒店的客房服务,每家酒店的房价不同,但是每家酒店所有房间的房价相同。对于预订住宿多天的客户,第2天的房费是第1天的95%,第3天是第2天的95%,以此类推(暂不考虑这种策略的经济效益)。设计一个程序让用户指定酒店和入住天数,然后计算并显示总费用。同时,程序要实现一份菜单,允许用户反复输入数据,除非用户选择退出。

  1. 酒店管理程序
c 复制代码
// 酒店管理函数
#include <stdio.h>
#include "hotel.h"
/*菜单函数*/
int menu()
{
    int code, status;
    printf("\n%s%s\n", STARS, STARS);
    printf("Enter the number of the desired hotel:\n");
    printf("1) Fairfield Arms 2) Hotel Olympic\n");
    printf("3) Chertworthy Plaza 4) The Stockton\n");
    printf("5) quit\n");
    printf("%s%s\n", STARS, STARS);
    while ((status = scanf("%d", &code)) != 1 || (code < 1 || code > 5))
    {
        if (status != 1)
            scanf("%*s"); // 处理非整数输入
        printf("Enter an integer from 1 to 5, please.\n");
    }
    return code;
}
/*用户需求*/
int getnights(void)
{
    int nights;
    printf("How many nights are needed? ");
    while (scanf("%d", &nights) != 1)
    {
        scanf("%*s"); // 处理非整数输入
        printf("Please enter an integer, such as 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("The total cost will be $%0.2f.\n", total);
}
  1. 酒店常量以及函数原型
c 复制代码
/* 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);
  1. 用户主函数
c 复制代码
// 房间费率计算程序
#include <stdio.h>
#include "hotel.h"

int main()
{
    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;
}
相关推荐
FPGA-李宇航2 小时前
FPGA中,“按键控制LED灯实验”学习中常见问题、解决思路和措施以及经验总结!!!(新手必看)
学习·fpga开发·按键控制led灯
正经教主3 小时前
【Trae+AI】和Trae学习搭建App_01:项目架构设计与环境搭建
学习·app
普蓝机器人3 小时前
面向智慧农业的自主移动果蔬采摘机器人:融合视觉识别与自动驾驶的智能化农作系统研究
人工智能·学习·机器人·移动机器人·三维仿真导航
lazily-c3 小时前
Web后端开发总结
java·学习·web·1024程序员节
于顾而言5 小时前
【笔记】Comprehensive Rust语言学习
笔记·学习·rust
im_AMBER7 小时前
React 06
前端·javascript·笔记·学习·react.js·前端框架
zyq99101_18 小时前
树与二叉树的奥秘全解析
c语言·数据结构·学习·1024程序员节
微露清风8 小时前
系统性学习C++-第七讲-string类
java·c++·学习
报错小能手8 小时前
项目——基于C/S架构的预约系统平台(3)
linux·开发语言·笔记·学习·架构·1024程序员节
W.Y.B.G8 小时前
css3 学习笔记
笔记·学习·css3·1024程序员节