某航空公司规定:在旅游旺季(4、5、9、10月), 如果订票数量20张及以上,则优惠20%,20张以下,则优惠10%;在旅游淡季(1-3、6-8、11、12月),如果订票数量20张及以上,则优惠40%,20张以下,则优惠20%。编写程序,计算机票总价格。机票单价、订票时间、订票数量由键盘输入。
#include<stdio.h>
#include<stdlib.h>
double amount (double unitPrice, int month, int number); //总价计算
void season (int month); //判断是否是旺季
double discount[2] = { 0.2, 0.4 }; // 折扣初始化为淡季值
int main(int argc, char const *argv[])
{
//初始化
double unitPrice = 0;
int month = 0;
int number = 0;
//输入
printf("请输入机票单价、订票时间、订票数量:");
scanf("%lf%d%d",&unitPrice, &month, &number );
//判断与输出
if(unitPrice>0 && month>=1 && month<=12 && number>=0 ){
printf("机票总价格为:%g元.。\n", amount(unitPrice, month, number));
}
else{
printf("输入数据有误。\n");
}
system("pause");
return 0;
}
double amount (double unitPrice, int month, int number)
{
season(month);
int i;
double amount = 0;
if(number<20){
i = 0;
}
else{
i = 1;
}
amount = unitPrice * (1 - discount[i]) * number;
return amount;
}
void season (int month)
{
int busy;
switch (month) {
case 1: busy = 0; break;
case 2: busy = 0; break;
case 3: busy = 0; break;
case 4: busy = 1; break;
case 5: busy = 1; break;
case 6: busy = 0; break;
case 7: busy = 0; break;
case 8: busy = 0; break;
case 9: busy = 1; break;
case 10: busy = 1; break;
case 11: busy = 0; break;
case 12: busy = 0; break;
}
if(busy){
discount[0] = 0.1;
discount[1] = 0.2;
}
}