使用头文件
如果把main()放在第一个文件中,把函数定义在第二个文件中,那么第一个文件任然要使用函数原型。把函数原型放在头文件中,就不用在每次使用函数文件时都写出函数模型。C标准库就是这样做的,例如,把I/O函数原型放在stdio.h中,把数学函数原型放在math.h中。也可以用这样用自定义的额函数文件。另外,程序中经常使用C与处理器定义符号常量。这种定义指出村了那些包含#define 指令的文件。如果把程序的一个函数放进一个独立的文件中,也可以适应#define指令访问每个文件。最直接的方法是在灭个文件中再次输入指令,但是这个方法既耗时有容易出错。另外还会有维修问题:如果修改了#define定义的值,就必须在每个文件中修改。更好的做法是把#define指令发放进头文件,然后在每个原文件中使用#define指令包含该文件即可。以下例子:
假设管理4家酒店的客房服务,每家酒店的房价不同,但是每家酒店所有房间的房价相同。对于预定住宿多天的客户,第二天的房费是第一天的95%,第三天的房价是第二天的%95,以此类推,设计一个程序让用户指定酒店和入住天数,然后计算并显示总费用,,同时,程序要实现一分菜单,允许用户反复输入,除非用户选择退出。
//usehotel.c--房间费率程序
//与程序清单9.10一起编译
#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;
case 2:hotel_rate = HOTEL2;
case 3:hotel_rate = HOTEL3;
case 4:hotel_rate = HOTEL4;
default:hotel_rate = 0.0;
printf("Oopen!\n");
break;
}
nights = getnights();
showprice(hotel_rate, nights);
}
printf("thank you and goodbye.\n");
return 0;
}
程序清单9.9包含了main函数提供了整个程序的组织结构。
//hotel.c--酒店管理函数9.10
#include "hotel.h"
int mian(void)
{
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 night are needed?");
while (scanf("%d", &nights) != 1)
{
scanf("%*s"); //处理非整数输入
printf("please enter an integer ,such as 2.\n");
}
return nighs;
}
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 totsl cost whill be $%0.2f.\n", total);
}这个程序清单包含了支持的函数,我们假设他在独立的文件中//hotel.h--符号常量和hotel.c中所欲函数的原型
#define QUIT 5
#define HOTELI 180.00
#define HOTEL2 225.00
#define HOTEL3 225.00
#define HOTEL4 355.00
#define STARS "************************************"
//显示选择列表
int menu(void);
//返回预定天数
int getnights(void);
//更具费率 入住时间计算费用
//并显示结果
void showprice(double rate, int nights);
下面是多文件程序的运行是咧
*******************************************************************
enter the number of the desired hotel:
1)fairfileld Arms 2)Hotel Olympic
3) chertworthy plaza 4) hotel stockon
5)quit
**************************************************************************
3
how hany nights are needed?1
the total cost will be $255.00
********************************************************************************
enter the number of the desired hotel:
1)fairfileld Arms 2)Hotel Olympic
3) chertworthy plaza 4) hotel stockon
5)quit
*************************************************************************************
4
how many needed are needed?3
the total cost will be $1012.64
*****************************************************************************
enter the number of the desired hotel:
1)fairfileld Arms 2)Hotel Olympic
3) chertworthy plaza 4) hotel stockon
5)quit
************************************************************************************************
5
thank you goodbye.