停车场管理 (数据结构)
2009-03-27 14:44
197 查看
#include<iostream>
using namespace std;
#define null 0
int getn()
{
int n;
cout<<"本停车场每小时10元."<<endl;
cout<<"请输入停车场可容纳的车:";
cin>>n;
return n;
}
int n=getn();
struct car{
int num;
int time;
car *next;
};
struct stack{
int top;
int base0,base1,base2;
car *stack0;
};
void initstack(stack &s){
s.stack0=new car
;
s.top=s.base0=s.base1=-1;
s.base2=0;
}
void push(stack &s,car p){
s.top++;
s.stack0[s.top]=p;
}
void pop(stack &s,car &p){
if(s.top==-1)cout<<"no car!!";
else {p=s.stack0[s.top];s.top--;}
}
struct Queue{
car *front,*front1;
car *rear;
int k;
};
void initQueue(Queue &Q){
Q.rear=Q.front=Q.front1=new car;
Q.front->next=null;
Q.k =0;
}
void EnQueue(Queue &Q,car *p){
car *pt;
pt=new car;
pt=p;
pt->next=null;
Q.rear->next=pt;
Q.rear=pt;
Q.k++;
}
void DeQueue(Queue &Q,car &p){
car *ps;
if(Q.rear==Q.front)cout<<"便道上的车为空!!"<<endl;
else{
Q.k--;
ps=Q.front->next;
p=*ps;
Q.front->next=ps->next;
if(Q.rear ==ps)Q.rear =Q.front ;
delete ps;
}
}
int travel(stack &s,Queue &Q,car &pt){
int k;
k=1;
while(s.base2<=s.top){
if(pt.num==s.stack0[s.base2].num||pt.time<=s.stack0[s.base2].time){
k=0;break;
}
s.base2++;
}
while(Q.front1->next!=null){
if(Q.front1->next->num==pt.num||pt.time<=Q.front1->next->time){
k=0;break;
}
Q.front1=Q.front1->next;
}
s.base2=0;
Q.front1=Q.front;
return k;
}
int main(){
char c;
stack s,s1;
Queue Q;
initstack(s);
initstack(s1);
initQueue(Q);
car *ps;
car p,pt,p1,p2;
while(cin>>c){
ps=new car;
cin>>ps->num>>ps->time;
pt.num=ps->num;pt.time=ps->time;
if(c=='e')break;
if(c=='a'){
travel(s,Q,pt);
if(travel(s,Q,pt)==0){cout<<"输入车辆错误!"<<endl;continue;}
else{
if(s.top<=n-2){
push(s,pt);s.base0=s.top;cout<<"车牌号码为"<<pt.num<<"的车,停车位置为停车场"<<s.top<<"号!"<<endl;
}
else {
EnQueue(Q,ps);cout<<"车牌号码为"<<pt.num<<"的车,停车位置为便道"<<Q.k<<"号!"<<endl;
}
}
}
else if(c=='d'){
while(s.stack0[s.top].num!=ps->num&&s.base1!=s.top){
pop(s,p);push(s1,p);
}
if(s.base1==s.top){cout<<"没有对应汽车"<<endl;s.top=s.base0;s1.top=-1;continue;}
else{
pop(s,p);p2.time=pt.time;
if(ps->time<p.time){cout<<"输入时间错误!"<<endl;push(s,p);
while(s.top<=n-2&&s1.top>=0){
pop(s1,p);push(s,p);s.base0=s.top;
}
continue;
}
else{cout<<"车号为"<<p.num<<"的车停车时间为:"<<ps->time-p.time<<"小时"<<endl;
cout<<"车号为"<<p.num<<"的车该负现金为:"<<(ps->time-p.time)*10<<"元!"<<endl;
}
while(s.top<=n-2&&s1.top>=0){
pop(s1,p);push(s,p);s.base0=s.top;
}
}
while(s.top<=n-2){
if(Q.rear==Q.front){cout<<"便道上的车为空!!"<<endl;break;}
else{DeQueue(Q,p1); p1.time=p2.time;push(s,p1);}
}
}
else{
cout<<"输入格式错误"<<endl;break;
}
}
return 0;
}
using namespace std;
#define null 0
int getn()
{
int n;
cout<<"本停车场每小时10元."<<endl;
cout<<"请输入停车场可容纳的车:";
cin>>n;
return n;
}
int n=getn();
struct car{
int num;
int time;
car *next;
};
struct stack{
int top;
int base0,base1,base2;
car *stack0;
};
void initstack(stack &s){
s.stack0=new car
;
s.top=s.base0=s.base1=-1;
s.base2=0;
}
void push(stack &s,car p){
s.top++;
s.stack0[s.top]=p;
}
void pop(stack &s,car &p){
if(s.top==-1)cout<<"no car!!";
else {p=s.stack0[s.top];s.top--;}
}
struct Queue{
car *front,*front1;
car *rear;
int k;
};
void initQueue(Queue &Q){
Q.rear=Q.front=Q.front1=new car;
Q.front->next=null;
Q.k =0;
}
void EnQueue(Queue &Q,car *p){
car *pt;
pt=new car;
pt=p;
pt->next=null;
Q.rear->next=pt;
Q.rear=pt;
Q.k++;
}
void DeQueue(Queue &Q,car &p){
car *ps;
if(Q.rear==Q.front)cout<<"便道上的车为空!!"<<endl;
else{
Q.k--;
ps=Q.front->next;
p=*ps;
Q.front->next=ps->next;
if(Q.rear ==ps)Q.rear =Q.front ;
delete ps;
}
}
int travel(stack &s,Queue &Q,car &pt){
int k;
k=1;
while(s.base2<=s.top){
if(pt.num==s.stack0[s.base2].num||pt.time<=s.stack0[s.base2].time){
k=0;break;
}
s.base2++;
}
while(Q.front1->next!=null){
if(Q.front1->next->num==pt.num||pt.time<=Q.front1->next->time){
k=0;break;
}
Q.front1=Q.front1->next;
}
s.base2=0;
Q.front1=Q.front;
return k;
}
int main(){
char c;
stack s,s1;
Queue Q;
initstack(s);
initstack(s1);
initQueue(Q);
car *ps;
car p,pt,p1,p2;
while(cin>>c){
ps=new car;
cin>>ps->num>>ps->time;
pt.num=ps->num;pt.time=ps->time;
if(c=='e')break;
if(c=='a'){
travel(s,Q,pt);
if(travel(s,Q,pt)==0){cout<<"输入车辆错误!"<<endl;continue;}
else{
if(s.top<=n-2){
push(s,pt);s.base0=s.top;cout<<"车牌号码为"<<pt.num<<"的车,停车位置为停车场"<<s.top<<"号!"<<endl;
}
else {
EnQueue(Q,ps);cout<<"车牌号码为"<<pt.num<<"的车,停车位置为便道"<<Q.k<<"号!"<<endl;
}
}
}
else if(c=='d'){
while(s.stack0[s.top].num!=ps->num&&s.base1!=s.top){
pop(s,p);push(s1,p);
}
if(s.base1==s.top){cout<<"没有对应汽车"<<endl;s.top=s.base0;s1.top=-1;continue;}
else{
pop(s,p);p2.time=pt.time;
if(ps->time<p.time){cout<<"输入时间错误!"<<endl;push(s,p);
while(s.top<=n-2&&s1.top>=0){
pop(s1,p);push(s,p);s.base0=s.top;
}
continue;
}
else{cout<<"车号为"<<p.num<<"的车停车时间为:"<<ps->time-p.time<<"小时"<<endl;
cout<<"车号为"<<p.num<<"的车该负现金为:"<<(ps->time-p.time)*10<<"元!"<<endl;
}
while(s.top<=n-2&&s1.top>=0){
pop(s1,p);push(s,p);s.base0=s.top;
}
}
while(s.top<=n-2){
if(Q.rear==Q.front){cout<<"便道上的车为空!!"<<endl;break;}
else{DeQueue(Q,p1); p1.time=p2.time;push(s,p1);}
}
}
else{
cout<<"输入格式错误"<<endl;break;
}
}
return 0;
}
相关文章推荐
- 【数据结构】关于停车场的管理
- 【数据结构】 停车场管理优化
- 数据结构 课设一:停车场管理
- 停车场的模拟管理(数据结构 C++)
- 停车场管理,初步成型样,还差一点功能
- 停车场管理(栈与队列的应用)
- 用数据库实现停车场管理
- C语言数据结构之学生信息管理系统课程设计
- 数据结构项目二:停车场管理
- 数据结构-链表的简实现->学生管理系统
- servlet停车场信息管理系统
- 【开源.NET】 分享一个前后端分离的轻量级内容管理框架(第二篇前后端交互数据结构分析)
- 数据结构记录--图书管理系统
- 【开源.NET】 分享一个前后端分离的轻量级内容管理框架(第二篇前后端交互数据结构分析)
- 停车场系统管理
- 数据结构简单的停车管理系统
- 请实现一个队列,既可以存放整数,又可以存放字符串。简单的说,队列是一种数据结构,按照先进先出的顺序管理进、出队列的元素
- 第二章 存储管理 几个重要的数据结构和函数
- 天耀18期 –12.数据结构 ArrayList【作业】-计算机管理
- 数据结构课程设计--立体停车场管理系统(改)