鱼C论坛

 找回密码
 立即注册
查看: 279|回复: 10

加急求助,求大神指导

[复制链接]
最佳答案
0 
发表于 2020-7-1 08:23:36 | 显示全部楼层 |阅读模式
50鱼币

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

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

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
最佳答案
12 
发表于 2020-7-1 08:39:54 | 显示全部楼层
估计没人愿意花时间单独写,除非有现成的例子
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
最佳答案
0 
发表于 2020-7-29 10:08:08 | 显示全部楼层
哪有张口就要整个管理系统的啊,没几百行解决不了的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
最佳答案
38 
发表于 2020-7-29 12:07:11 | 显示全部楼层
  1. A sample solution:

  2. Application controller : First object that will receive calls on application events.
  3. public enum ParkingLotController { //singleton facade controller
  4.         INSTANCE;
  5.         public ParkingLotController() {
  6.                 VehicleSensorPool.INSTANCE.register(VehicleEventListenerPool.INSTANCE);
  7.         }
  8.        
  9.         public ParkingTicket enter(VehicleEntryEvent vehicleEntryEvent) throws SpotNotAvailableException, IllegitimateVehicleException { // app event
  10.                 ParkingSpot parkingSpot = null;
  11.                 Vehicle enteringVehicle = vehicleEntryEvent.getVehicle();
  12.                 try {
  13.                         parkingSpot = ParkingLot.addVehicle(enteringVehicle);
  14.                         return printTicket(enteringVehicle);
  15.                 }
  16.                 catch(SpotNotAvailableException e) {
  17.                         displayWaitMessage(e);
  18.                         //very primitive retry mechanism..substitute your own here
  19.                         //or we can use wait-notify
  20.                         Thread.sleep(WAIT_DURATION);
  21.                         enter(vehicleEntryEvent);
  22.                 }
  23.                 catch(IllegitimateVehicleException e) {
  24.                         displayIntoleranceMessage(e);
  25.                 }
  26.         }
  27.        
  28.         public ParkingBill exit(VehicleExitEvent vehicleExitEvent) { // app event
  29.                 Vehicle exitingVehicle = ParkingLot.getVehicle(enteringVehicle);
  30.                 ParkingLot.removeVehicle(exitingVehicle);
  31.                 long timeVehicleKept = ParkingLot.getTimeKept(exitingVehicle);
  32.                 return printBill(exitingVehicle, timeVehicleKept);
  33.         }
  34.        
  35.         private ParkingTicket printTicket(Vehicle vehicle) {
  36.                 //..
  37.         }
  38.        
  39.         private ParkingBill printBill(Vehicle vehicle, long timeVehicleKept) {
  40.                 //BillingSystem is again a facade
  41.                 return BillingSystem.INSTANCE.printBill(vehicle, timeVehicleKept);
  42.         }
  43. }
  44. 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.
  45. 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.
  46. Further note: SensorData is inner for enhanced encapsulation.
  47. public enum VehicleSensorPool {
  48.         INSTANCE;
  49.         private List<VehicleSensor> vehicleSensors;
  50.         public VehicleSensorPool() {
  51.                 //init vehicleSensors, according to config
  52.                 for(VehicleSensor vehicleSensor : vehicleSensors) {
  53.                         new Thread(vehicleSensor).start();
  54.                 }
  55.         }
  56.         public void register(VehicleEventListener vehicleEventListener) {
  57.                 for(VehicleSensor vehicleSensor : vehicleSensors) {
  58.                         vehicleSensor.addEventListener(vehicleEventListener);
  59.                 }
  60.         }
  61. }

  62. public interface VehicleEventListener {
  63.         public void onVehicleEnter(Vehicle vehicle);
  64.         public void onVehicleExit(Vehicle vehicle);
  65. }

  66. public class VehicleSensor implements Runnable {
  67.         private class SensorData {//contains raw sensor data that the sensor must use to raise events}
  68.         public void run() {
  69.                 while(true) {
  70.                         //sense Vehicle entry and exit and notify event listeners
  71.                         SensorData sensorData = sense(); //blocking call
  72.                        
  73.                         //on entry create a Vehicle object (populated with height, type, plate etc) and notify.
  74.                         //on exit retrieve vehicle object from ParkingLot object and raise event.
  75.                 }
  76.         }
  77.         private Vehicle createVehicleEvent(SensorData sensorData) {
  78.                 if( //sensorData points to entry) {
  79.                         Vehicle vehicle = createVehicle(sensorData);
  80.                         return new VehicleEntryEvent(vehicle);
  81.                 }
  82.                 else { //sensorData points to exit
  83.                         return new VehicleExitEvent(sensorData.getPlate());
  84.                 }
  85.         }
  86.         private Vehicle createVehicle(SensorData sensorData) {
  87.                 //series of vehicle.setXXX(sensorData.getXXX())
  88.         }
  89. }

  90. public enum VehicleEventListenerPool implements VehicleEventListener { //composite singleton
  91.         INSTANCE;
  92.         private List<VehicleEventListener> vehicleEventListeners;
  93.         public VehicleEventListenerPool() {
  94.                 //init vehicleEventListeners, according to config
  95.         }
  96.         public void onVehicleEnter(VehicleEntryEvent vehicleEntryEvent) {
  97.                 //select a listener from the pool and call its onVehicleEnter method
  98.         }
  99.         public void onVehicleExit(VehicleExitEvent vehicleExitEvent) {
  100.                 //select a listener from the pool and call its onVehicleExit method
  101.         }
  102. }

  103. public class VehicleEventListenerImpl implements VehicleEventListener {
  104.         public void onVehicleEnter(VehicleEntryEvent vehicleEntryEvent) {
  105.                 ParkingLotController.INSTANCE.enter(vehicleEntryEvent);
  106.         }
  107.         public void onVehicleExit(VehicleExitEvent vehicleExitEvent) {
  108.                 ParkingLotController.INSTANCE.exit(vehicleExitEvent);
  109.         }
  110. }
  111. ParkingLot : Facade for the parking lot subsystem. Manages parkingSpot assignments. Validates vehicles to ensure that they follow parking policy (for eg max height).
  112. 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.)
  113. 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.
  114. Additionally, it would be easy to add timekeeping as shown. This would aid billing.
  115. public class Vehicle {
  116.         private String plate;
  117.         private double height;
  118.         private VehicleType type;
  119. }

  120. public enum VehicleType {
  121.         BIKE(100.0), BICYCLE(90.0), TRUCK(1000.0), CAR(400.0); //sample props of VehicleType
  122.         private int maxHeight;
  123.         public VehicleType(int maxHeight) {
  124.                 this.maxHeight = maxHeight;
  125.         }
  126.         //..
  127. }

  128. public class ParkingSpot {
  129.         private int level;
  130.         private int height;
  131.         private int spotNo;
  132.         private boolean vacant;
  133.         private Set<VehicleType> suitableFor;
  134.         //equals() and hashCode()..
  135.         //..
  136. }

  137. public enum ParkingLot {
  138.         INSTANCE,
  139.         //..internal DSes to maintain parking spots, vehicles and assignments.
  140.         //strategies for validation and assignment
  141.         private ParkingSpotAssignmentStrategy parkingSpotAssignmentStrategy;
  142.         private VehicleValidationStrategy vehicleValidationStrategy;
  143.         public ParkingLot() {
  144.                 //..init internal DSes to maintain parking spots, vehicles and assignments.
  145.                 //init strategies
  146.                 parkingSpotAssignmentStrategy = new ParkingSpotAssignmentStrategyImpl(this);
  147.                 vehicleValidationStrategy = new VehicleValidationStrategyImpl(this);
  148.         }
  149.         public ParkingSpot addVehicle(Vehicle vehicle) throws SpotNotAvailableException, IllegitimateVehicleException {
  150.                 //check if vehicle is elligible for parking(we can have a Validator object) and assign ParkingSpot according to strategy
  151.                 vehicleValidationStrategy.validate();
  152.                 //assignment happens only after validation
  153.                 return assign(vehicle);
  154.         }
  155.         private ParkingSpot assign(Vehicle vehicle) throws SpotNotAvailableException {
  156.                 ParkingSpot vacantParkingSpot = parkingSpotAssignmentStrategy.assign(vehicle);
  157.                 synchronized(vacantParkingSpot) { //so that no two vehicles are assigned the same spot
  158.                         if(vacantParkingSpot.isVacant()) {
  159.                                 //..associate vehicle with vacantParkingSpot. Update internal DSes.       
  160.                                 //start timekeeping using a TimeKeeper object
  161.                         }
  162.                         else {
  163.                                 //retry
  164.                                 return assign(vehicle);
  165.                         }
  166.                 }
  167.                 return vacantParkingSpot;
  168.         }
  169.         public Vehicle getVehicle(String plate) {
  170.                 //..get vehicle by plate
  171.         }
  172.         public void removeVehicle(Vehicle vehicle) {
  173.                 //..remove vehicle from lot. reclaim parkingSpot. Update internal DSes.
  174.                 //stop timekeeping.
  175.         }
  176.         public void getTimeKept(Vehicle vehicle) {
  177.                 //return time kept by TimeKeeper
  178.         }
  179. }

  180. public interface ParkingSpotAssignmentStrategy {
  181.         public ParkingSpot assign(Vehicle vehicle) throws SpotNotAvailableException;
  182. }

  183. public interface VehicleValidationStrategy {
  184.         public void validate(Vehicle vehicle) throws IllegitimateVehicleException;
  185. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
最佳答案
64 
发表于 2020-7-29 16:28:57 | 显示全部楼层

怎么感觉有些像java呀(是不是发错了,这里是c++专区呀)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
最佳答案
38 
发表于 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++
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
最佳答案
12 
发表于 2020-7-30 11:04:59 | 显示全部楼层
昨非 发表于 2020-7-29 10:08
哪有张口就要整个管理系统的啊,没几百行解决不了的

应该是不会有人写的!50鱼币太
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
最佳答案
30 
发表于 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++

你是外国人??看得懂中国话但是不会说?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
最佳答案
30 
发表于 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]中第一个数据为该车牌号,
     调用系统时间函数填充第二个数据,j++。若等于N,判断便道,即i是否等于M,若等于,则让车
     离开,没地方了。若不等于,则车牌记于A[i]中,i++。
7:创建函数,若有车离开,遍历B数组,找到符合车牌号的,调用时间,减去结构体中刚进来时填进的时
     间,便是停车时间。并列出它的序号。从该序号开始,每个都是后面车的信息。并将便道第一个车弄进
     去,即A[0]车牌。A也是每个都填后面一辆的信息。i--。若便道无车,则j--。
8:当然,也可以用结构体指针,这样就不用一个个填后面的的信息了,只需指针指向另一个位置即可。太多
     了,就不再说了。
     面一辆车的信息
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
最佳答案
38 
发表于 2020-8-1 22:48:51 | 显示全部楼层
405794672 发表于 2020-8-1 19:58
你是外国人??看得懂中国话但是不会说?


No, I did not install Chinese input for my OS
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
最佳答案
415 
发表于 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……
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

小甲鱼强烈推荐上一条 /1 下一条

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

GMT+8, 2020-8-12 11:39

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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