出租车计价器:C语言计算出租车费用
#include <stdio.h> #include <math.h>
int main() { float distance, waiting_time, total_cost; int base_distance = 3; float base_cost = 10.0; float per_km_cost_within_10km = 2.0; float per_km_cost_above_10km = 3.0; float waiting_cost_per_5_min = per_km_cost_within_10km / 5.0;
printf('请输入行驶里程(公里):');
scanf('%f', &distance);
printf('请输入等待时间(分钟):');
scanf('%f', &waiting_time);
// 计算行驶费用
if (distance <= base_distance) {
total_cost = base_cost;
} else if (distance <= 10) {
total_cost = base_cost + per_km_cost_within_10km * (distance - base_distance);
} else {
total_cost = base_cost + per_km_cost_within_10km * (10 - base_distance) + per_km_cost_above_10km * (distance - 10);
}
// 计算等待费用
float waiting_distance = waiting_time / 5.0;
total_cost += waiting_distance * waiting_cost_per_5_min;
// 四舍五入保留到元
total_cost = round(total_cost);
printf('乘客应支付的车费为%.0f元\n', total_cost);
return 0;
}
原文地址: https://www.cveoy.top/t/topic/l5ay 著作权归作者所有。请勿转载和采集!