/*设计一个酒店预订系统能实现如下功能:
1.显示有酒店信息2.询问顾客需要订几天3.告知顾客一共的费用
1.显示有酒店信息2.询问顾客需要订几天3.告知顾客一共的费用
*/
要求:1.使用函数来编写 2.将最基本的参数定义成头文件
整个过程分为三个部分,首先是自己编辑的头文件
#ifndef HOTEL_H_INCLUDED
#define HOTEL_H_INCLUDED
#define Hotel1 872.0 //各个酒店一晚的费用
#define Hotel2 1838.0
#define Hotel3 789.0
#define Hotel4 1658.0
#define Discount 0.95 //折扣
#endif // HOTEL_H_INCLUDED
int Menu(void); //定义菜单函数
int Days(void); //询问预订的天数
void TotalPrices(double price,int day);//总的花费的金额
这里主要要注意的就是在函数申明里需要注意该怎么申明?
然后是函数.c的部分:
#include <stdio.h>
#include <stdlib.h>
#include "Hotel.h" //特别注意,要使自己编辑的头文件有效,这个语句必不可少。
Menu()
{
//int choice;
printf("*********************************\n");
printf("请选择你需要入住的酒店\n");
printf("1.Belloy Saint-Germain Paris $872\n");
printf("2.Hotel champs Elysees Plaza Paris $1838\n");
printf("3.Hotel Astra Opera-Astotel Paris $789\n");
printf("4.Hotel Scribe Paris by SOFITEL $1658\n");
printf("5.退出系统\n");
printf("*********************************\n");
//scanf("%d",&choice);
//Judgment(choice);
//return choice;
//这里需要一个判定函数,当输入的不是数字或者不在1到5之间,重新输入
}
Days()
{
int day;
printf("请输入您要住几天\n");//添加一个判定的过程
while(scanf("%d",&day) != 1) //这是一个判定过程,非常重要,因为如果输入的是整形也就是整数时,scanf()的值是1,否则则为0
{
scanf("%*s");
printf("输入必须为整数,请重新输入\n");
printf("请输入您要住几天\n");
}
return day;
}
TotalPrices(double price,int day)
{
int i;
double TotalP=0;
for(i=0; i<day; i++)
{
TotalP += price;
price *= Discount;
}
printf("总共住了%d天,共需要花费%.2lf",day,TotalP);
}
主代码:
#include <stdio.h>
#include <stdlib.h>
#include "Hotel.h"
/*设计一个酒店预订系统能实现如下功能:
1.显示有酒店信息2.询问顾客需要订几天3.告知顾客一共的费用
*/
int main()
{
int day,price;
int choice;
int result;
Menu();
while((result = scanf("%d",&choice)) != 1 ||choice < 1 ||choice > 5)
//出错了,用户输入的有误
{
if(result != 1)
{
scanf("%*s"); //用来处理非整数输入,不然的话输入缓存会出现问题
}
printf("输入有误,请重新输入\n");
scanf("%d",&choice);
}
switch(choice)
{
case 1:
price =Hotel1;
break;
case 2:
price =Hotel2;
break;
case 3:
price =Hotel3;
break;
case 4:
price =Hotel4;
break;
case 5:
exit(0);
}
day = Days();
TotalPrices(price,day);
}