染小指
2018-12-11 15:07
#include <stdio.h>
double getTaxiPrice(int hours,int distance)
{
double totalPrice = 0.0; //定义打车费用
double perPrice = 2.3; //定义每公里单价计费
int startPrice = 13; //定义起步价
if(hours<0 || hours>24){
printf("请填写正确的时间\n");
return 0;
}
else if(!(hours>=5 && hours<23)) //判断打车时间是否要增加费用
{
perPrice *= 1.2; //费用增加20%
}
if(distance >3) //判断公里数
{
totalPrice = startPrice +(distance - 3)*perPrice; //计算价钱
}
else
{
totalPrice = startPrice;
}
totalPrice++; //加一块钱的燃油费
return totalPrice;
}
int main()
{
int moring = 9; //定义上午打车时间
int afternoon = 18; //定义下午打车时间
int distance = 12; //定义打车公里数
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;
}
嘛耶 ~ QAQ希望采纳~~~不会的可私戳
#include <stdio.h>
//这里声明了一个coast计费函数
double coast(double time,double distance){
double price=0;
if(time>=5&&time<23){//判断输入的是哪个时间段,这里是判断时间在不在5<=time<23,也就是[5,23) if(distance<=3)//这里判断超没超过3公里,没超过就13元
price=13+1; //printf("价格为13元,别忘记燃油费一元");
else{//
price=13+(distance-3)*2.3+1;//这里看超过3公里了么,超过就要按公里计费了哦
}
}
else {//这里是判断时间在不在time>23,time<5
price=13+(distance-3)*2.3*0.2+1;//这里需要乘20%,也就是0.2了
}
}
int main(){
//下面开始调用coast函数了
double price=coast(9,12)+coast(6,12);//总和为早上的9点12公里加晚上的6点12公里
printf("哎呀,竟然花了%f",price);
return 0;
}我觉得我的代码可能更好理解,下面给你解释你的代码,如下:首先你要知道的是getTaxiPrice(int hours,int distance)这个函数是用来计算一次打车价格的,也就是只计算上午或者只计算下午,其次totalprice是总价,start price起步价(也就是那个13块钱),其次我们看代码,在main函数那里,我直接在代码旁边注释了。。。
int main(){
int moring = 9;
//定义上午打车时间,if里面的 totalPrice = getTaxiPrice(moring,distance)就是用的你现在定义的morning的值
int afternoon = 18; //定义下午打车时间, 下面的totalPrice += getTaxiPrice(afternoon,distance) 就是讲
int distance = 12; //定义打车公里数
double totalPrice = 0; //定义总费用,这里是总费用初始化为0
if(getTaxiPrice(moring,distance) != 0){
totalPrice = getTaxiPrice(moring,distance);
//printf("这里total price是计算的上午打车价格,数值为%f",totalPrice);
/*这里调用计算费用的函数计算morning也就是上午打车的价格,所以下面代码的total price已经是有上午价格值了,你可以添加一个printf("这里total price是计算的上午价格,数值为%f",totalPrice);试试看是不是有值了~*/
}
else if(totalPrice != 0) {
//这里我用简单方式说明一下totalPrice += getTaxiPrice(afternoon,distance);这个语句
/*totalPrice += getTaxiPrice(afternoon,distance);这里这个等式我换个方法给你讲,你就懂了,第一步定义一个double类型变量为下班打车费拼音,
double xiabandachefei=getTaxiPrice(afternoon,distance);
//getTaxiPrice(afternoon,distance)是调用main外边的计算函数计算下午的打车费用
printf("这里开始计算下午下班的费用了,数值为%f",xiabandachefei);
totalPrice += xiabandachefei;
//这里 totalPrice += xiabandachefei;
//就等于 totalPrice = totalPrice+xiabandachefei;
*/
//下面是原代码
totalPrice += getTaxiPrice(afternoon,distance); //调用计算费用的函数
/*注意这里的totalprice已经是上午打车的价格值了哦~再次调用就是计算好下午下班打车 +=就是用计算好的上午的加下午的了,a+=b本质就是a=a+b;*/
}
printf("小明每天打车的总费用是:%.2f\n",totalPrice); //输出
return 0;
}C语言入门
927900 学习 · 21542 问题
相似问题