請(qǐng)大神幫忙看看還有哪里不對(duì)的嗎?(運(yùn)行結(jié)果已正確)
#include <stdio.h>
?double getTotalCost(float n,float t)
? ? {
? ? ? ? double cost;
? ? ? ? if(n<=3)
? ? ? ? {
? ? ? ? ? ? cost=13+1;
? ? ? ? }
? ? ? ? else
? ? ? ? {
? ? ? ? ? ? if(5<=t&&t<23)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? cost=13+1+2.3*(n-3);
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? cost=13+1+2.3*(n-3)*1.2;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return cost;
? ? }
int main()
{
? ? printf("小明每天打車總費(fèi)用為%f:",getTotalCost(12,9)+getTotalCost(12,18));
? ? return 0;
}
2017-06-25
我是做安卓的,因?yàn)楣ぷ餍枰_始學(xué)習(xí)C語(yǔ)言
貼上我自己的代碼,相互交流哈。
#include <stdio.h>
int getRent(int mile, int time) {
? ? int startRent = 13; //起步價(jià)
? ? int rentPerMile = 2.3; //每公里單價(jià)
? ? int extraRent = 1; //燃油附加稅
? ? int rent = 0; //打車費(fèi)用
? ??
? ? //打車時(shí)間小于0或大于24都是不合法的,輸出提示信息并結(jié)束方法
? ? if(time < 0 || time > 24) {
? ? ? ? printf("您輸入的打車時(shí)間為:%d,這不是一個(gè)合法的時(shí)間,請(qǐng)重新檢查您的輸入??!");
? ? ? ? return 0;
? ? }
? ? //在23點(diǎn)和5點(diǎn)前打車,每公里單價(jià)計(jì)費(fèi)加收20%
? ? if(time >= 23 || time < 5) {
? ? ? ? rentPerMile *= rentPerMile*1.2;
? ? }
? ??
? ? if(mile <= 3) {
? ? ? ? rent = startRent + extraRent;
? ? } else {
? ? ? ? rent = startRent + rentPerMile*(mile-3) + extraRent;
? ? }
? ? return rent;
}
int main()
{
? ? int onWorkRent = getRent(12, 9);
? ? int offWorkRent = getRent(12, 18);
? ? printf("您的上班打車費(fèi)用為:%d 元\n", onWorkRent);
? ? printf("您的下班打車費(fèi)用為:%d 元\n", offWorkRent);
? ? printf("您一天的打車費(fèi)用為:%d 元", onWorkRent+offWorkRent);
? ? return 0;
}
2017-07-03
是呀,用float或者都變了好一些,int不能輸出小數(shù)的
2017-06-25
還是該用float類型,全用int類型,因?yàn)樽詣?dòng)轉(zhuǎn)型等原因,最后的值就不對(duì)了~
2017-06-25
我覺得把起步價(jià)這些值單獨(dú)定義出來(lái), 這樣以后這些值有變化的話, 便于修改。就不用在代碼里面到處去找需要修改的地方了
目前不知道C語(yǔ)言里面是否可以主動(dòng)拋異常,所以只通過(guò)打印的方式提示函數(shù)的參數(shù)傳入有誤。這里只考慮了打車時(shí)間的參數(shù)非法問(wèn)題,沒考慮公里數(shù)為負(fù)等情況。
打車公里數(shù)和時(shí)間都只考慮了int類型的~~
2017-06-25
沒有問(wèn)題吧