鱼C论坛

 找回密码
 立即注册
查看: 2879|回复: 7

[已解决]求鱼油帮忙

[复制链接]
发表于 2018-4-16 18:15:25 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

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

x
题目:某商场有一个100个车位的停车场,当车位未满时,等待的车辆可以进入并计时;当车位已满时,必须有车辆离开,等待的车辆才能进入;当车辆离开时计算停留的的时间,并且按照每小时1元收费。
汽车的输入信息格式可以是(进入/离开,车牌号,进入/离开时间),要求可以随时显示停车场内的车辆信息以及收费历史记录。
这道题目对我来说太难了,求大神帮忙!
最佳答案
2018-4-18 18:27:21
修复bug

main.cpp
#include "CarParking.h"
#include <iostream>

CarParking car_parking;

int main()
{
        car_parking.EnterParking(new Car("晋H·A1363"), "2018/04/18 15:24:31");
        car_parking.EnterParking(new Car("晋H·YQ911"), "2018/04/18 15:43:52");

        Car *car;
        
        car = car_parking.LeaveParking("晋H·YQ911", "2018/04/18 16:46:11");
        //std::cout << car->license_plate_number << std::endl;
        delete car;
        car_parking.ParkingStatus();

        car = car_parking.LeaveParking("晋H·A1363", "2018/04/18 16:47:37");
        //std::cout << car->license_plate_number << std::endl;
        delete car;
        car_parking.ParkingStatus();

        car_parking.EnterParking(new Car("?????????"), "2018/04/18 17:56:39");
        car = car_parking.LeaveParking("?????????", "2018/04/18 17:57:01");
        delete car;
        car_parking.ParkingStatus();

        car_parking.EnterParking(new Car("?????????"), "2018/04/18 17:56:39");
        car = car_parking.LeaveParking("?????????", "2018/04/18 17:57:01");
        delete car;
        car_parking.ParkingStatus();

        car_parking.EnterParking(new Car("?????????"), "2018/04/18 17:56:39");
        car = car_parking.LeaveParking("?????????", "2018/04/18 17:57:01");
        delete car;
        car_parking.ParkingStatus();

        car_parking.EnterParking(new Car("?????????"), "2018/04/18 17:56:39");
        car = car_parking.LeaveParking("?????????", "2018/04/18 17:57:01");
        delete car;
        car_parking.ParkingStatus();

        car_parking.EnterParking(new Car("?????????"), "2018/04/18 17:56:39");
        car = car_parking.LeaveParking("?????????", "2018/04/18 17:57:01");
        delete car;
        car_parking.ParkingStatus();


        car_parking.EnterParking(new Car("?????????"), "2018/04/18 17:56:39");
        car_parking.EnterParking(new Car("?????????"), "2018/04/18 17:56:39");
        car_parking.EnterParking(new Car("?????????"), "2018/04/18 17:56:39");
        car_parking.EnterParking(new Car("?????????"), "2018/04/18 17:56:39");
        car_parking.EnterParking(new Car("?????????"), "2018/04/18 17:56:39");
        car_parking.EnterParking(new Car("?????????"), "2018/04/18 17:56:39");
        car_parking.EnterParking(new Car("?????????"), "2018/04/18 17:56:39");
        car_parking.EnterParking(new Car("?????????"), "2018/04/18 17:56:39");
        car_parking.EnterParking(new Car("?????????"), "2018/04/18 17:56:39");
        car_parking.EnterParking(new Car("?????????"), "2018/04/18 17:56:39");
        car_parking.ParkingStatus();

        car = car_parking.LeaveParking("?????????", "2018/04/18 16:47:37");
        //std::cout << car->license_plate_number << std::endl;
        delete car;
        car_parking.ParkingStatus();
        
        car = car_parking.LeaveParking("?????????", "2018/04/18 16:47:37");
        //std::cout << car->license_plate_number << std::endl;
        delete car;
        car_parking.ParkingStatus();
        return 0;
}

CarParking.cpp
#include "CarParking.h"

