随笔 - 66  文章 - 0  评论 - 0  阅读 - 22203

C语言知识点二十五: 头文件的使用

头文件的使用:

//usehotel.c
#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; 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.");
	getchar();
	getchar();
	return 0;
}


//hotel.c
#include<stdio.h>
#include "hotel.h"    
int menu(void)
{
	int code, status;

	printf("\n****************************************\n");
	printf("Enter the number of the desired hotel:\n");
	printf("1) Fairfield Arms 2) Hotel Olympic\n");
	printf("3) Chertworthy Plaza 4) The Stokton\n");
	printf("5) quit\n");
	printf("****************************************\n");
	while((status = scanf("%d", &code)) != 1 || (code < 1 || code > 5))
	{
		if(status != 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 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
#define QUIT	5
#define HOTEL1		80.00
#define HOTEL2		125.00
#define HOTEL3		155.00
#define HOTEL4		200.00
#define DISCOUNT	0.95

int menu(void);

int getnights(void);

void showprice(double,int);

这是代码文件的百度云网址:

https://pan.baidu.com/s/1WSO-9NoHlh4TCkW2iSNINg

 

posted on   Daniel_lmz  阅读(105)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

点击右上角即可分享
微信分享提示