kykio 发表于 2018-4-16 18:15:25

求鱼油帮忙

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

段坤11 发表于 2018-4-17 22:33:53

我也不会

单甲凯 发表于 2018-4-18 10:57:44

{:10_266:}

人造人 发表于 2018-4-18 18:12:02



还剩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

人造人 发表于 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

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

人造人 发表于 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

溯影 发表于 2018-4-18 21:51:10

对不起{:10_277:}
还有楼上的同学真厉害,我也要学习一下{:10_256:}

DYC 发表于 2018-4-18 22:48:30

太难了
页: [1]
查看完整版本: 求鱼油帮忙