#include <iostream>

CarParking::CarParking(): history(parking_space_count)
{
        total_income = 0;
}

void CarParking::EnterParking(Car *car, std::string enter_time)
{
        waiting_queue.push(InfoNode(car, enter_time));

        if(parking_space.size() < 5)
        {
                parking_space.push_back(waiting_queue.front());
                waiting_queue.pop();
        }
}

static int IteratorToIndex(std::vector<InfoNode> &v, std::vector<InfoNode>::iterator iter)
{
        return iter - v.begin();
}

// 在等待队列的车不能离开停车场^_^,这里也许需要改进,目前就这样了
Car *CarParking::LeaveParking(const std::string &license_plate_number, const std::string leave_time)
{
        for(auto iter = parking_space.begin(); iter != parking_space.end(); ++iter)
        {
                if(iter->car->license_plate_number == license_plate_number)
                {
                        InfoNode in(iter->car, iter->enter_time);
                        HistoryNode hn(*in.car, in.enter_time, leave_time);
                        CollectionOfCharges(hn);
                        history.at(IteratorToIndex(parking_space, iter)).push_back(hn);
                        
                        parking_space.erase(iter);


                        // 如果等待队列有车,现在有空位了
                        // 这里处理进入停车场和离开停车场的车位有点问题,目前就这样了
                        // 也许停车位该用数组?
                        if(waiting_queue.size() != 0)
                        {
                                parking_space.push_back(waiting_queue.front());
                                waiting_queue.pop();
                        }

                        return in.car;
                }
        }

        return NULL;
}

void CarParking::ParkingStatus() const
{
        std::cout << "总收入:" << total_income << std::endl;
        ViewHistory();
}

void CarParking::ViewHistory() const
{
        std::cout << "**************************************************" << std::endl;
        std::cout << "历史记录:" << std::endl;
        for(int i = 0; i < parking_space_count; ++i)
        {
                if(history.at(i).size() == 0)
                        continue;

                std::cout << "第 " << i << "号停车位:" << std::endl;
                for(auto iter = history.at(i).begin(); iter != history.at(i).end(); ++iter)
                {
                        std::cout << std::endl;
                        std::cout << "车牌号:" << iter->car.license_plate_number << std::endl;
                        std::cout << "进入时间:" << iter->enter_time << std::endl;
                        std::cout << "离开时间:" << iter->leave_time << std::endl;
                        std::cout << std::endl;
                }
        }
        std::cout << "**************************************************" << std::endl;
}

void CarParking::CollectionOfCharges(HistoryNode & hn)
{
}

CarParking.h
#ifndef _CAR_PARKING_HPP_
#define _CAR_PARKING_HPP_

#include <queue>
#include <string>
#include <vector>

class Car
{
public:
        const std::string license_plate_number;

        Car(std::string license_plate_number): license_plate_number(license_plate_number){}
};


class InfoNode
{
public:
        Car *car;
        const std::string enter_time;
        InfoNode(Car *car, std::string enter_time): enter_time(enter_time){ this->car = car; }
        InfoNode &operator=(const InfoNode &dest)
        {
                this->car = dest.car;
                return *this;
        }
};

class HistoryNode
{
public:
        const Car car;
        const std::string enter_time;
        const std::string leave_time;
        HistoryNode(const Car car, const std::string enter_time, const std::string leave_time): car(car), enter_time(enter_time), leave_time(leave_time){}
};

class CarParking
{
public:
        const int parking_space_count = 5;        // 设小点,方便调试

        CarParking();

        void EnterParking(Car *car, std::string enter_time);
        Car *LeaveParking(const std::string &license_plate_number, const std::string leave_time);
        void ParkingStatus() const;
        void ViewHistory() const;
private:
        std::vector<std::vector<HistoryNode>> history;
        std::queue<InfoNode> waiting_queue;
        std::vector<InfoNode> parking_space;
        long total_income;

        void CollectionOfCharges(HistoryNode &hn);
};

