HJX54088 发表于 2020-7-1 08:23:36

加急求助,求大神指导


停车场管理方案的数据结构设计
【问题描述】
停车场是一条可以停放n辆车的狭窄车道,并且只有一个大门是车的出入口。汽车停放按照到达时间的先后顺序依次由东向西排列(大门在最西端,最先到达的第一辆车停在最东端),若停车场已经停满了n辆车,后来的汽车在便道上等候,一旦有车开走,排在便道上的第一辆车可以开入;当停车场的某辆车要离开时,停在他后面的车要先后退为他让路,等它开出后其他车再按照原先次序开入车场,每辆停在停车场的车要按时间长短缴费!
【基本要求】
请用C++语言编写程序实现该停车场的管理过程。
(1)根据车辆到达停车场到车辆离开停车场时所停留的时间进行计时收费。
(2)根据车牌号查到该车辆在停车场或者便道中的位置。
(3)当有车辆从停车场离开时,等待的车辆按顺序进入停车场停放。
【测试数据】
测试数据:要求使用全部合法数据,整体非法数据,局部非法数据进行程序测试,以保证程序的稳定性。测试数据及其测试结果请在上交的资料文档中写明!
【实现提示】
用栈模拟停车场(后进先出),用队列模拟车场外的便道。按照从终端输入的数据进行模拟管理。数据结构应该包括三个数据项:汽车车牌号码,汽车“到达”或者“离开”信息,汽车到达或者离开的时刻。

用C++编写代码顺带注释,谢谢 !

Seawolf 发表于 2020-7-1 08:23:37

A sample solution:

Application controller : First object that will receive calls on application events.
public enum ParkingLotController { //singleton facade controller
        INSTANCE;
        public ParkingLotController() {
                VehicleSensorPool.INSTANCE.register(VehicleEventListenerPool.INSTANCE);
        }
       
        public ParkingTicket enter(VehicleEntryEvent vehicleEntryEvent) throws SpotNotAvailableException, IllegitimateVehicleException { // app event
                ParkingSpot parkingSpot = null;
                Vehicle enteringVehicle = vehicleEntryEvent.getVehicle();
                try {
                        parkingSpot = ParkingLot.addVehicle(enteringVehicle);
                        return printTicket(enteringVehicle);
                }
                catch(SpotNotAvailableException e) {
                        displayWaitMessage(e);
                        //very primitive retry mechanism..substitute your own here
                        //or we can use wait-notify
                        Thread.sleep(WAIT_DURATION);
                        enter(vehicleEntryEvent);
                }
                catch(IllegitimateVehicleException e) {
                        displayIntoleranceMessage(e);
                }
        }
       
        public ParkingBill exit(VehicleExitEvent vehicleExitEvent) { // app event
                Vehicle exitingVehicle = ParkingLot.getVehicle(enteringVehicle);
                ParkingLot.removeVehicle(exitingVehicle);
                long timeVehicleKept = ParkingLot.getTimeKept(exitingVehicle);
                return printBill(exitingVehicle, timeVehicleKept);
        }
       
        private ParkingTicket printTicket(Vehicle vehicle) {
                //..
        }
       
        private ParkingBill printBill(Vehicle vehicle, long timeVehicleKept) {
                //BillingSystem is again a facade
                return BillingSystem.INSTANCE.printBill(vehicle, timeVehicleKept);
        }
}
Event listener infrastructure : The next question is who will forward events to the controller. We have vehicle sensors at multiple points of entry and exit. These sensors are able to scan a vehicle's props such as plate, height, type etc. and notify entry and exit event listeners about entry and exit. Note that this enables a real time feel and reduces waiting for vehicles (as spots can be made async available) if slots are full.
Sensors run on their own threads. We have the flexibility of having sensors:listeners in a m:n relationship through the use of the composite listener pool.
Further note: SensorData is inner for enhanced encapsulation.
public enum VehicleSensorPool {
        INSTANCE;
        private List<VehicleSensor> vehicleSensors;
        public VehicleSensorPool() {
                //init vehicleSensors, according to config
                for(VehicleSensor vehicleSensor : vehicleSensors) {
                        new Thread(vehicleSensor).start();
                }
        }
        public void register(VehicleEventListener vehicleEventListener) {
                for(VehicleSensor vehicleSensor : vehicleSensors) {
                        vehicleSensor.addEventListener(vehicleEventListener);
                }
        }
}

public interface VehicleEventListener {
        public void onVehicleEnter(Vehicle vehicle);
        public void onVehicleExit(Vehicle vehicle);
}

