#include <stdio.h>
int cost(int distance,int time)
{
double price = 2.3;
double cost;
if(distance<=3){
cost = 13+1;
}else if(time<=23 && time>5){
cost = (distance-3)*price+1+13;
}else{
cost = (distance-3)*(price*1.2)+1+13;
}
}
int main()
{
double allcost = cost(12,9)+cost(12,18);
printf("小明每天打车是%f块钱",allcost);
return 0;
}
#include <stdio.h> float cost(int distance,int time) { double price = 2.3; double cost; if(distance <= 3) { cost = 13 + 1; } else if(time < 23 && time>=5) { cost = (distance - 3) * price + 1 + 13; } else { cost = (distance - 3) * (price * 1.2) + 1 + 13; } return cost; } int main() { double allcost = cost(12,9)+cost(12,18); printf("小明每天打车是%0.1f块钱",allcost); return 0; } 1、定义cost函数为int型却没有返回值,即没有return cost。
#include <stdio.h>
int cost(int distance,int time)
{
double price = 2.3;
double costs;
if(distance<=3){
costs = 14;
}else if(time<=23 || time>=5){
costs = (distance-3)*price+1+13;
printf("白天车费为:%f\n",costs);
}else{
costs = (distance-3)*(price*1.2)+1+13;
printf("夜间车费为:%f\n",costs);
}
}
int main()
{
double allcost = cost(12,9)+cost(12,18);
printf("小明每天打车是%f块钱",allcost);
return 0;
}