#endif
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-4-17 22:33:53 | 显示全部楼层

回帖奖励 +5 鱼币

我也不会
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-18 10:57:44 | 显示全部楼层

回帖奖励 +5 鱼币

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-4-18 18:12:02 | 显示全部楼层

回帖奖励 +5 鱼币

360截图17710407302953.png

还剩CollectionOfCharges函数没写,你自己先试着写一写,我再研究研究

main.cpp
#include "CarParking.h"
#include <iostream>

CarParking car_parking;

int main()
{
        car_parking.EnterParking(new Car("晋H·A1363"), "2018/04/18 15:24:31");
        car_parking.EnterParking(new Car("晋H·YQ911"), "2018/04/18 15:43:52");

        Car *car;
        
        car = car_parking.LeaveParking("晋H·YQ911", "2018/04/18 16:46:11");
        //std::cout << car->license_plate_number << std::endl;
        delete car;
        car_parking.ParkingStatus();

        car = car_parking.LeaveParking("晋H·A1363", "2018/04/18 16:47:37");
        //std::cout << car->license_plate_number << std::endl;
        delete car;
        car_parking.ParkingStatus();

        car_parking.EnterParking(new Car("?????????"), "2018/04/18 17:56:39");
        car = car_parking.LeaveParking("?????????", "2018/04/18 17:57:01");
        delete car;
        car_parking.ParkingStatus();

        car_parking.EnterParking(new Car("?????????"), "2018/04/18 17:56:39");
        car = car_parking.LeaveParking("?????????", "2018/04/18 17:57:01");
        delete car;
        car_parking.ParkingStatus();

        car_parking.EnterParking(new Car("?????????"), "2018/04/18 17:56:39");
        car = car_parking.LeaveParking("?????????", "2018/04/18 17:57:01");
        delete car;
        car_parking.ParkingStatus();

        car_parking.EnterParking(new Car("?????????"), "2018/04/18 17:56:39");
        car = car_parking.LeaveParking("?????????", "2018/04/18 17:57:01");
        delete car;
        car_parking.ParkingStatus();

        car_parking.EnterParking(new Car("?????????"), "2018/04/18 17:56:39");
        car = car_parking.LeaveParking("?????????", "2018/04/18 17:57:01");
        delete car;
        car_parking.ParkingStatus();


        car_parking.EnterParking(new Car("?????????"), "2018/04/18 17:56:39");
        car_parking.EnterParking(new Car("?????????"), "2018/04/18 17:56:39");
        car_parking.EnterParking(new Car("?????????"), "2018/04/18 17:56:39");
        car_parking.EnterParking(new Car("?????????"), "2018/04/18 17:56:39");
        car_parking.EnterParking(new Car("?????????"), "2018/04/18 17:56:39");
        car_parking.EnterParking(new Car("?????????"), "2018/04/18 17:56:39");
        car_parking.EnterParking(new Car("?????????"), "2018/04/18 17:56:39");
        car_parking.EnterParking(new Car("?????????"), "2018/04/18 17:56:39");
        car_parking.EnterParking(new Car("?????????"), "2018/04/18 17:56:39");
        car_parking.EnterParking(new Car("?????????"), "2018/04/18 17:56:39");
        car_parking.ParkingStatus();

        return 0;
}

CarParking.cpp
#include "CarParking.h"

#include <iostream>

CarParking::CarParking(): history(parking_space)
{
        total_income = 0;
}

void CarParking::EnterParking(Car *car, std::string enter_time)
{
        waiting_queue.push(InfoNode(car, enter_time));

        if(info.size() < 5)
        {
                info.push_back(waiting_queue.front());
                waiting_queue.pop();
        }
}

static int IteratorToIndex(std::vector<InfoNode> &v, std::vector<InfoNode>::iterator iter)
{
        return iter - v.begin();
}