public class VehicleSensor implements Runnable {
        private class SensorData {//contains raw sensor data that the sensor must use to raise events}
        public void run() {
                while(true) {
                        //sense Vehicle entry and exit and notify event listeners
                        SensorData sensorData = sense(); //blocking call
                       
                        //on entry create a Vehicle object (populated with height, type, plate etc) and notify.
                        //on exit retrieve vehicle object from ParkingLot object and raise event.
                }
        }
        private Vehicle createVehicleEvent(SensorData sensorData) {
                if( //sensorData points to entry) {
                        Vehicle vehicle = createVehicle(sensorData);
                        return new VehicleEntryEvent(vehicle);
                }
                else { //sensorData points to exit
                        return new VehicleExitEvent(sensorData.getPlate());
                }
        }
        private Vehicle createVehicle(SensorData sensorData) {
                //series of vehicle.setXXX(sensorData.getXXX())
        }
}

public enum VehicleEventListenerPool implements VehicleEventListener { //composite singleton
        INSTANCE;
        private List<VehicleEventListener> vehicleEventListeners;
        public VehicleEventListenerPool() {
                //init vehicleEventListeners, according to config
        }
        public void onVehicleEnter(VehicleEntryEvent vehicleEntryEvent) {
                //select a listener from the pool and call its onVehicleEnter method
        }
        public void onVehicleExit(VehicleExitEvent vehicleExitEvent) {
                //select a listener from the pool and call its onVehicleExit method
        }
}

public class VehicleEventListenerImpl implements VehicleEventListener {
        public void onVehicleEnter(VehicleEntryEvent vehicleEntryEvent) {
                ParkingLotController.INSTANCE.enter(vehicleEntryEvent);
        }
        public void onVehicleExit(VehicleExitEvent vehicleExitEvent) {
                ParkingLotController.INSTANCE.exit(vehicleExitEvent);
        }
}
ParkingLot : Facade for the parking lot subsystem. Manages parkingSpot assignments. Validates vehicles to ensure that they follow parking policy (for eg max height).
A parking spot encapsulates level, spot no and vehicle types it is suitable for. This enables a vehicle to enter through a different level(floor) and park at a completely different floor (in case there was no space available on the entry floor.)
Validators are strategy objects to ensure flexibility around different parking policies that might arise. Spot assignment is again based on strategy objects which decide which spot goes to which vehicle based on parameters.
Additionally, it would be easy to add timekeeping as shown. This would aid billing.
public class Vehicle {
        private String plate;
        private double height;
        private VehicleType type;
}

public enum VehicleType {
        BIKE(100.0), BICYCLE(90.0), TRUCK(1000.0), CAR(400.0); //sample props of VehicleType
        private int maxHeight;
        public VehicleType(int maxHeight) {
                this.maxHeight = maxHeight;
        }
        //..
}

public class ParkingSpot {
        private int level;
        private int height;
        private int spotNo;
        private boolean vacant;
        private Set<VehicleType> suitableFor;
        //equals() and hashCode()..
        //..
}

public enum ParkingLot {
        INSTANCE,
        //..internal DSes to maintain parking spots, vehicles and assignments.
        //strategies for validation and assignment
        private ParkingSpotAssignmentStrategy parkingSpotAssignmentStrategy;
        private VehicleValidationStrategy vehicleValidationStrategy;
        public ParkingLot() {
                //..init internal DSes to maintain parking spots, vehicles and assignments.
                //init strategies
                parkingSpotAssignmentStrategy = new ParkingSpotAssignmentStrategyImpl(this);
                vehicleValidationStrategy = new VehicleValidationStrategyImpl(this);
        }
        public ParkingSpot addVehicle(Vehicle vehicle) throws SpotNotAvailableException, IllegitimateVehicleException {
                //check if vehicle is elligible for parking(we can have a Validator object) and assign ParkingSpot according to strategy
                vehicleValidationStrategy.validate();
                //assignment happens only after validation
                return assign(vehicle);
        }
        private ParkingSpot assign(Vehicle vehicle) throws SpotNotAvailableException {
                ParkingSpot vacantParkingSpot = parkingSpotAssignmentStrategy.assign(vehicle);
                synchronized(vacantParkingSpot) { //so that no two vehicles are assigned the same spot
                        if(vacantParkingSpot.isVacant()) {
                                //..associate vehicle with vacantParkingSpot. Update internal DSes.       
                                //start timekeeping using a TimeKeeper object
                        }
                        else {
                                //retry
                                return assign(vehicle);
                        }
                }
                return vacantParkingSpot;
        }
        public Vehicle getVehicle(String plate) {
                //..get vehicle by plate
        }
        public void removeVehicle(Vehicle vehicle) {
                //..remove vehicle from lot. reclaim parkingSpot. Update internal DSes.
                //stop timekeeping.
        }
        public void getTimeKept(Vehicle vehicle) {
                //return time kept by TimeKeeper
        }
}

public interface ParkingSpotAssignmentStrategy {
        public ParkingSpot assign(Vehicle vehicle) throws SpotNotAvailableException;
}

public interface VehicleValidationStrategy {
        public void validate(Vehicle vehicle) throws IllegitimateVehicleException;
}

jhanker 发表于 2020-7-1 08:39:54

估计没人愿意花时间单独写,除非有现成的例子

昨非 发表于 2020-7-29 10:08:08

哪有张口就要整个管理系统的啊,没几百行解决不了的

xiaosi4081 发表于 2020-7-29 16:28:57

Seawolf 发表于 2020-7-29 12:07


怎么感觉有些像java呀(是不是发错了,这里是c++专区呀)

Seawolf 发表于 2020-7-29 22:00:01

xiaosi4081 发表于 2020-7-29 16:28
怎么感觉有些像java呀(是不是发错了,这里是c++专区呀)

Yeah, it's just a sample code and you need to change it to c++

Cool_Breeze 发表于 2020-7-30 11:04:59

昨非 发表于 2020-7-29 10:08
哪有张口就要整个管理系统的啊,没几百行解决不了的

应该是不会有人写的!50鱼币太{:10_266:}

405794672 发表于 2020-8-1 19:58:08

Seawolf 发表于 2020-7-29 22:00
Yeah, it's just a sample code and you need to change it to c++

你是外国人??看得懂中国话但是不会说?

405794672 发表于 2020-8-1 20:22:32

给你个思路。声明一下。我不知道怎么调用系统当前时间,也不知道怎么进行测试
1:创建一个结构体H,第一个数据为车牌号,第二个数据为时间,第三个数据为计数器。
2:创建一个数组A,全部用来存放车牌号。这是便道,但道上能停车数M。
3:创建一个数组B,用来存放结构体。N个结构体
4:创建变量i,记车牌号数组。初始化为0。创建变量K,记车牌号个数
5:创建变量j,记结构体数组,初始化为0。创建变量L,记结构体数量
6:创建函数,判断是否有车来。若有,则判断J是否等于N,若不是,则填充B中第一个数据为该车牌号,
   调用系统时间函数填充第二个数据,j++。若等于N,判断便道,即i是否等于M,若等于,则让车
   离开,没地方了。若不等于,则车牌记于A中,i++。
7:创建函数,若有车离开,遍历B数组,找到符合车牌号的,调用时间,减去结构体中刚进来时填进的时
   间,便是停车时间。并列出它的序号。从该序号开始,每个都是后面车的信息。并将便道第一个车弄进
   去,即A车牌。A也是每个都填后面一辆的信息。i--。若便道无车,则j--。
8:当然,也可以用结构体指针,这样就不用一个个填后面的的信息了,只需指针指向另一个位置即可。太多
   了,就不再说了。
   面一辆车的信息

Seawolf 发表于 2020-8-1 22:48:51

405794672 发表于 2020-8-1 19:58
你是外国人??看得懂中国话但是不会说?

No, I did not install Chinese input for my OS

永恒的蓝色梦想 发表于 2020-8-2 10:32:48

Seawolf 发表于 2020-8-1 22:48
No, I did not install Chinese input for my OS

I think you should speak Chinese Pinyin……

cyndiwants 发表于 2020-8-18 11:13:46

感觉这不就是一个栈结构吗

蠢蠢熊 发表于 2020-8-22 18:32:28

哈哈哈哈

昨非 发表于 2020-8-22 23:06:32

永恒的蓝色梦想 发表于 2020-8-2 10:32
I think you should speak Chinese Pinyin……

绝了,噗嗤哈哈哈哈嗝哈哈哈哈

风过无痕1989 发表于 2020-8-23 00:12:43

这辆车好像停了很久了,咋还没停好呢?

Mondayisgood 发表于 2020-8-23 11:19:18

感觉也不难啊
为何这么久都没结束
这是作业题吧
是不是还得加上你的学习进度
学到哪了
应该用什么
不应该用什么
输入格式是不是应该规定一下
这么久了
自己硬写,也能写出来了吧
页: [1]
查看完整版本: 加急求助,求大神指导