Java使用栈和队列模拟停车场问题
Posted xuyiqing
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java使用栈和队列模拟停车场问题相关的知识,希望对你有一定的参考价值。
停车场管理:
本题为计算机系专业课数据结构实验
[问题描述]
设停车场是一个可以停放n辆汽车的狭长通道,且只有一个大门可供汽车进出。汽车在停车场内按车辆到达时间的先后顺序,依次有北向南排列(大门在最南端,最先到达的第一车停放在车场的最北端),若车场内已停满n辆车,那么后来的车只能在门外的便道上等候,一旦有车开走,则排在便道上的第一辆车即可开入;当停车场内某辆车要离开时,在它之后进入的车辆必须先退出车场为它让路,待该辆车开出大门外,其他车辆再按原次序进入车场,每辆停放在车场的车在它离开停车场时必须按它停留的时间长短交纳费用。试为停车场编制按上述要求进行管理的模拟程序。
[实现提示]
以栈模拟停车场,以队列模拟车场外的便道。每一组输入数据包括三个数据项:汽车“到达”或“离去”信息、汽车牌照号码以及到达或离去的时刻。对每一组输入数据进行操作后的输出信息为:若是车辆到达,则输出汽车在停车场内或便道上的停车位置;若是车辆离去,则输出汽车在停车场内停留的时间和应交纳的费用(在便道上停车不收费)。栈以顺序存储结构实现,队列以链表结构实现。
实现:
车辆类:
package test2; /** * 汽车 * @author Xu Yiqing * */ public class Car { String id; private long reachTime; private long leaveTime; public String getId() { return id; } public void setId(String id) { this.id = id; } public long getReachTime() { return reachTime; } public void setReachTime(long reachTime) { this.reachTime = reachTime; } public long getLeaveTime() { return leaveTime; } public void setLeaveTime(long leaveTime) { this.leaveTime = leaveTime; } }
停车场栈:
package test2; /** * 停车场栈 * @author Xu Yiqing * */ public class ParkingLod { private Car[] cars; private int top; private Integer maxSize; public Car[] getCars() { return cars; } public ParkingLod(int size) { if (size < 1) { throw new RuntimeException("请输入正确的停车场大小"); } this.cars = new Car[size]; this.maxSize = size; this.top = -1; } public boolean isEmpty() { return top == -1 ; } public int getSize() { return top+1; } public boolean push(Car car) { if (this.top == this.maxSize - 1) { throw new RuntimeException("停车场已满,无法停车"); } else { this.cars[++top] = car; return true; } } public Car pop() { if (top == -1) { throw new RuntimeException("停车场为空"); } else { return cars[top--]; } } public boolean isFull(){ return top==maxSize-1; } public Car getCar(String carId) { for(int i=0;i<this.getSize();i++) { if(carId.equals(cars[i].getId())) { return cars[i]; } } return null; } }
道路队列:
package test2; /** * 通道 * @author Xu Yiqing * */ public class Path { private Car[] cars; private int maxSize=0; private int front; private int rear; public Car[] getCars() { return cars; } public int getMaxSize() { return maxSize; } public int getFront() { return front; } public int getRear() { return rear; } public Path(int size){ if(this.maxSize >=0){ this.maxSize = size; this.cars = new Car[size]; front = rear =0; }else{ throw new RuntimeException("道路大小设置有误"); } } public boolean isEmpty(){ return rear==front; } public boolean add(Car car){ if(rear== maxSize){ throw new RuntimeException("道路已满,停不下车"); }else{ cars[rear++]=car; return true; } } public Car peek(){ if(this.isEmpty()){ throw new RuntimeException("道路异常"); }else{ return cars[front]; } } public Car poll(){ if(this.isEmpty()){ throw new RuntimeException("道路异常"); }else{ Car car = cars[front]; cars[front++] = null; return car; } } public int length(){ return rear-front; } }
主程序:
package test2; import java.awt.Color; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.JTextField; /** * 主类 * @author Xu Yiqing * */ public class Main { private static JFrame frame = null; private static JTextArea resultText; private static JTextField carIdText; // 默认车库能停10辆车,通道能停100辆车 private static ParkingLod parkingLod = new ParkingLod(10); private static Path path = new Path(100); /** * 程序入口 * @param args */ public static void main(String[] args) { frame = new JFrame("Parking Lot 1.0"); frame.setBounds(800, 300, 350, 300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setResizable(false); JPanel panel = new JPanel(); panel.setBackground(Color.lightGray); frame.add(panel); placeComponents(panel); frame.setVisible(true); } /** * 图形化界面 * @param panel Panel */ private static void placeComponents(JPanel panel) { panel.setLayout(null); JLabel carLabel = new JLabel("Car ID:"); carLabel.setBounds(10, 20, 80, 25); panel.add(carLabel); carIdText = new JTextField(20); carIdText.setBounds(100, 20, 165, 25); panel.add(carIdText); JLabel resultLabel = new JLabel("Result:"); resultLabel.setBounds(10, 150, 80, 50); panel.add(resultLabel); resultText = new JTextArea(5, 1); resultText.setEditable(false); resultText.setBounds(80, 130, 200, 100); resultText.setLineWrap(true); Font font1 = new Font("宋体", Font.BOLD, 15); resultText.setFont(font1); panel.add(resultText); JButton arriveButton = new JButton("Arrive"); arriveButton.setBackground(Color.orange); arriveButton.setBounds(50, 50, 80, 25); panel.add(arriveButton); arriveButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { arrive(); } }); JLabel authorLabel = new JLabel("Author:许一清"); Font font = new Font("宋体", Font.ITALIC, 15); authorLabel.setFont(font); authorLabel.setBounds(120, 230, 150, 25); panel.add(authorLabel); JButton leaveButton = new JButton("Leave"); leaveButton.setBounds(200, 50, 80, 25); leaveButton.setBackground(Color.pink); panel.add(leaveButton); leaveButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { leave(); } }); JButton showButton = new JButton("Show"); showButton.setBounds(50, 80, 80, 25); panel.add(showButton); showButton.setBackground(Color.green); showButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { show(); } }); JButton exitButton = new JButton("Exit"); exitButton.setBounds(200, 80, 80, 25); panel.add(exitButton); exitButton.addActionListener(new ActionListener() { /** * 退出按钮比较简单 */ @Override public void actionPerformed(ActionEvent e) { System.exit(0); } }); } /** * 展示按钮 */ protected static void show() { resultText.setText("现在停车场有:" + parkingLod.getSize() + "辆车 道路上有:" + path.length() + "辆车"); carIdText.setText(""); } /** * 离开按钮 */ protected static void leave() { Car car = new Car(); String id = carIdText.getText(); if (id.isEmpty()) { resultText.setText("请输入车牌号"); return; } else { // 车库不满直接删除车 if (!parkingLod.isFull()) { Car[] cars; Car[] resultCars = new Car[parkingLod.getSize()]; ParkingLod newParkingLod; car = parkingLod.getCar(id); if (car != null) { resultText.setText("车辆:" + car.getId() + " 离开了"); cars = parkingLod.getCars(); int index = 0; for (int i = 0; i < parkingLod.getSize(); i++) { if (!cars[i].getId().equals(id)) { resultCars[index++] = cars[i]; } } newParkingLod = new ParkingLod(10); for (int i = 0; i < index; i++) { newParkingLod.push(resultCars[i]); } parkingLod = newParkingLod; } else { resultText.setText("停车场不存在 车辆:" + carIdText.getText()); return; } } // 车库满了通道新车进入 else { Car[] cars; Car[] resultCars = new Car[parkingLod.getSize()]; car = parkingLod.getCar(id); ParkingLod newParkingLod; if (car != null) { car.setLeaveTime(System.currentTimeMillis()); long time = car.getLeaveTime() - car.getReachTime(); resultText.setText("车辆:" + car.getId() + " 离开了 停车时间:" + time + "毫秒"); cars = parkingLod.getCars(); int index = 0; for (int i = 0; i < parkingLod.getSize(); i++) { if (!cars[i].getId().equals(id)) { resultCars[index++] = cars[i]; } } newParkingLod = new ParkingLod(10); for (int i = 0; i < index; i++) { newParkingLod.push(resultCars[i]); } if (!path.isEmpty()) { Car temp = path.getCars()[path.getFront()]; newParkingLod.push(temp); temp.setReachTime(System.currentTimeMillis()); path.poll(); } parkingLod = newParkingLod; } else { resultText.setText("停车场中不存在 车辆:" + carIdText.getText()); return; } } } } /** * 到达按钮 */ protected static void arrive() { Car car = new Car(); String id = carIdText.getText(); if (!id.isEmpty()) { car.setId(id); // 判断停车场是否满 if (!parkingLod.isFull()) { car.setReachTime(System.currentTimeMillis()); parkingLod.push(car); resultText.setText("车辆:" + id + " 停车成功"); carIdText.setText(""); } else { path.add(car); resultText.setText("停车场满了 车辆:" + car.getId() + " 停在道路上"); carIdText.setText(""); } } else { resultText.setText("请输入车牌号"); carIdText.setText(""); return; } } }
以上是关于Java使用栈和队列模拟停车场问题的主要内容,如果未能解决你的问题,请参考以下文章