// 在等待队列的车不能离开停车场^_^,这里也许需要改进,目前就这样了
Car *CarParking::LeaveParking(const std::string &license_plate_number, const std::string leave_time)
{
        for(auto iter = info.begin(); iter != info.end(); ++iter)
        {
                if(iter->car->license_plate_number == license_plate_number)
                {
                        InfoNode in(iter->car, iter->enter_time);
                        HistoryNode hn(*in.car, in.enter_time, leave_time);
                        CollectionOfCharges(hn);
                        history.at(IteratorToIndex(info, iter)).push_back(hn);
                        
                        info.erase(iter);
                        return in.car;
                }
        }

        return NULL;
}

void CarParking::ParkingStatus() const
{
        std::cout << "总收入:" << total_income << std::endl;
        ViewHistory();
}

void CarParking::ViewHistory() const
{
        std::cout << "**************************************************" << std::endl;
        std::cout << "历史记录:" << std::endl;
        for(int i = 0; i < parking_space; ++i)
        {
                if(history.at(i).size() == 0)
                        continue;

                std::cout << "第 " << i << "号停车位:" << std::endl;
                for(auto iter = history.at(i).begin(); iter != history.at(i).end(); ++iter)
                {
                        std::cout << std::endl;
                        std::cout << "车牌号:" << iter->car.license_plate_number << std::endl;
                        std::cout << "进入时间:" << iter->enter_time << std::endl;
                        std::cout << "离开时间:" << iter->leave_time << std::endl;
                        std::cout << std::endl;
                }
        }
        std::cout << "**************************************************" << std::endl;
}

void CarParking::CollectionOfCharges(HistoryNode & hn)
{
}

CarParking.h
#ifndef _CAR_PARKING_HPP_
#define _CAR_PARKING_HPP_

#include <queue>
#include <string>
#include <vector>

class Car
{
public:
        const std::string license_plate_number;

        Car(std::string license_plate_number): license_plate_number(license_plate_number){}
};


class InfoNode
{
public:
        Car *car;
        const std::string enter_time;
        InfoNode(Car *car, std::string enter_time): enter_time(enter_time){ this->car = car; }
        InfoNode &operator=(const InfoNode &dest)
        {
                this->car = dest.car;
                return *this;
        }
};

class HistoryNode
{
public:
        const Car car;
        const std::string enter_time;
        const std::string leave_time;
        HistoryNode(const Car car, const std::string enter_time, const std::string leave_time): car(car), enter_time(enter_time), leave_time(leave_time){}
};

class CarParking
{
public:
        const int parking_space = 5;        // 设小点,方便调试

        CarParking();

        void EnterParking(Car *car, std::string enter_time);
        Car *LeaveParking(const std::string &license_plate_number, const std::string leave_time);
        void ParkingStatus() const;
        void ViewHistory() const;
private:
        std::vector<std::vector<HistoryNode>> history;
        std::queue<InfoNode> waiting_queue;
        std::vector<InfoNode> info;
        long total_income;

        void CollectionOfCharges(HistoryNode &hn);
};

#endif

评分

参与人数 1荣誉 +5 鱼币 +5 贡献 +3 收起 理由
kykio + 5 + 5 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-18 18:14:25 | 显示全部楼层
总收入:0
**************************************************
历史记录:
第 1号停车位:

车牌号:晋H·YQ911
进入时间:2018/04/18 15:43:52
离开时间:2018/04/18 16:46:11

**************************************************
总收入:0
**************************************************
历史记录:
第 0号停车位:

车牌号:晋H·A1363
进入时间:2018/04/18 15:24:31
离开时间:2018/04/18 16:47:37

第 1号停车位:

车牌号:晋H·YQ911
进入时间:2018/04/18 15:43:52
离开时间:2018/04/18 16:46:11

**************************************************
总收入:0
**************************************************
历史记录:
第 0号停车位:

车牌号:晋H·A1363
进入时间:2018/04/18 15:24:31
离开时间:2018/04/18 16:47:37


车牌号:?????????
进入时间:2018/04/18 17:56:39
离开时间:2018/04/18 17:57:01

第 1号停车位:

