📄 停车场管理系统.cpp
字号:
#include"car.h"
status InitStack(sqstack &s,int n)
{ s.base=(LelemType)malloc(n*sizeof(elemType));
if(!s.base) exit(OVeRFLOW);
s.top=s.base;
s.stacksize=n;
return OK;
}
status Push(sqstack &s,elemType e)
{ *s.top=e;
s.top++;
return OK;
}
status Pop(sqstack &s,elemType &e)
{if(s.top==s.base) return eRROR;
e=*--s.top;
return OK;
}
status InitQueue(LinkQueue &Q)
{ Q.front=Q.rear=(Queueptr)malloc(sizeof(QNode));
if(!Q.front) exit(OVeRFLOW);
Q.front->next=NULL;
return OK;
}
status destroyQueue(LinkQueue &Q)
{ while(Q.front){
Q.rear=Q.front->next;
free(Q.front);
Q.front=Q.rear;
}
return OK;
}
/*status Gethead( LinkQueue s,elemType &e)
{ if(s.front==s.rear) return eRROR;
e=s.front->data;
return OK;
}*/
status enQueue(LinkQueue &Q,elemType e)
{ Queueptr p;
Q.rear->data=e;
p=(Queueptr)malloc(sizeof(QNode));
if(!p) exit(OVeRFLOW);
p->next=NULL;
Q.rear->next=p;
Q.rear=p;
return OK;
}
status deQueue(LinkQueue &Q,elemType &e)
{ if(Q.front==Q.rear) return eRROR;
Queueptr p=Q.front;
e=p->data;
Q.front=p->next;
if(Q.rear==p->next) Q.rear=Q.front;
free(p);
return OK;
}
status InitList(tagList &L)
{ L.head=new(LNode);
L.tail=L.head;
L.size=0;
for(int i=0;i<5;i++)
L.head->ch[i]='0';
return OK;
}
status MakeNode(ListType t,char c[])
{ for(int i=0;i<5;i++)
t->ch[i]=c[i];
t->next=NULL;
return OK;
}
status Insert(tagList &L,ListType t)
{ L.tail->next=t;
L.tail=t;
L.tail->next=NULL;
return OK;
}
status judgenum(char num[])
{ int i=0;
ListType q=NULL;
if(L.size>0) q=L.head->next;
while(q)
{ if(strcmp(num,q->ch)) i=1;
q=q->next;
}
return i;
}
void deleteL(char num[])
{ ListType q=L.head->next,R=L.head;
while(q)
{ if(!strcmp(q->ch,num))
{ R->next=q->next;
if(q==L.tail) L.tail=R;
delete(q);
q=R->next;
L.size--;
}
else {R=R->next;q=q->next;}
}
}
void count(elemType q,char wh)
{ cout<<endl;
cout<<"车 型: ";
if(q.type=='p')
{cout<<"客车 ";
cout<<"收费标准: ";
if(wh=='g') cout<<"0.15";
else cout<<"0.075";
cout<<" 元/分钟 "<<endl;
}
else if(q.type=='t')
{ cout<<"卡车 ";
cout<<"收费标准: ";
if(wh=='g') cout<<"0.3";
else cout<<"0.15";
cout<<" 元/分钟 "<<endl;
}
else { cout<<"小汽车 ";
cout<<"收费标准: ";
if(wh=='g') cout<<"0.1";
else cout<<"0.05";
cout<<" 元/分钟 "<<endl;
}
cout<<"停车时间: "<<elem->time-q.time<<" 分钟 ";
cout<<" 收费金额: ";
if(wh=='g')
{ if(q.type=='p')
cout<<0.15*(elem->time-q.time)<<" 元";
else if(q.type=='t')
cout<<0.30*(elem->time-q.time)<<" 元";
else cout<<0.1*(elem->time-q.time)<<" 元";
}
else { if(q.type=='p')
cout<<0.075*(elem->time-q.time)<<" 元";
else if(q.type=='t')
cout<<0.15*(elem->time-q.time)<<" 元";
else cout<<0.05*(elem->time-q.time)<<" 元";
}
cout<<endl;
for(n=1;n<80;n++)
cout<<"-";
cout<<endl;
}
int main()
{
char c='e';
int n;
elem=new(elemType);
elem->time=0;
printmessage();
InitList(L);
cout<<" 1.请输入车场总车位数:";
cin>>n;
InitStack(garage,n);
InitQueue(waitway);
do{
Readmessage(c);
Interpret(c);
}while(c!='e');
return 0;
}
void printmessage()
{
printf("\n");
for(n=1;n<80;n++)
printf("-");
printf("\n");
printf(" 广 东 工 业 大 学 停 车 场 自 动 模 拟 管 理 系 统");
printf("\n");
for(n=1;n<80;n++)
printf("-");
printf("\n\n");
printf(" 请用户按提示信息输入:\n");
for(n=1;n<80;n++)
printf("-");
}
void Readmessage(char &c)
{
char h;
char num[5];
int time=0;
again1:;
cout<<"\n 2.请输入车辆的情况,'a'代表进站,'d'代表出站,'e'代表结束:";
cin>>c;
if(c!='a'&&c!='d'&&c!='e')
{ cout<<" 输入不正确,请按要求重新输入!"<<endl;goto again1;}
if(c=='d') {cout<<endl;goto again3;}
if(c=='e') goto end;
cout<<endl;
again2:;
cout<<" 3.请输入该车的类型,'p'代表客车,'t'代表卡车,'c'代表小汽车:";
cin>>h;
if(h!='p'&&h!='t'&&h!='c')
{ cout<<" 输入不正确,请按要求重新输入!"<<endl;goto again2;}
elem->type=h;
cout<<endl;
again3:;
cout<<" 4.请输入该车的车牌:";
cin>>num;
if(c!='d')
{if(judgenum(num)) {cout<<" 该车牌已存在!请重新输入!"<<endl;goto again3;}
else { ListType s;
s=new(LNode);
MakeNode(s,num);
Insert(L,s);
}
}
strcpy(elem->num,num);
cout<<endl;
again4:;
if(c=='a') cout<<" 5.请输入该车进车场时间:";
else cout<<" 5.请输入该车出车场的时间:";
cin>>time;
if(time<=elem->time)
{ cout<<" 时间必须递增!请重新输入!"<<endl;
goto again4;
}
elem->time=time;
cout<<endl;
end:;
}
void Interpret(char c)
{ if(c=='a')
{if(garage.top-garage.base<garage.stacksize)
{ Push(garage,*elem);
cout<<" 请停在车场第 "<< i<<" 号车位!"<<endl;
cout<<" ---------------------------";
i++;
}
else{ enQueue(waitway,*elem);
cout<<" 请停在便道!"<<endl;
cout<<" ----------";
j++;
}
}
else if(c=='d')
{ int k=1;
LelemType s,g;
elemType e;
Queueptr q;
char wh='g';
s=garage.base;
while(strcmp(s->num,elem->num)&&k<i)
{ s++;k++;}
if(k>=i)
{ q=waitway.front;
k=1;
while(strcmp(s->num,elem->num)&&k<j)
{ q++;k++;}
if(k<j) wh='w';
else wh='n';
}
//计算停车费
if(wh=='g')
{ cout<<"-----------------------------------该车在车场内--------------------------------"<<endl;
count(*s,wh);
}
else if(wh=='w')
{ cout<<"-----------------------------------该车在便道内---------------------------------"<<endl;
count(q->data,wh);
}
else { cout<<" 该车没在停车场内!"<<endl<<
" _________________"<<endl;goto end;}
//让路情况
if(wh=='g') //从车场开走
{ i--;
deleteL(s->num);
if(++s!=garage.top)
{
giveway.base=s;
giveway.top=garage.top;
giveway.stacksize=giveway.top-giveway.base;
cout<<" 为该车让路,须从入口退出车场的车辆顺序: ";
g=giveway.top;
while(g!=giveway.base)
{ g--;
cout<<" "<<g->num;
}
cout<<endl;
}
else cout<<" 不必让路,该车可直接从车场开走!"<<endl;
s--;
g=giveway.base;
while(g!=giveway.top)
{*s=*g;s++;g++;}
garage.top--;
if(waitway.front!=waitway.rear) //便道上的车辆数目不为零
{ if(deQueue(waitway,e))
Push(garage,e);
cout<<" 请便道内车牌为 "<<e.num<<" 的车进车场,停在第"<<i<<"号车位! "<<endl;
cout<<" ______________________________________________________"<<endl;
j--;
i++;
}
}
else if(wh=='w') //从便道上开走
{ j--;
deleteL(s->num);
if(q!=waitway.front)
{
LinkQueue Q;
InitQueue(Q);
Queueptr p=waitway.front;
do{ enQueue(Q,p->data); //copy它前面的车辆情况
p=p->next;
}while(p->next!=q);
p=waitway.front;
Queueptr M=q->next;
do{ p->data=M->data;
p=p->next;
M=M->next;
}while(M);
cout<<" 便道上为该车让路的车辆顺序: ";
cout<<" __________________________";
Queueptr N=Q.front;
do{ cout<<" "<<N->data.num;
p->data=N->data;
p=p->next;
N=N->next;
}while(N!=Q.rear);
destroyQueue(Q);
delete(waitway.rear);
waitway.rear=p;
}
}
else cout<<" 不必让路,该车可直接从便道开走!"<<endl;
}
end:;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -