[问题描述]:设有一个可以停放n辆汽车的狭长停车场,它只有一个大门可以供车辆进出。车辆按到达停车场时间的早晚依次从停车场最里面向大门口处停放(最先到达的第一辆车放在停车场的最里面)。如果停车场已放满n辆车,则后来的车辆只能在停车场大门外的便道上等待,一旦停车场内有车开走,则排在便道上的第一辆车就进入停车场。停车场内如有某辆车要开走,在它之后进入停车场的车都必须先退出停车场为它让路,待其开出停车场后,这些车辆再依原来的次序进场。每辆车在离开停车场时,都应根据它在停车场内停留的时间长短交费。如果停留在便道上的车未进停车场就要离去,允许其离去,不收停车费,并且仍然保持在便道上等待的车辆的次序。编制一程序模拟该停车场的管理。
[实现要求]:要求程序输出每辆车到达后的停车位置(停车场或便道上),以及某辆车离开停车场时应缴纳的费用和它在停车场内停留的时间。
[实现提示]:汽车的模拟输入格式可以是:(到达/离去,汽车牌照号,到达/离去的时刻)。例如,(‘A’,1,5)表示1号牌照车在5这个时刻到达,而(‘D’,5,20)表示5号牌照车在20这个时刻离去。整个程序可以在输入信息为(‘E’,0,0)时结束。本题可以用栈和队列来实现。
——————————————car类————————————————
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace parking_solve { class car { public void Car(String id, DateTime InTime, DateTime OutTime, int carSize) { this.id =id ; this.InTime = InTime; this.OutTime = OutTime; this.carSize = carSize; } private String id; private DateTime InTime; private DateTime OutTime; private int carSize; } }
——————————————Parking类实现————————————————
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; namespace parking_solve { class Program { static void Main(string[] args) { ParkingModel pm = new ParkingModel(); pm.pricePerSecond = 2.0; String cmd; Console.Write("请输入指令:"); while ((cmd = Console.ReadLine()) != "end") { if ("gogoin" == cmd) { Console.Write("请输入车牌号:"); String lpm = Console.ReadLine(); Console.WriteLine("[操作]---->添加车辆:" + lpm); pm.gogoin(lpm); } else if (cmd.StartsWith("join:")) { String lpm = cmd.Substring(5).Trim(); Console.WriteLine("[操作]---->添加车辆:" + lpm); pm.gogoin(lpm); } else if ("out" == cmd || "pg" == cmd) { Console.Write("请输入车牌号:"); String lpm = Console.ReadLine(); Console.WriteLine("[操作]---->移除停车场中:" + lpm); pm.parkingGo(lpm); } else if (cmd.StartsWith("pg:")) { String lpm = cmd.Substring(3).Trim(); Console.WriteLine("[操作]---->移除停车场中:" + lpm); pm.parkingGo(lpm); } else if ("gun" == cmd || "qg" == cmd) { Console.Write("请输入车牌号:"); String lpm = Console.ReadLine(); Console.WriteLine("[操作]---->移除候车队列中:" + lpm); pm.queueGo(lpm); } else if (cmd.StartsWith("qg:")) { String lpm = cmd.Substring(3).Trim(); Console.WriteLine("[操作]---->移除候车队列中:" + lpm); pm.queueGo(lpm); } else if ("process" == cmd || "op" == cmd) { Console.WriteLine("[操作]---->【打开】显示车辆进出过程"); pm.isShowProcess = true; } else if ("close" == cmd || "cp" == cmd) { Console.WriteLine("[操作]---->【关闭】显示车辆进出过程"); pm.isShowProcess = false; } else if ("show" == cmd || "display" == cmd || "dp" == cmd) { Console.WriteLine("[操作]---->【显示】停车场现在停车状态"); pm.show(); } else if ("room" == cmd || "sc" == cmd) { Console.WriteLine("[操作]---->【修改】停车场容量"); // pm.capacity == cmd; } else { Console.WriteLine("error"); } Console.Write("请输入指令:"); } } } class ParkingModel { Stack<Car> parkingCar; Queue<Car> queueCar; public int capacity = 10; public double pricePerSecond; public Boolean isShowProcess = true; public ParkingModel() { Console.WriteLine("-----------------------停车场模拟系统(最大容量:" + capacity + "辆)-----------------------" + "\r\n" + "\r\n" + "指令友情提示 :"+"\r\n"+"进入车库:" + "gogoin" + "\r\n"+"离开车库:"+"out"+"\r\n"+ "便道车离开:"+"gun"+"\r\n"+"显示车库状态:"+""+"show" +"\r\n"+"修改车库容量:"+""+"room"); startParking(); Console.WriteLine("车场初始状态:\t"); show (); } public void startParking() { parkingCar = new Stack<Car>(); queueCar = new Queue<Car>(); #region 初始入库 parkingCar.Push(new Car("杭1", DateTime.Now, new DateTime(), 1)); parkingCar.Push(new Car("杭2", DateTime.Now, new DateTime(), 1)); parkingCar.Push(new Car("杭3", DateTime.Now, new DateTime(), 1)); parkingCar.Push(new Car("杭4", DateTime.Now, new DateTime(), 1)); parkingCar.Push(new Car("杭5", DateTime.Now, new DateTime(), 1)); parkingCar.Push(new Car("杭6", DateTime.Now, new DateTime(), 1)); parkingCar.Push(new Car("杭7", DateTime.Now, new DateTime(), 1)); parkingCar.Push(new Car("杭8", DateTime.Now, new DateTime(), 1)); parkingCar.Push(new Car("杭9", DateTime.Now, new DateTime(), 1)); parkingCar.Push(new Car("杭10", DateTime.Now, new DateTime(), 1)); queueCar.Enqueue(new Car("杭21", DateTime.Now, new DateTime(), 1)); queueCar.Enqueue(new Car("杭22", DateTime.Now, new DateTime(), 1)); queueCar.Enqueue(new Car("杭23", DateTime.Now, new DateTime(), 1)); #endregion } public void show() { Console.WriteLine("\t\t等候区:"); if (queueCar.Count == 0) { Console.WriteLine("\t\t\t无"); } else { foreach (Car car in queueCar) { Console.WriteLine("\t\t\t" + car.id); } } Console.WriteLine("\t\t停车区:" + (parkingCar.Count == capacity ? "【满】" :"还有空位")); if (parkingCar.Count == 0) { Console.WriteLine("\t\t\t无"); } else { foreach (Car car in parkingCar) { Console.WriteLine("\t\t\t" + car.id); } } } public void gogoin(String id) { if (parkingCar.Count < capacity) { parkingCar.Push(new Car(id , DateTime.Now, new DateTime(), 1)); if (true) { Console.WriteLine( id + " 进入停车场"); } } else { queueCar.Enqueue(new Car(id, DateTime.Now, new DateTime(), 1)); if (true) { Console.WriteLine( id + " 进入候车区"); } } } public void parkingGo(String id) { Stack<Car> tempStack = new Stack<Car>(); Car car; while (true) { if (parkingCar.Count == 0) { Console.WriteLine("无此车辆!"); break; } car = parkingCar.Pop(); if (car.id == id) { if (isShowProcess) { Console.WriteLine(car.id + "-离开停车场"); } car.OutTime = DateTime.Now; TimeSpan ts = car.OutTime - car.InTime; double charge = ts.TotalSeconds * pricePerSecond; Console.WriteLine( car.id + "-需交费:" + Convert.ToDouble(charge).ToString("0.0") + "元" + "\t车辆停留时间:" + ts.Minutes + "分钟 " + ts.Seconds + "秒 "); break; } else { tempStack.Push(car); if (isShowProcess) { Console.WriteLine("---车牌号:" + car.id + " 进入临时栈---"); } } } foreach (Car tcar in tempStack) { parkingCar.Push(tcar); if (isShowProcess) { Console.WriteLine( tcar.id + " 回到停车场"); } } if (queueCar.Count > 0 && parkingCar.Count < capacity) { car = queueCar.Dequeue(); parkingCar.Push(car); Console.WriteLine( car.id + " 从候车区进入停车场"); } } public void queueGo(String id) { Queue<Car> tempQueue = new Queue<Car>(); Car car; int i = queueCar.Count; bool flag = true; while (i-- != 0) { car = queueCar.Dequeue(); if (car.id == id) { if (isShowProcess) { Console.WriteLine(car.id + "离开候车区"); } flag = false; } else { tempQueue.Enqueue(car); if (isShowProcess) { Console.WriteLine(car.id + " 进入临时队列"); } } } if (flag) { Console.WriteLine("查无此车辆!"); } foreach (Car tcar in tempQueue) { queueCar.Enqueue(tcar); if (isShowProcess) { Console.WriteLine( tcar.id + " 回到候车队列"); } } } } class Car { public Car(String id, DateTime InTime, DateTime OutTime, int carSize) { this.id =id ; this.InTime = InTime; this.OutTime = OutTime; this.carSize = carSize; } public String id; public DateTime InTime; public DateTime OutTime; public int carSize; } }
说点什么
欢迎讨论