车牌号:晋H·YQ911
进入时间:2018/04/18 15:43:52
离开时间:2018/04/18 16:46:11

**************************************************
总收入:0
**************************************************
历史记录:
第 0号停车位:

车牌号:晋H·A1363
进入时间:2018/04/18 15:24:31
离开时间:2018/04/18 16:47:37


车牌号:?????????
进入时间:2018/04/18 17:56:39
离开时间:2018/04/18 17:57:01


车牌号:?????????
进入时间:2018/04/18 17:56:39
离开时间:2018/04/18 17:57:01

第 1号停车位:

车牌号:晋H·YQ911
进入时间:2018/04/18 15:43:52
离开时间:2018/04/18 16:46:11

**************************************************
总收入:0
**************************************************
历史记录:
第 0号停车位:

车牌号:晋H·A1363
进入时间:2018/04/18 15:24:31
离开时间:2018/04/18 16:47:37


车牌号:?????????
进入时间:2018/04/18 17:56:39
离开时间:2018/04/18 17:57:01


车牌号:?????????
进入时间:2018/04/18 17:56:39
离开时间:2018/04/18 17:57:01


车牌号:?????????
进入时间:2018/04/18 17:56:39
离开时间:2018/04/18 17:57:01

第 1号停车位:

车牌号:晋H·YQ911
进入时间:2018/04/18 15:43:52
离开时间:2018/04/18 16:46:11

**************************************************
总收入:0
**************************************************
历史记录:
第 0号停车位:

车牌号:晋H·A1363
进入时间:2018/04/18 15:24:31
离开时间:2018/04/18 16:47:37


车牌号:?????????
进入时间:2018/04/18 17:56:39
离开时间:2018/04/18 17:57:01


车牌号:?????????
进入时间:2018/04/18 17:56:39
离开时间:2018/04/18 17:57:01


车牌号:?????????
进入时间:2018/04/18 17:56:39
离开时间:2018/04/18 17:57:01


车牌号:?????????
进入时间:2018/04/18 17:56:39
离开时间:2018/04/18 17:57:01

第 1号停车位:

车牌号:晋H·YQ911
进入时间:2018/04/18 15:43:52
离开时间:2018/04/18 16:46:11

**************************************************
总收入:0
**************************************************
历史记录:
第 0号停车位:

车牌号:晋H·A1363
进入时间:2018/04/18 15:24:31
离开时间:2018/04/18 16:47:37


车牌号:?????????
进入时间:2018/04/18 17:56:39
离开时间:2018/04/18 17:57:01


车牌号:?????????
进入时间:2018/04/18 17:56:39
离开时间:2018/04/18 17:57:01


车牌号:?????????
进入时间:2018/04/18 17:56:39
离开时间:2018/04/18 17:57:01


车牌号:?????????
进入时间:2018/04/18 17:56:39
离开时间:2018/04/18 17:57:01


车牌号:?????????
进入时间:2018/04/18 17:56:39
离开时间:2018/04/18 17:57:01

第 1号停车位:

车牌号:晋H·YQ911
进入时间:2018/04/18 15:43:52
离开时间:2018/04/18 16:46:11

**************************************************
总收入:0
**************************************************
历史记录:
第 0号停车位:

车牌号:晋H·A1363
进入时间:2018/04/18 15:24:31
离开时间:2018/04/18 16:47:37


车牌号:?????????
进入时间:2018/04/18 17:56:39
离开时间:2018/04/18 17:57:01


车牌号:?????????
进入时间:2018/04/18 17:56:39
离开时间:2018/04/18 17:57:01


车牌号:?????????
进入时间:2018/04/18 17:56:39
离开时间:2018/04/18 17:57:01


车牌号:?????????
进入时间:2018/04/18 17:56:39
离开时间:2018/04/18 17:57:01


车牌号:?????????
进入时间:2018/04/18 17:56:39
离开时间:2018/04/18 17:57:01

第 1号停车位:

车牌号:晋H·YQ911
进入时间:2018/04/18 15:43:52
离开时间:2018/04/18 16:46:11

