java使用栈和队列模拟停车场问题

xuyiqing      2022-04-19     151

关键词:

停车场管理:

本题为计算机系专业课数据结构实验

[问题描述]

设停车场是一个可以停放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;

        }
    }
}

 



sdut-refresh的停车场(栈和队列)

题目描写叙述 refresh近期发了一笔横財,开了一家停车场。因为土地有限,停车场内停车数量有限,可是要求进停车场的车辆过多。当停车场满时,要进入的车辆会进入便道等待。最先进入便道的车辆会优先进入停车场,并... 查看详情

数据结构和算法之栈和队列一:两个栈模拟一个队列以及两个队列模拟一个栈

...经常看到的两种结构,栈和队列。可以说我们是一直都在使用栈,比如说在前面递归所使用的的系统的栈,以及在链表倒序输出时介绍的自定义栈类Stack和使用系统的栈进行递归。那么,在这里我们就讲述一下这两个比较具有特... 查看详情

数组和链表的区别arraylist和linkedlist的区别使用linkedlist模拟栈和队列(代码片段)

...绍了数组和链表的区别,ArrayList和LinkedList的区别以及使用LinkedList模拟栈和队列。文章目录1数组和链表的结构的异同2ArrayList和LinkedList的异同3栈和队列的模拟3.1模拟栈3.2模拟队列1数组和链表的结构的异同相同点:数组和... 查看详情

栈和队列

...的数组就叫做栈FIRSTINLASTOUT先进的最后出栈的特点:何时使用:如果希望永远保持使用最新的元素,就要使用栈结构。 如何使用:1、从结尾出入栈入栈(压栈):arr.push(值)出栈(弹栈):varlast=arr.pop()特点 查看详情

栈和队列-

...务队列简单模拟2.4PTA提交列表说明。1.7-2符号配对最初没使用c++编译器审题不清,这题最大难点在于注释符号的配对,需 查看详情

使用多线程在 Java 中模拟停车场中的多个入口和出口

】使用多线程在Java中模拟停车场中的多个入口和出口【英文标题】:SimulatingMultpleEntriesandExitsinaVehicleCarParkinJavawithMultiThreading【发布时间】:2022-01-0913:40:42【问题描述】:我目前创建了CarPark和Floor等类来表示停车场。我使用了Floo... 查看详情

c++停车场问题谁能帮我写一下啊

...些最好有注释问题描述设有一个可以停放n辆汽车的狭长停车场,它只有一个大门可以供车辆进出。车辆按到达停车场时间的早晚依次从停车场最里面向大门口停放(最先到达的第一辆车放在停车场最里面)。如果停车场已经放... 查看详情

第三章:6.栈和队列--离散事件模拟

前言:  本节讲述,队列的入队和离队行为,由事件决定情况下,是如何实现的。目录:  离散事件模拟正文:  问题:假设某银行有4个窗口对外接待客户,从早晨银行开门起不断有客户进入银行。由于每个窗口在某个... 查看详情

数据结构15:停车场管理系统(代码片段)

...先自行完成,然后参照本节给出的完整代码。项目简介设停车场是一个可以停放n辆汽车的南北方向的狭长通道,且只有一个大门可供汽车 查看详情

如何模拟停车场的边界?

】如何模拟停车场的边界?【英文标题】:HowcanIsimulateaborderofaparkingplace?【发布时间】:2021-04-2002:26:22【问题描述】:我目前正在研究停车场模拟。在进入停车场之前,汽车必须越过边界。为了模拟这一点,我添加了一个“carMove... 查看详情

c++栈和队列(stack&queue)(代码片段)

栈和队列一.stack的介绍和使用(1)概念(2)常见接口(3)使用1.最小栈问题2.栈的压入、弹出序列3.逆波兰表达式(4)stack的模拟实现二.queue的介绍和使用(1)概念(2)常见接口... 查看详情

c++栈和队列(stack&queue)(代码片段)

栈和队列一.stack的介绍和使用(1)概念(2)常见接口(3)使用1.最小栈问题2.栈的压入、弹出序列3.逆波兰表达式(4)stack的模拟实现二.queue的介绍和使用(1)概念(2)常见接口... 查看详情

数据结构学习--java栈和队列

...组存放,但是删除的时候不是删除了数组中的数据,而是使用增加游标标识的方式实现删除,“游标标识”加加或者减减完成删除操作,查看的时候,也不是直接查看栈和队列的数组元素,而是使用游标的方式向外查看。... 查看详情

第03次作业-栈和队列

 1.学习总结2.PTA实验作业2.1题目栈:7-1字符串是否对称7-2符号配对队列:7-2银行业务队列的简单模拟2.2设计思路栈:7-1字符串是否对称7-2符号配对队列:7-2银行业务队列的简单模拟2.3代码截图 栈: 7-1字符串是否对称&n... 查看详情

基于停车管理系统的分析与理解(代码片段)

这个程序的要求是:停车场是一个能放n辆车的狭长通道,只有一个大门,汽车按到达的先后次序停放。若车场满了,车要在门外的便道上等候,一旦有车走,则便道上第一辆车进入。当停车场中的车离开时,由于通道窄,在它... 查看详情

表栈和队列

   这是最基本的三种数据结构,每个程序中都至少使用一种,比如说栈,递归程序其实就是借助方法调用栈来使用的。站在计算机的角度,没有什么问题是栈解决不了的。本文基于Java语言对这些数据结构进行说明,并结合JD... 查看详情

java栈和队列·下(代码片段)

...法:2.2实现队列也可以数组和链表的结构实现,使用链表的结构实现更优一些,因为如果使用数组的结构,出队列在数组头上出数据,效率会比较低。classNodepublicintval;publicNodenext;publicNode(intval)this.val=val 查看详情

stl栈和队列的内部实现

...queues【发布时间】:2014-08-1515:54:32【问题描述】:我正在使用stl堆栈和队列来存储大量项目。标准模板库中的堆栈是如何在内部实现的?是链表的形式吗?或者有没有给它任何最大尺寸?【问题讨论】:std::stack默认使用std::deque... 查看详情