系統功能清單
- 密碼驗證入口(三次失敗鎖定)
- 車位狀態管理(編號、佔用狀態)
- 停車登記(車牌、車型、車位、入場時間)
- 取車結算(依停車時長×單價自動計費)
- 資料檔案持久化(二進制讀寫)
- 單價設定、密碼修改、查詢統計
核心資料結構
#define MAX_SPOTS 20
struct ParkedCar {
char plate[20]; // 車牌
char model[20]; // 車型
int spot; // 車位號
char entry_time[30]; // 入場時間字串
int entry_hour; // 入場小時
int entry_min; // 入場分鐘
};
struct ParkedCar cars[MAX_SPOTS];
int total_records = 0;
int rate_per_min = 1; // 預設 1 元/分
char password[20];
關鍵演算法實現
1. 彈性時間計費
int calc_fee(int entry_h, int entry_m, int rate) {
time_t now = time(NULL);
struct tm *t = localtime(&now);
int duration_min = (t->tm_hour - entry_h) * 60 + (t->tm_min - entry_m);
return duration_min * rate;
}
2. 車位衝突檢測
int find_spot(int spot) {
for (int i = 0; i < total_records; i++)
if (cars[i].spot == spot) return i;
return -1; // -1 表示空閒
}
3. 檔案持久化策略
- 啟動時讀取
parking.dat 重建記憶體陣列
- 異動(停車/取車)即時
wb 全量覆寫,簡單可靠
- 密碼獨立存
password.dat
主選單流程
void menu() {
while (1) {
show_menu();
switch (get_choice()) {
case 1: add_car(fopen("parking.dat", "ab")); break;
case 2: list_all(); break;
case 3: pickup_by_plate(); break;
case 4: pickup_by_spot(fopen("parking.dat", "wb")); break;
case 5: query_by_plate(); break;
case 6: query_by_spot(); break;
case 7: set_rate(); break;
case 8: change_password(); break;
case 9: system("clear"); break;
case 0: return;
}
}
}
安全性與健壯性補強
- 輸入驗證:車位範圍、車牌長度、菜單選項邊界
- 檔案操作失敗重試機制
- 密碼輸入回顯遮罩(
getch())
- 時間計算跨日處理(以
time_t 秒數差值更可靠)
擴展建議
- 引入鏈表/動態陣列突破
MAX_SPOTS 限制
- 採用 SQLite 替代二進制檔案,支援並發與查詢
- 增加車牌識別介面(攝影機/Ocr)實現無感入場
- Web 管理後台(CGI/嵌入式 HTTP Server)