求大神看看,在能考虑所有打车情况下,用if+switch语句是否要简洁一点?
#include <stdio.h>
int Price(int d,int t)
{
float cost;
int a;
if (d<=0)
{
a=0;
}
else if(d>0&&d<=3)
{
a=1;
}
else if(d>3)
{
a=2;
}
switch(a)
{
case 0:
printf("%s","请输入0以上的距离哦~");
break;
case 1:
if(t>24)
{
printf("%s","请输入正确的打车时间!");
}
else if((t<=5&&t>=0)||(t<=24&&t>=23))
{
cost=13*1.2+1;
printf("%f",cost);
}
else
{
cost=13+1;
printf("%f",cost);
}
break;
case 2:
if(t<=24)
{
if((t<=5&&t>=0)||(t<=24&&t>=23))
{
cost=(2.3*d+13)*1.2+1;
printf("%f",cost);
}
else
{
cost=2.3*d+13+1;
printf("%f",cost);
}
}
else
{
printf("%s","请输入正确的打车时间!");
}
break;
}
return 0;
}
int main()
{
Price(5,23);
return 0;
}