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

该系统具有以下功能:

- 停车:输入车牌号和进入时间,自动分配停车位编号,如果停车场已满则提示停车场已满。

- 离开停车场结算停车费用:输入停车位编号和离开时间,自动计算停车费用,如果已结算则提示已结算。

- 显示停车记录:按照停车位编号顺序显示停车记录,包括车牌号、进入时间、离开时间和停车费用。

- 退出系统:结束程序。

该程序通过使用 vector 存储停车记录,动态分配停车位编号,并通过 switch-case 语句实现菜单选择功能。

#include <iostream>
#include <vector>
#include <string>
using namespace std;

const int MAX_CAPACITY = 100;

// 停车记录结构体
struct ParkingRecord {
    int id;                 // 停车位编号
    string licensePlate;    // 车牌号
    int timeIn;             // 进入时间
    int timeOut;            // 离开时间
    float cost;             // 停车费用
};

// 停车场类
class ParkingLot {
public:
    ParkingLot();

    // 显示菜单
    void showMenu();

    // 停车
    void parkCar();

    // 离开停车场结算停车费用
    void checkOutCar();

    // 显示停车记录
    void showParkingRecords();

private:
    vector<ParkingRecord> parkingLot;   // 停车场
    int currentCapacity;                // 当前停车场容量
};

// 构造函数
ParkingLot::ParkingLot() {
    currentCapacity = 0;
}

// 显示菜单
void ParkingLot::showMenu() {
    cout << "Welcome to the Parking Lot Management System!" << endl;
    cout << "1. Park a car" << endl;
    cout << "2. Check out a car" << endl;
    cout << "3. Show parking records" << endl;
    cout << "4. Exit" << endl;
    cout << "Please enter your choice: ";
}

// 停车
void ParkingLot::parkCar() {
    if (currentCapacity >= MAX_CAPACITY) {
        cout << "Sorry, the parking lot is full." << endl;
        return;
    }

    ParkingRecord newRecord;
    cout << "Please enter the license plate number (up to 9 characters): ";
    cin >> newRecord.licensePlate;
    cout << "Please enter the time in (in minutes since 00:00): ";
    cin >> newRecord.timeIn;

    newRecord.id = currentCapacity + 1;
    parkingLot.push_back(newRecord);
    currentCapacity++;

    cout << "The car is parked at spot " << newRecord.id << "." << endl;
}

// 离开停车场结算停车费用
void ParkingLot::checkOutCar() {
    int id;
    cout << "Please enter the parking spot number: ";
    cin >> id;

    if (id < 1 || id > currentCapacity) {
        cout << "Invalid parking spot number." << endl;
        return;
    }

    ParkingRecord *record = &parkingLot[id-1];
    if (record->timeOut > 0) {
        cout << "The car has already been checked out." << endl;
        return;
    }

    cout << "Please enter the time out (in minutes since 00:00): ";
    cin >> record->timeOut;

    float hours = (record->timeOut - record->timeIn) / 60.0;
    record->cost = hours * 1.5;

    cout << "The cost for parking is $" << record->cost << "." << endl;
}

// 显示停车记录
void ParkingLot::showParkingRecords() {
    if (currentCapacity == 0) {
        cout << "There is no car parked in the parking lot." << endl;
        return;
    }

    cout << "Parking records:" << endl;
    cout << "IDtLicense PlatetTime IntTime OuttCost" << endl;
    for (int i = 0; i < currentCapacity; i++) {
    ParkingRecord record = parkingLot[i];
    cout << record.id << "t" << record.licensePlate << "tt" 
         << record.timeIn << "t" << record.timeOut << "t" 
         << record.cost << endl;
    }

// 主函数
int main() {
ParkingLot parkingLot;
int choice;
do {
    parkingLot.showMenu();
    cin >> choice;

    switch (choice) {
        case 1:
            parkingLot.parkCar();
            break;
        case 2:
            parkingLot.checkOutCar();
            break;
        case 3:
            parkingLot.showParkingRecords();
            break;
        case 4:
            cout << "Thank you for using the Parking Lot Management System." << endl;
            break;
        default:
            cout << "Invalid choice." << endl;
            break;
    }

    cout << endl;
} while (choice != 4);

return 0;
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码

)">
< <上一篇

)">
下一篇>>