**************************************************
请按任意键继续. . .

评分

参与人数 1荣誉 +5 鱼币 +5 贡献 +2 收起 理由
kykio + 5 + 5 + 2

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-18 18:27:21 | 显示全部楼层    本楼为最佳答案   
修复bug

main.cpp
#include "CarParking.h"
#include <iostream>

CarParking car_parking;

int main()
{
        car_parking.EnterParking(new Car("晋H·A1363"), "2018/04/18 15:24:31");
        car_parking.EnterParking(new Car("晋H·YQ911"), "2018/04/18 15:43:52");

        Car *car;
        
        car = car_parking.LeaveParking("晋H·YQ911", "2018/04/18 16:46:11");
        //std::cout << car->license_plate_number << std::endl;
        delete car;
        car_parking.ParkingStatus();

        car = car_parking.LeaveParking("晋H·A1363", "2018/04/18 16:47:37");
        //std::cout << car->license_plate_number << std::endl;
        delete car;
        car_parking.ParkingStatus();

        car_parking.EnterParking(new Car("?????????"), "2018/04/18 17:56:39");
        car = car_parking.LeaveParking("?????????", "2018/04/18 17:57:01");
        delete car;
        car_parking.ParkingStatus();

        car_parking.EnterParking(new Car("?????????"), "2018/04/18 17:56:39");
        car = car_parking.LeaveParking("?????????", "2018/04/18 17:57:01");
        delete car;
        car_parking.ParkingStatus();

        car_parking.EnterParking(new Car("?????????"), "2018/04/18 17:56:39");
        car = car_parking.LeaveParking("?????????", "2018/04/18 17:57:01");
        delete car;
        car_parking.ParkingStatus();

        car_parking.EnterParking(new Car("?????????"), "2018/04/18 17:56:39");
        car = car_parking.LeaveParking("?????????", "2018/04/18 17:57:01");
        delete car;
        car_parking.ParkingStatus();

        car_parking.EnterParking(new Car("?????????"), "2018/04/18 17:56:39");
        car = car_parking.LeaveParking("?????????", "2018/04/18 17:57:01");
        delete car;
        car_parking.ParkingStatus();


        car_parking.EnterParking(new Car("?????????"), "2018/04/18 17:56:39");
        car_parking.EnterParking(new Car("?????????"), "2018/04/18 17:56:39");
        car_parking.EnterParking(new Car("?????????"), "2018/04/18 17:56:39");
        car_parking.EnterParking(new Car("?????????"), "2018/04/18 17:56:39");
        car_parking.EnterParking(new Car("?????????"), "2018/04/18 17:56:39");
        car_parking.EnterParking(new Car("?????????"), "2018/04/18 17:56:39");
        car_parking.EnterParking(new Car("?????????"), "2018/04/18 17:56:39");
        car_parking.EnterParking(new Car("?????????"), "2018/04/18 17:56:39");
        car_parking.EnterParking(new Car("?????????"), "2018/04/18 17:56:39");
        car_parking.EnterParking(new Car("?????????"), "2018/04/18 17:56:39");
        car_parking.ParkingStatus();

        car = car_parking.LeaveParking("?????????", "2018/04/18 16:47:37");
        //std::cout << car->license_plate_number << std::endl;
        delete car;
        car_parking.ParkingStatus();
        
        car = car_parking.LeaveParking("?????????", "2018/04/18 16:47:37");
        //std::cout << car->license_plate_number << std::endl;
        delete car;
        car_parking.ParkingStatus();
        return 0;
}

CarParking.cpp
#include "CarParking.h"

#include <iostream>

CarParking::CarParking(): history(parking_space_count)
{
        total_income = 0;
}

void CarParking::EnterParking(Car *car, std::string enter_time)
{
        waiting_queue.push(InfoNode(car, enter_time));

        if(parking_space.size() < 5)
        {
                parking_space.push_back(waiting_queue.front());
                waiting_queue.pop();
        }
}

