|
因本人不再做技术,
这个blog将不再连载技术文章,
只作为心情点滴的记录,
想学技术的请绕道,谢谢!
联系方式:
feiyu_lili@163.com |
时 间 记 忆 |
« | August 2025 | » | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | | | | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | | | | | | | |
|
blog名称:飞鱼的成长 日志总数:120 评论数量:488 留言数量:18 访问次数:1047671 建立时间:2006年2月27日 |
 | | | |
|
|
今天基本看完了函数这章,今天印象最深的是多源代码的文件程序编译。以前虽然也有分模块的概念但都是写在同一个文件内的。这次学会多文件的模块化。还有一个就是把宏定义单独的放在一个.h文件里面。
多源代码文件程序的编译
usehotel.c 控制模块
/* usehotel.c -- room rate program */
/* compile with Listing 9.10 */
#include <stdio.h>
#include "hotel.h" /* defines constants, declares functions */
int main(void)
{
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.");
return 0;
}
hotel.c函数支持模块
***************************************************
/* hotel.c -- hotel management functions */
#include <stdio.h>
#include "hotel.h"
int menu(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 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);
}
hotel.h头文件
*****************************************************
/* hotel.h -- constants and declarations for hotel.c */
#define QUIT 5
#define HOTEL1 80.00
#define HOTEL2 125.00
#define HOTEL3 155.00
#define HOTEL4 200.00
#define DISCOUNT 0.95
#define STARS "**********************************"
// shows list of choices
int menu(void);
// returns number of nights desired
int getnights(void);
// calculates price from rate, nights
// and displays result
void showprice(double rate, int nights);
500)this.width=500'>
|
|
| | |
|