某城市停車場收費規則較為複雜:
// 常量定義
const int Bigcar_rule = 5; // 大車白天首15分
const float Smallcar_rule = 2.5f; // 小車白天首15分
const float Bigcar_att = 7.5f; // 大車白天後續15分
const float Smallcar_att = 3.75f; // 小車白天後續15分
const int Bigcar_night = 2; // 大車夜間2小時
const int Smallcar_night= 1; // 小車夜間2小時
const int Per_time = 15; // 白天計費粒度(分)
// 入口函數
int StatisticsTime(int sy, int sm, int sd, int sh, int smi,
int ey, int em, int ed, int eh, int emi,
int car_type) {
// 迭代累加 sumday, sumhour_day, sumhour_night, summinute
// 呼叫 StatisticsCost 計算並輸出總費用
}
// 主程序交互
int main() {
int sy, sm, sd, sh, smi, car_type;
int ey, em, ed, eh, emi;
printf("請輸入進場時間 (YYYY MM DD HH MI): ");
scanf("%d %d %d %d %d", &sy, &sm, &sd, &sh, &smi);
printf("車型 (1小車 2大車): ");
scanf("%d", &car_type);
printf("請輸入出場時間 (YYYY MM DD HH MI): ");
scanf("%d %d %d %d %d", &ey, &em, &ed, &eh, &emi);
if (Compare(sy,sm,sd,sh,smi, ey,em,ed,eh,emi)) {
StatisticsTime(sy,sm,sd,sh,smi, ey,em,ed,eh,emi, car_type);
} else {
printf("輸入時間邏輯錯誤\\n");
}
return 0;
}
在 `StatisticsTime` 的主循環中,每次推進一個計費粒度(白天 15分、夜間 2小時),需動態判斷當前時刻屬於哪個時段:
if (sh >= 21 || sh < 7) { // 夜間
sumhour_night += StatisticsHour(); // 推進 2 小時
sh += 2;
if (sh >= 7 && sh < 9) { sh = 7; smi = 0; } // 修正到白天起點
} else { // 白天
sumhour_day += StatisticsHour(); // 推進 1 小時
sh++;
if (sh >= 21) smi = 0; // 修正到夜間起點
}
// 處理日期進位...
此邏輯確保在 07:00 和 21:00 邊界自動切換計費模式,並正確處理跨日、跨月、跨年場景。