*问题描述
* 设停车藏是一个可以停放n辆车的狭长的通道,且只有一个大门可供进出。汽车在停车场内按到达的先后顺序,一次由北向南停放,若场内停放已满,那么就先停放在便道中。
* 一旦由车从停车场内出来后。便道中的第一辆车马上那个听进去。当停车场内由车要出来时。在他后面的车要为他让路。然后再按原顺序进入停车场,收取费用。
parking.java
/**
* ***********CopyRight**************
*-------Powered by QianXunNet-----
*-----Version 1.00 2009-01-17-----
*----- Design BY NiChao -----
*=========description===========
*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
*/
package yanweimin.Parking;
import java.util.Scanner;
import yanweimin.Parking.*;
import java.util.Scanner;
public class Park {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//设定一个便道和两个车库
System.out.println("the parking money is 10 yuan per hour");
System.out.println("input a num for the maxsize of parking:");
Scanner in = new Scanner(System.in);
int maxsize = in.nextInt();
Queue biandao = new Queue();
StackArray main = new StackArray(maxsize,"主车库");
StackArray fuzhu = new StackArray(maxsize, "辅助车库");
loop: while(true)
{
Scanner cin = new Scanner(System.in);
System.out.println("请输入当前时间:");
//======当前时间=======
int time = cin.nextInt();
System.out.println("select === 1: 到达 2.离去 3.结束程序");
int select = cin.nextInt();
switch(select)
{
//车辆到站
case 1 :
{
System.out.println("input the num of car");
int num = cin.nextInt();
Car mycar = new Car(time ,num);
int into = main.push(mycar);
if(into == -1)
{
biandao.addQueue(mycar);
}
break;
}
//出车库
case 2 :
{
System.out.println("input the num of the car you want to out:");
int num = cin.nextInt();
while( main.stackvalue[main.p].num != num)
{
Car acar = main.stackvalue[main.p];
main.pop();
fuzhu.push(acar);
}
Car right_car = main.stackvalue[main.p];
main.pop();
int money = (time - right_car.time)*10;
System.out.println("该车应该付费"+money);
while( fuzhu.p != -1)
{
Car acar = fuzhu.stackvalue[fuzhu.p];
fuzhu.pop();
main.push(acar);
}
if(biandao.rear != -1)
{
Car acar = biandao.queuevalue[0];
acar.time = time;
biandao.delQueue();
main.push(acar);
}
break;
}
case 3 :
{
break loop;
}
}
}
}
}
queue.class
package yanweimin.Parking;
//优化过的数组模拟的队列类
import yanweimin.Parking.Car;
public class Queue {
/*
* 查看队列数组中的数
*/
// public void coutQueue()
// {
//设定一根移动指针 用于指示当前的虚拟队头
// if(this.rear == -1)
// System.out.println("当前队列为空");
// else
// {
// int j = 0;
// for(int i =0; i<=rear;i++,j++)
// {
// System.out.print(queuevalue[i]+" ");
// }
// System.out.println("共有元素"+j+"个");
// }
// }
/*
* 向队列中增加元素
* @param num 欲增加的值
*/
public void addQueue(Car car)
{
//队列已满
if(this.rear == this.maxsize-1)
{
System.out.println("便道已满");
}
else
{
this.rear ++;
this.queuevalue[this.rear] = car;
System.out.println(" 车牌号为 "+car.num+"进入便道了");
}
}
/*
* 取出队列中的元素
* @return num 取出的元素
*/
public void delQueue()
{
if(this.rear == -1)
{
System.out.println(" 便道已经空");
}
else
{
Car car = this.queuevalue[0];
int temp = 0;
while ( temp != this.rear )
{
this.queuevalue[temp] = this.queuevalue[temp +1];
temp ++;
}
this.rear --;
System.out.println("车牌号为"+car.num+"出便道了");
}
}
public int maxsize = 20;
public Car[] queuevalue = new Car[maxsize];
public int rear = -1;
}
stackarray.class
package yanweimin.Parking;
import yanweimin.Parking.Car;
public class StackArray {
//构造函数
public StackArray(int maxsize,String name)
{
p = -1;
this.maxsize = maxsize;
stackvalue = new Car[maxsize];
this.name = name;
}
//打印数组内的数据
// public void print()
// {
//指针指向当前栈的有效数组上
// if(this.p==-1)
// System.out.println("栈空");
// else
//{
// int i = this.p;
// while(i>=0)
// {
// System.out.println(this.stackvalue[p]);
// i--;
// }
// }
// } //print
//进车库
//返回值 -1 没有成功进入车库
//返回值 1 成功进入车库
public int push(Car value)
{
if(this.p == this.maxsize-1 )
{
System.out.println(name+"已满");
return -1;
}
else
{
this.p++;
this.stackvalue[p] = value;
System.out.println("车牌为 "+value.num+"进入了"+this.name);
return 1;
}
} //push
public int pop()
{
// int temp = -1;
if(this.p == -1)
{
System.out.println("车库中已经没有车了");
return -1;
}
else
{
int temp;
temp = this.stackvalue[p].num;
System.out.println("车牌号为 "+temp+" 出"+this.name);
this.p--;
return temp;
}
}
int p; //栈内指针
int maxsize; //栈的最大容量
Car[] stackvalue;
String name;
}
测试结果
--------------------------------------------
the parking money is 10 yuan per hour
input a num for the maxsize of parking:
2
请输入当前时间:
5
select === 1: 到达 2.离去 3.结束程序
1
input the num of car
1
车牌为 1进入了主车库
请输入当前时间:
10
select === 1: 到达 2.离去 3.结束程序
1
input the num of car
2
车牌为 2进入了主车库
请输入当前时间:
15
select === 1: 到达 2.离去 3.结束程序
2
input the num of the car you want to out:
1
车牌号为 2 出主车库
车牌为 2进入了辅助车库
车牌号为 1 出主车库
该车应该付费100
车牌号为 2 出辅助车库
车牌为 2进入了主车库
请输入当前时间:
20
select === 1: 到达 2.离去 3.结束程序
1
input the num of car
3
车牌为 3进入了主车库
请输入当前时间:
25
select === 1: 到达 2.离去 3.结束程序
1
input the num of car
4
主车库已满
车牌号为 4进入便道了
请输入当前时间:
30
select === 1: 到达 2.离去 3.结束程序
1
input the num of car
5
主车库已满
车牌号为 5进入便道了
请输入当前时间:
35
select === 1: 到达 2.离去 3.结束程序
2
input the num of the car you want to out:
2
车牌号为 3 出主车库
车牌为 3进入了辅助车库
车牌号为 2 出主车库
该车应该付费250
车牌号为 3 出辅助车库
车牌为 3进入了主车库
车牌号为4出便道了
车牌为 4进入了主车库
请输入当前时间:
40
select === 1: 到达 2.离去 3.结束程序
2
input the num of the car you want to out:
4
车牌号为 4 出主车库
该车应该付费50
车牌号为5出便道了
车牌为 5进入了主车库
请输入当前时间:
45
select === 1: 到达 2.离去 3.结束程序
3
分享到:
相关推荐
数据结构——停车场管理系统数据结构——停车场管理系统数据结构——停车场管理系统数据结构——停车场管理系统数据结构——停车场管理系统
停车场管理系统是基于数据结构中的栈设计的,用c语言编写
数据结构课程设计C++语言停车场管理系统源码,添加了UI数据结构课程设计C++语言停车场管理系统源码,添加了UI数据结构课程设计C++语言停车场管理系统源码,添加了UI数据结构课程设计C++语言停车场管理系统源码,添加...
停车场管理,数据结构课程设计,亲自测试通过,准确良好!
数据结构课程设计 停车场模拟管理系统报告(含源码).docx数据结构课程设计 停车场模拟管理系统报告(含源码).docx数据结构课程设计 停车场模拟管理系统报告(含源码).docx数据结构课程设计 停车场模拟管理系统报告(含...
实 训 报 告 实训名称: 停车场管理 姓 名: nnn 院 (系): 软件学院 专业班级: 学 号: 指导教师: 成 绩: 时间:2010 年 6 月 21 日至 2010 年 6 月 25 日 实训任务书 一、题目 1. 停车场管理 2. 员工管理系统...
本文档为数据结构停车场管理问题源代码,亲测可以运行,C语言编写
数据结构c语言版课程设计停车场管理系统方案.doc
数据结构课程设计模拟停车场管理,严蔚敏习题集题目,C++源代码。
利用栈模拟停车场,车可以单向进出停车场,可以收费
有一个停车场,只有一个门供车辆进出。最先到达的第一辆车停在停车场的最...编写一个程序模拟该停车场的管理,要求程序输出每辆车到达后的停车位置,以及某辆车离开停车场时应该缴纳的费用和它在停车场内停留的时间。
数据结构试验源代码,采用严蔚敏老师所采用的基本数据操作函数,效果很好哟!
设停车场是一个可停放n辆汽车的狭长通道,且只有一个大门可供汽车进出。汽车在停车场内按车辆到达时间的先后顺序,依次由北向南排列,若停车场内已满n辆汽车,则后来的汽车只能在门外的便道上等候,一旦有车开走,则...
严蔚敏版数据结构实验停车场管理包含拓展题!
实验二 停车场管理 班级:A0712 学号:12 姓名:冷清淼 成绩:__________ 指导教师签名:__________ 一、问题描述 设停车场是一个可停放n辆车的狭长通道,且只有一个大门可供汽车进出。在停车场 ,汽车按到达的先后...
数据结构习题集中的实习题目,停车场管理 c语言源码完整版
数据结构课程设计 停车场管理,带运行截图。
数据结构 停车场管理 队列栈 源代码 实习设计
本人是南京航空航天大学的学生,我们的一个计算机软件基础大作业是编写4个程序,分别是约瑟夫斯问题、停车场管理、带权图的最小生成树提取、几种排序算法的比较。希望能够帮助到大家,尤其是南航的学弟学妹们!工程...
基于c语言数据结构中栈和队列思想的简单停车场管理系统,以栈模拟停车场,以队列模拟车场外的便道,按照从终端读入的输入数据序列进行模拟管理。每一组输入数据包括三个数据项:汽车“到达”或“离去”信息、汽车...