用C++实现停车场管理系统

打印 上一主题 下一主题

主题 1617|帖子 1617|积分 4851

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
该系统具有以下功能:
  - 停车:输入车牌号和进入时间,自动分配停车位编号,如果停车场已满则提示停车场已满。
  - 离开停车场结算停车费用:输入停车位编号和离开时间,自动盘算停车费用,如果已结算则提示已结算。
  - 显示停车记录:按照停车位编号顺序显示停车记录,包括车牌号、进入时间、离开时间和停车费用。
  - 退出系统:结束程序。
  该程序通过利用 vector 存储停车记录,动态分配停车位编号,并通过 switch-case 语句实现菜单选择功能。
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. using namespace std;
  5. const int MAX_CAPACITY = 100;
  6. // 停车记录结构体
  7. struct ParkingRecord {
  8.     int id;                 // 停车位编号
  9.     string licensePlate;    // 车牌号
  10.     int timeIn;             // 进入时间
  11.     int timeOut;            // 离开时间
  12.     float cost;             // 停车费用
  13. };
  14. // 停车场类
  15. class ParkingLot {
  16. public:
  17.     ParkingLot();
  18.     // 显示菜单
  19.     void showMenu();
  20.     // 停车
  21.     void parkCar();
  22.     // 离开停车场结算停车费用
  23.     void checkOutCar();
  24.     // 显示停车记录
  25.     void showParkingRecords();
  26. private:
  27.     vector<ParkingRecord> parkingLot;   // 停车场
  28.     int currentCapacity;                // 当前停车场容量
  29. };
  30. // 构造函数
  31. ParkingLot::ParkingLot() {
  32.     currentCapacity = 0;
  33. }
  34. // 显示菜单
  35. void ParkingLot::showMenu() {
  36.     cout << "Welcome to the Parking Lot Management System!" << endl;
  37.     cout << "1. Park a car" << endl;
  38.     cout << "2. Check out a car" << endl;
  39.     cout << "3. Show parking records" << endl;
  40.     cout << "4. Exit" << endl;
  41.     cout << "Please enter your choice: ";
  42. }
  43. // 停车
  44. void ParkingLot::parkCar() {
  45.     if (currentCapacity >= MAX_CAPACITY) {
  46.         cout << "Sorry, the parking lot is full." << endl;
  47.         return;
  48.     }
  49.     ParkingRecord newRecord;
  50.     cout << "Please enter the license plate number (up to 9 characters): ";
  51.     cin >> newRecord.licensePlate;
  52.     cout << "Please enter the time in (in minutes since 00:00): ";
  53.     cin >> newRecord.timeIn;
  54.     newRecord.id = currentCapacity + 1;
  55.     parkingLot.push_back(newRecord);
  56.     currentCapacity++;
  57.     cout << "The car is parked at spot " << newRecord.id << "." << endl;
  58. }
  59. // 离开停车场结算停车费用
  60. void ParkingLot::checkOutCar() {
  61.     int id;
  62.     cout << "Please enter the parking spot number: ";
  63.     cin >> id;
  64.     if (id < 1 || id > currentCapacity) {
  65.         cout << "Invalid parking spot number." << endl;
  66.         return;
  67.     }
  68.     ParkingRecord *record = &parkingLot[id-1];
  69.     if (record->timeOut > 0) {
  70.         cout << "The car has already been checked out." << endl;
  71.         return;
  72.     }
  73.     cout << "Please enter the time out (in minutes since 00:00): ";
  74.     cin >> record->timeOut;
  75.     float hours = (record->timeOut - record->timeIn) / 60.0;
  76.     record->cost = hours * 1.5;
  77.     cout << "The cost for parking is $" << record->cost << "." << endl;
  78. }
  79. // 显示停车记录
  80. void ParkingLot::showParkingRecords() {
  81.     if (currentCapacity == 0) {
  82.         cout << "There is no car parked in the parking lot." << endl;
  83.         return;
  84.     }
  85.     cout << "Parking records:" << endl;
  86.     cout << "ID\tLicense Plate\tTime In\tTime Out\tCost" << endl;
  87.     for (int i = 0; i < currentCapacity; i++) {
  88.     ParkingRecord record = parkingLot[i];
  89.     cout << record.id << "\t" << record.licensePlate << "\t\t"
  90.          << record.timeIn << "\t" << record.timeOut << "\t"
  91.          << record.cost << endl;
  92.     }
  93. // 主函数
  94. int main() {
  95. ParkingLot parkingLot;
  96. int choice;
  97. do {
  98.     parkingLot.showMenu();
  99.     cin >> choice;
  100.     switch (choice) {
  101.         case 1:
  102.             parkingLot.parkCar();
  103.             break;
  104.         case 2:
  105.             parkingLot.checkOutCar();
  106.             break;
  107.         case 3:
  108.             parkingLot.showParkingRecords();
  109.             break;
  110.         case 4:
  111.             cout << "Thank you for using the Parking Lot Management System." << endl;
  112.             break;
  113.         default:
  114.             cout << "Invalid choice." << endl;
  115.             break;
  116.     }
  117.     cout << endl;
  118. } while (choice != 4);
  119. return 0;
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

农妇山泉一亩田

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表