誰能給我解釋一下調用那一段,謝謝
#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;? ??
}
2018-12-13
嘛耶 ?? ~ ?? QAQ希望采納~~~不會的可私戳
2018-12-13
#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;???? }