题目要求
北京市出租车打车计费规则如下:
每公里单价计费2.3元
起步价13元(包含3公里)
晚上23点(含)至次日凌晨5点(不含)打车,每公里单价计费加收20%。
每次乘车加收1元钱的燃油附加税。
小明每天上下班都要打车,公司和家的距离为12公里,上午上班时间为9点,下午下班时间为6点。
请编写一个小程序计算小明每天打车的总费用。
这道题出自慕课网,代码如下
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 32 33 34 35 36 37 38 39 40 41
| #include <stdio.h> double price =0.0; int real_price; int price_check(int t){ if(t>=23||t<=5) { return 1; } else{ return 0; } } double function_price(int check,double mile){ double pre_price=0; if (check==1) { pre_price=2.3+(2.3*0.2); }else if(check==0) { pre_price=2.3; } double real_mile=0.0; if(mile>3){ real_mile=mile-3; }else{ real_mile=3; } double total_price=13+(pre_price*real_mile)+1; return total_price; } int main() { double morning_price=0.0; morning_price=function_price(price_check(9),12); double evening_price=0.0; evening_price= function_price(price_check(18),12); double total=morning_price+evening_price; printf("总费用为%.1f",total);
return 0; }
|
自我总结:代码还是过于臃肿,日后进行精简。