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