static int IteratorToIndex(std::vector<InfoNode> &v, std::vector<InfoNode>::iterator iter)
{
        return iter - v.begin();
}

// 在等待队列的车不能离开停车场^_^,这里也许需要改进,目前就这样了
Car *CarParking::LeaveParking(const std::string &license_plate_number, const std::string leave_time)
{
        for(auto iter = parking_space.begin(); iter != parking_space.end(); ++iter)
        {
                if(iter->car->license_plate_number == license_plate_number)
                {
                        InfoNode in(iter->car, iter->enter_time);
                        HistoryNode hn(*in.car, in.enter_time, leave_time);
                        CollectionOfCharges(hn);
                        history.at(IteratorToIndex(parking_space, iter)).push_back(hn);
                        
                        parking_space.erase(iter);


                        // 如果等待队列有车,现在有空位了
                        // 这里处理进入停车场和离开停车场的车位有点问题,目前就这样了
                        // 也许停车位该用数组?
                        if(waiting_queue.size() != 0)
                        {
                                parking_space.push_back(waiting_queue.front());
                                waiting_queue.pop();
                        }

                        return in.car;
                }
        }

        return NULL;
}

void CarParking::ParkingStatus() const
{
        std::cout << "总收入:" << total_income << std::endl;
        ViewHistory();
}

void CarParking::ViewHistory() const
{
        std::cout << "**************************************************" << std::endl;
        std::cout << "历史记录:" << std::endl;
        for(int i = 0; i < parking_space_count; ++i)
        {
                if(history.at(i).size() == 0)
                        continue;

                std::cout << "第 " << i << "号停车位:" << std::endl;
                for(auto iter = history.at(i).begin(); iter != history.at(i).end(); ++iter)
                {
                        std::cout << std::endl;
                        std::cout << "车牌号:" << iter->car.license_plate_number << std::endl;
                        std::cout << "进入时间:" << iter->enter_time << std::endl;
                        std::cout << "离开时间:" << iter->leave_time << std::endl;
                        std::cout << std::endl;
                }
        }
        std::cout << "**************************************************" << std::endl;
}

void CarParking::CollectionOfCharges(HistoryNode & hn)
{
}

CarParking.h
#ifndef _CAR_PARKING_HPP_
#define _CAR_PARKING_HPP_

#include <queue>
#include <string>
#include <vector>

class Car
{
public:
        const std::string license_plate_number;

        Car(std::string license_plate_number): license_plate_number(license_plate_number){}
};


class InfoNode
{
public:
        Car *car;
        const std::string enter_time;
        InfoNode(Car *car, std::string enter_time): enter_time(enter_time){ this->car = car; }
        InfoNode &operator=(const InfoNode &dest)
        {
                this->car = dest.car;
                return *this;
        }
};

class HistoryNode
{
public:
        const Car car;
        const std::string enter_time;
        const std::string leave_time;
        HistoryNode(const Car car, const std::string enter_time, const std::string leave_time): car(car), enter_time(enter_time), leave_time(leave_time){}
};

class CarParking
{
public:
        const int parking_space_count = 5;        // 设小点,方便调试

        CarParking();

        void EnterParking(Car *car, std::string enter_time);
        Car *LeaveParking(const std::string &license_plate_number, const std::string leave_time);
        void ParkingStatus() const;
        void ViewHistory() const;
private:
        std::vector<std::vector<HistoryNode>> history;
        std::queue<InfoNode> waiting_queue;
        std::vector<InfoNode> parking_space;
        long total_income;

        void CollectionOfCharges(HistoryNode &hn);
};

#endif

评分

参与人数 1荣誉 +5 鱼币 +5 贡献 +3 收起 理由
kykio + 5 + 5 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-18 21:51:10 | 显示全部楼层

回帖奖励 +5 鱼币

对不起
还有楼上的同学真厉害,我也要学习一下
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-18 22:48:30 | 显示全部楼层

回帖奖励 +5 鱼币

太难了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-11-23 22:44

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表