main.c——房间费率程序
#include <stdio.h>
#include <stdlib.h>
#include "hotel.h"
int main(int argc, char *argv[]) {
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 5: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;
}
hotel.c——酒店管理函数
#include <stdio.h>
#include "hotel.h"
int menu(void)
{int code,status;
printf("\n%s%s\n",STARS,STARS);
printf("请输入各类房间前的号码\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("请输入各类房间前的号码\n");
}
return code;
}
int getnights(void)
{
int nights;
printf("你要住几天晚上\n");
while(scanf("%d",&nights)!=1)
{scanf("%*s");
printf("请输入数字,比如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("您的消费是%0.2f.\n",total);
}
hotel.h——头文件
#define QUIT 5
#define HOTEL1 50.00
#define HOTEL2 60.00
#define HOTEL3 70.00
#define HOTEL4 80.00
#define DISCOUNT 0.95
#define STARS"******************"
int menu(void);
int getnights(void);
void showprice(double rate,int night);
scanf("%s")*
scanf("%s")表示跳至下一空白字符,这里主要是中间的字符起的作用。比如:
scanf("%s")表示跳至下一空白字符,这里主要是中间的字符起的作用。比如:
int n;
scanf("%*d %*d %d",&n);
printf("%d",n);
return 0;
如果输入的是1 2 3,那么输出的是3,因为前两个已经忽略啦。