#include <stdio.h>
//判断是否需要额外20%费用
float getAddition(int times){
if(times>=23 || times<5){
return 1.2;
}else{
return 1;
}
}
//获取往返路程花费 需要来回的时间 路程distance
float getCost(int goTime,int backTime,int distance)
{
float cost=13;
if(distance<=3){
return cost+1;
}else{
cost=distance*getAddition(goTime)*2.3+1;
cost+=distance*getAddition(backTime)*2.3+1;
return cost<13? 13: cost;
}
}
int main()
{
printf("上班打车总花费是%f",getCost(4,24,12));
return 0;
}
float cost=13; ??你怎么不直接写这样 float cost;
if(distance<=3){
return cost+1; 这里返回值只有单程啊 return 14*2;
}else{
cost=distance*getAddition(goTime)*2.3+1; 这里cost被重新赋值,那你之前的赋值就没意义了
cost+=distance*getAddition(backTime)*2.3+1;
return cost<13? 13: cost; 没必要用一次判断,cost不可能小于13