qq_tor_1
2018-09-03 12:39
#include <stdio.h>
int cost(double distance,int time)
{
double sum;
if(distance<=3)
{
sum=13;
}
else if(distance>3)
{
if(time<=5||time>23)
{
sum=13+(distance-3)*2.3*1.2;
}
else if(time>5&&time<=23)
{
sum=13+(distance-3)*2.3;
}
}
return sum+1;
}
int main()
{
printf("小明路费为%d\n",cost(12,18)+cost(12,9));
return 0;
}
你自定义函数的时候就写的是整型,把int cost(double distance,int time)改成double cost(double distance,double time),然后再把printf中的%d改成%f就不会报错
#include <stdio.h>
float totalprice(int time,int distance)
{
float perprice=2.3,totalprice;
if(time>=5&&time<23);
else
perprice+=perprice*0.2;
if(distance<=3)
return 14;
else
{
totalprice=13+(distance-3)*perprice+1;
return totalprice;
}
}
int main()
{
int time1=9,time2=18,distance=12;
float price1=totalprice(time1,distance);
float price2=totalprice(time2,distance);
printf("%.2f",price1+price2);
return 0;
}
并且将printf中%d改为%f会报错
C语言入门
926210 学习 · 20797 问题
相似问题