实习报告停车场.docx
《实习报告停车场.docx》由会员分享,可在线阅读,更多相关《实习报告停车场.docx(13页珍藏版)》请在冰豆网上搜索。
![实习报告停车场.docx](https://file1.bdocx.com/fileroot1/2022-11/21/75d6a51f-9981-43a2-a732-b01f53802996/75d6a51f-9981-43a2-a732-b01f538029961.gif)
实习报告停车场
实习报告
姓名:
陈英 学号:
3090717331
1.实验题目
编制停车场管理系统的程序
2.需求分析
设停车场内只有一个可停放n辆汽车的狭长通道,且只有一个大门可供汽车进出。
汽车在停车场内按车辆到达时间的先后顺序,依次由北向南排列(大门在最南端,最先到达的第一辆车停放在车场的最北端),若车场内已停满n辆汽车,则后来的汽车只能在门外的便道上等候,一旦有车开走,则排在便道上的第一辆车即可开入;当停车场内某辆车要离开时,在它之后开入的车辆必须先退出车场为它让路,待该辆车开出大门外,其它车辆再按原次序进入车场,每辆停放在车场的车在它离开停车场时必须按它停留的时间长短交纳费用。
试为停车场编制按上述要求进行管理的模拟程序。
1).输入数据和输出数据的形式、取值范围;
输入数据:
形式:
文本;取值范围:
栈的最大值为10;
输出数据:
形式:
文本;取值范围:
”括号匹配“,括号不匹配;
2).测试数据以及测试目的
测试数据:
(A,2,1),(A,3,2),(D,3,1),(D,2,1),(E,0,0)
测试目的:
测试汽车达到和离去的情况
3.概要设计
(1)设计思路
1)用栈模拟停车场,用队列模拟停车场外的狭长通道,按照从终端读入数据序列进行模拟管理
2)每一组输入数据包括两个数据项,汽车到达或离去的信息,汽车牌号码。
3)每次输入完进行输出操作,若是车辆到达,输出汽车在停车场内或下通道上的停车位置;若是离去,输出汽车离去信息,若有等候车辆,还会输出从通道进入车场的车牌号码;
4)其中栈依顺序存储结构实现,队列依链表存储结构实现。
(2)该程序有4个主要函数
1)主函数main()
2)创建栈stack()
3)创建队列shortgy()
4)建立节点node()
4.详细设计
(1)定义栈(临时停车栈和停车栈)以及基本操作
临时栈和停车场:
classstack{//临时栈和停车场
inttop;
ints[]=newint[3];
元素出栈:
voidinit_stack()
{
top=-1;
}
元素入栈:
voidpush_stack(intx)
{
s[top]=x;
}
删除栈顶元素:
voidpop_stack()
{//删除栈顶元素
top=top-1;
}
inttop_stack()
{//取得栈顶元素
returns[top];
}
判断栈空:
booleanempty_stack()
{//判断停车场是否有车
if(top==-1)returntrue;
elsereturnfalse;
}
判断栈满:
booleanfull_stack()
{
if(top==3)returntrue;
elsereturnfalse;
}
(2)定义队列(停车场外的狭长通道)以及基本操作
狭长通道队列:
classshortgy
{//便道队列
Nodehead,tail;
初始化队列:
publicvoidinit_shortgy()
{//初始化队列
head=newNode();
tail=head;//表示队列的头结点和队列的尾节点
head.next=null;
}
元素入队publicvoidappendToTail(Noden)
{//插如节点到编导队列中
if(head==null)//链表为空,返回”便道为空
{
}
else{
tail.next=n;
tail=n;
}//插入节点n到链表的尾部
}
元素出队:
publicvoiddelectHeadTail(Noden)
{//删除便道队列的第一节点
if(head==null)//链表为空,返回”便道为空
{System.out.println("便道为空");}
else{head=head.next;}//删除第一个节点
}
返回首车牌号:
intgetHeadList()
{//返回队首车牌号
returnhead.next.number;
}
判断空队:
booleanempty_shortgy()
{//若队列为空则返回true,否则返回false
if(head==tail){returntrue;}
elsereturnfalse;
}
}
(3)判断车到达的情况
if(ps.top>=2)
{
q.appendToTail(node);//若停车场栈满车进入便道队
i++;
System.out.println("本车停放在便道第"+i+"号位置");
}
else//否则车进入停车场
{
ps.top++;
ps.push_stack(num);
j++;
System.out.println("本车停在停车场第"+j+"号位置");
}
}break;
(4)判断车离去的情况
while((!
ps.empty_stack())&&(ps.top_stack()!
=num))
{//要离开的车不在停车场首位
temp=ps.top_stack();
ps.pop_stack();
ts.top++;
ts.push_stack(temp);
}
if(ps.empty_stack())
{
System.out.println("停车场无"+n+"号码的车");}
else//要离开的车正好在首位
{
System.out.println(ps.top_stack()+"离去");
ps.pop_stack();
out=true;//停车场空出一个位置
}
while(!
ts.empty_stack())//如果栈不为空
{
temp=ts.top_stack();//临时栈的车回到停车场
ts.pop_stack();
ps.top++;
ps.push_stack(temp);
ps.display();
}
if(out&&!
q.empty_shortgy())//停车场栈有车要离去{//若便道队非空,让便道队头回到停车场中
temp=q.getHeadList();
//q.deleteHeadTail(Noden);
out=false;//停车场中的空位补上了
System.out.println(temp+"进入停车场");
ps.push_stack(temp);
}
}break;
(5)定义结点类
classNode{
intnumber;//表示车牌号
Nodenext;//节点中的指针,指向下一个Node对象的引用
}
5.调试分析
1)问题1:
编译car.java时经常出问题,有时粗心打错了变量名,有时时字母打错。
后经过耐心的检查,完成程序。
编程要求很细心和有耐心。
2)问题2:
使用了栈,但是车出栈的时候不按先进后出原则。
在我经过
多次检查都不能解决,不过从中我学会了如何使用API文档,在以后要进一步学习栈和队列。
6.使用说明
程序名为car.Java,使用bluej运行,程序执行后可看到如下:
请输入信息(A:
到达D:
离去E:
结束):
1.输入A后再输入车牌号码,就会输出车停放的位置
输入D后再输入车牌号码,如果输入的车牌号没停放在停车场中,结果就显示没有此车。
如果输入的车号码存在,则显示车离去的信息。
输入E,结果就会显示(E,0,0)结束程序。
7.测试结果
附录:
源程序
classNode{
intnumber;//表示车牌号
Nodenext;//节点中的指针,指向下一个Node对象的引用
}
队列类
classshortgy
{//便道队列
Nodehead,tail;
publicvoidinit_shortgy()
{//初始化队列
head=newNode();
tail=head;//表示队列的头结点和队列的尾节点
head.next=null;
}
publicvoidappendToTail(Noden)
{//插如节点到编导队列中
if(head==null)//链表为空,返回”便道为空
{
head=n;
tail=n;
}
else{
tail.next=n;
tail=n;
}//插入节点n到链表的尾部
}
publicvoiddelectHeadTail(Noden)
{//删除便道队列的第一节点
if(head==null)//链表为空,返回”便道为空
{System.out.println("便道为空");}
else{head=head.next;}//删除第一个节点
}
intgetHeadList()
{//返回队首车牌号
returnhead.next.number;
}
booleanempty_shortgy()
{//若队列为空则返回true,否则返回false
if(head==tail){returntrue;}
elsereturnfalse;
}
}
classstack{//临时栈和停车场
inttop;
ints[]=newint[3];
voidinit_stack()
{
top=-1;
}
voidpush_stack(intx)
{
s[top]=x;
}
voidpop_stack()
{//删除栈顶元素
top=top-1;
}
inttop_stack()
{//取得栈顶元素
returns[top];
}
booleanempty_stack()
{//判断停车场是否有车
if(top==-1)returntrue;
elsereturnfalse;
}
booleanfull_stack()
{
if(top==3)returntrue;
elsereturnfalse;
}
intgetHeadStack()
{//返回停车场栈首位车牌号
returns[top];
}
voiddisplay()
{
if(top==-1){System.out.println("停车场无车");}
else
{
System.out.println("停车场存放有车");
System.out.println(s[top]);
}
}
}
importjava.io.*;
importjava.awt.*;
publicclasscar{
publicstaticvoidmain(String[]args)throwsIOException
{
booleanout=false;
inttemp,i=0,j=0;
Stringstate;
stackts=newstack(),ps=newstack();
shortgyq=newshortgy();
ts.init_stack();
ps.init_stack();
q.init_shortgy();//定义两个空栈ts,ps和一个队列q
while(true)
{
BufferedReaderkeyin=newBufferedReader(newInputStreamReader(System.in));
System.out.println("请输入信息(A:
到达D:
离去E:
结束):
");
state=keyin.readLine();//读取选项
//System.out.println("汽车车牌号码;");
System.out.println("请输入汽车车牌号码");
intn=Integer.parseInt(keyin.readLine());//输入的车牌号为整型
System.out.println("你输入的汽车车牌号码为"+n+"");
intnum=n;//车牌号
Nodenode=newNode();
node.number=num;
charc=state.charAt(0);//取出输入字符串的首字母
switch(c)
{
case'A':
{
if(ps.top>=2)
{
q.appendToTail(node);//若停车场栈满车进入便道队
i++;
System.out.println("本车停放在便道第"+i+"号位置");
}
else//否则车进入停车场
{
ps.top++;
ps.push_stack(num);
j++;
System.out.println("本车停在停车场第"+j+"号位置");
}
}break;
case'D':
{
while((!
ps.empty_stack())&&(ps.top_stack()!
=num))
{//要离开的车不在停车场首位
temp=ps.top_stack();
ps.pop_stack();
ts.top++;
ts.push_stack(temp);
}
if(ps.empty_stack())
{
System.out.println("停车场无"+n+"号码的车");}
else//要离开的车正好在首位
{
System.out.println(ps.top_stack()+"离去");
ps.pop_stack();
out=true;//停车场空出一个位置
}
while(!
ts.empty_stack())//如果栈不为空
{
temp=ts.top_stack();//临时栈中的车回到停车场
ts.pop_stack();
ps.top++;
ps.push_stack(temp);
ps.display();
}
if(out&&!
q.empty_shortgy())//停车场栈中有车要离开
{//若便道队非空,让便道队头回到停车场中
temp=q.getHeadList();
//q.deleteHeadTail(Noden);
out=false;//停车场中的空位补上了
System.out.println(temp+"进入停车场");
ps.push_stack(temp);
}
}break;
case'E':
{
ps.top=0;
ts.top=0;
System.out.println("(E,0,0)");
}break;
default:
System.out.println("输入有误,请输入(A,D,E)");
}
}
}
}