继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

cyuyanshiyanbaogaodan

lxq99
关注TA
已关注
手记 9
粉丝 2
获赞 13

/*

  1. 每公里单价计费2.3元
  2. 起步价13元(包含3公里)//////////////

  3. 晚上23点(含)至次日凌晨5点(不含)打车,每公里单价计费加收20%。

  4. 每次乘车加收1元钱的燃油附加税。

小明每天上(shijian:9)下班(shijian:??)都要打车,公司和家的距离为12公里,上午上班时间为9点,下午下班时间为6点。

请编写一个小程序计算小明每天打车的"总费用"---------时间相关,距离相关,4。

*/

include <stdio.h>

////////函数的返回值 ,函数名,函数的参数:时间,距离 ,任何一个人,在不同的时间,不同车程使用该函数来计算本次的费用
double getTaxiPrice(int hours,int distance)
{
double totalPrice = 0.0; //定义打车费用
double perPrice = 2.3; //定义每公里单价计费
int startPrice = 13; //定义起步价
///1.时间相关
if(hours<0 || hours>24){
printf("请填写正确的时间\n");
return 0;
}
else if(!(hours>=5 && hours<23)) //判断打车时间是否要增加费用
{
perPrice *= 1.2; //费用增加20%
}

/////////2.距离相关 
if(distance<=0)
{
    return 0;
}
else if(distance >3)        //判断公里数
{
    totalPrice = startPrice +(distance - 3)*perPrice;   //计算价钱
}
else
{
    totalPrice = startPrice; /////////////////   
}

//////
totalPrice++;   //加一块钱的燃油费
return totalPrice;////////消费的乘车计费 

}
int main()
{
int moring = 9; //定义上午打车时间
int afternoon = 3; //定义下午打车时间
int distance = 27; //定义打车公里数
double totalPrice = 0; //定义总费用
////////测试一下不同时间,不同距离,可能非法的时间或距离;

if(getTaxiPrice(moring,distance) != 0)
{
    totalPrice = getTaxiPrice(moring,distance); //调用计算费用的函数
}
else if(totalPrice != 0)
{
    totalPrice += getTaxiPrice(afternoon,distance); //调用计算费用的函数
}
printf("小明每天打车的总费用是:%.2f\n",totalPrice);    //输出
return 0;    

}

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP