📄 tingchechang.txt
字号:
#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define price 0.02//每车每分中应交费用
#define Max 3//车库容量
struct time
{
int hour;
int min;
};
struct car//车辆信息
{
char num[15];
time reach;
time leave;
};
struct Tstack//模拟车站
{
car *V[Max+1];
int top;
};
struct Queue//模拟便道
{
car *data;
Queue *next;
};
struct node
{
Queue *rear;
Queue *head;
};
class carpot
{
private:
Tstack *r;
node *t;
time *p;
car *s;
public:
void IntTstack(Tstack *r);
int IntQueue(node *t);
void print(car *s,int x);//车站中车辆信息
int arrival(Tstack *en,node *m);//车辆到达
void leave(Tstack *en,Tstack *t1,node *m);
void depr1(Tstack *M);//显示车场信息
void depr2(node *N);//显示便道信息
void depr(Tstack M,node N);
};
void carpot :: IntTstack(Tstack *r)//初始化栈
{
int i;
r->top=0;
for(i=0;i<=Max;i++)
r->V[r->top]=NULL;
}
int carpot:: IntQueue(node *t)//初始化便道
{
t->head=new Queue;
if(t->head!=NULL)
{
t->head->next=NULL;
t->rear=t->head;
return(1);
}
else return(-1);
}
void carpot:: print(car *p,int x)//车站中车的信息
{
int a1,a2,b1,b2;
cout<<"请输入离开的时间:"<<endl;
cin>>p->leave.hour>>p->leave.min;
cout<<"请输入车牌号:"<<endl;
puts(p->num);
cout<<"到达时间为:"<<p->reach.hour<<p->reach.min<<endl;
cout<<"离开时间为:"<<p->leave.hour<<p->leave.min<<endl;
a1=p->reach.hour;
a2=p->reach.min;
b1=p->leave.hour;
b2=p->leave.min;
cout<<"应交费用为:"<<((b1-a1)*60+(b2-a2))*price;
/*delete(p);*/
}
int carpot::arrival(Tstack *en,node *m)//车辆到达
{
car *p;
Queue *t;
p=new car;
/* cout<<"请输入车牌号:"<<endl;
gets(p->num);*/
if(en->top<Max)
{
p=new car;
cout<<"请输入车牌号:"<<endl;
gets(p->num);
en->top++;
cout<<"车辆在车场的位置:"<<en->top<<endl;
cout<<"请输入到达时间:"<<endl;
cin>>p->reach.hour>>p->reach.min;
en->V[en ->top]=p;
return(1);
}
else //车进便道
{
cout<<"该车须在便道等待"<<endl;
t=new Queue;
t->data=p;
t->next=NULL;
m->rear->next=t;
m->rear=t;
return(1);
}
}
void carpot:: leave(Tstack *en,Tstack *t1,node *m)//车辆离开
{
int y;
car *p,*t;
Queue *q;
if(en->top>0)
{
while(1)
{cout<<"请输入车在车场的位置:"<<en->top<<endl;
cin>>y;
if(y>=1&&y<=en->top)break;
}
while(en->top>y)
{
t1->top++;
t1->V[t1->top]=en->V[en->top];
en->V[en->top]=NULL;
en->top--;
}
p=en->V[en->top];
en->V[en->top]=NULL;
en->top--;
while(t1->top>=1)
{
en->top++;
en->V[en->top]=t1->V[t1->top];
t1->V[t1->top]=NULL;
t1->top--;
}
print(p,y);
//判断通道上是否有车及车站是否已满
if((m->head!=m->rear)&&en->top<Max)
{
q=m->head->next;
t=q->data;
en->top++;
cout<<"进入车场的车牌号为:"<<t->num<<"进入车场的位置:"<<en->top<<endl;
cout<<"请输入现在的时间:"<<endl;
cin>>t->reach.hour>>t->reach.min;
m->head->next=q->next;
if(q==m->rear)
m->rear=m->head;
en->V[en->top]=t;
/* delete(q);*/
}
else cout<<"便道里没有车"<<endl;
}
else cout<<"车场里没有车"<<endl;
}
void carpot::depr1(Tstack *M)//显示车辆信息
{
int i;
if(M->top>0)
{
cout<<"车场:"<<endl;
cout<<"位置:"<<"到达时间:"<<"车牌号:"<<endl;
for(i=1;i<=M->top;i++)
{
cout<<i<<endl;
cout<<M->V[i]->reach.hour<<M->V[i]->reach.min<<endl;
puts(M->V[i]->num);
}
}
else cout<<"车场里没有车:"<<endl;
}
void carpot :: depr2(node *N)//显示便道信息
{
Queue *p;
p=N->head->next;
if(N->head!=N->rear)
{
cout<<"等待车辆的车牌号:"<<endl;
while(p!=NULL)
{
puts(p->data->num);
p=p->next;
}
}
else cout<<"便道里没有车:"<<endl;
}
void carpot:: depr(Tstack M,node N)
{
int flag,f;
flag=1;
while(flag)
{
cout<<"请选择1 2 3:"<<endl;
cout<<"车场:1"<<"便道:2"<<"返回:3"<<endl;
while(1)
{
cin>>f;
if(f>=1||f<=3)break;
else
cout<<"请选择1 2 3:"<<endl;
}
switch(f)
{
case 1:depr1(&M);break;
case 2:depr2(&N);break;
case 3:flag=0;break;
default:break;
}
}
}
void main()
{
carpot X;
Tstack F;
Tstack K;
node W;
int ch;
X.IntTstack( &F);
X.IntTstack( &F);
X.IntQueue(&W);
while(1)
{
cout<<"1车辆到达:"<<endl;
cout<<"2车辆离开:"<<endl;
cout<<"3列表显示:"<<endl;
cout<<"4退出程序:"<<endl;
while(1)
{
cin>>ch;
if(ch>=1&&ch<=4)break;
else
cout<<"请选择:1/2/3/4"<<endl;
}
switch(ch)
{
case 1:X.arrival(&F,&W);break;//车辆到达
case 2:X.leave(&F,&K,&W);break;//车辆离开
case 3:X.depr(F,W);break;//列表显示
case 4:exit(0);//退出程序
default:break;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -