#include<iomanip.h>
#include<stdlib.h>
#include<string.h>
#define MAX 2
struct Time
{
int hour;
int min;
};
typedef struct Car
{
char num[10];
char kind;
Time arri;
} ElemType;
struct SeqStack
{
ElemType stack[MAX];
int top;
};
struct LNode
{
ElemType data;
LNode* next;
};
struct LinkQueue
{
LNode * f;
LNode * r;
};
int q=0;
void InitStack(SeqStack &S)
{
S.top=-1;
}
bool Puch(SeqStack&S,ElemType e)
{
if(S.top==MAX-1)
return false;
S.top++;
S.stack[S.top]=e;
return true;
}
bool Pop(SeqStack & S,ElemType & e)
{
if(S.top==-1)
return false;
e=S.stack[S.top];
S.top--;
return true;
}
void InitQueue(LinkQueue &Q)
{
Q.f=Q.r=new LNode;
if(Q.f==NULL)
{
cout<<"分配失败"<<endl;
exit(1);
}
Q.f->next=NULL;
}
void InQueue(LinkQueue &Q,ElemType e)
{
LNode *p;
p=new LNode;
if(p==NULL)
{
cout<<"分配失败"<<endl;
exit(1);
}
p->data=e;
p->next=NULL;
Q.r->next=p;
Q.r=p;
}
ElemType OutQueue(LinkQueue&Q)
{
if(Q.f==Q.r)
{
cout<<"空队,出队失败"<<endl;
exit(1);
}
LNode *p=Q.f->next;
ElemType temp=p->data;
Q.f->next=p->next;
if(Q.r==p)Q.r=Q.f;
delete p;
return temp;
}
void DestroyQueue(LinkQueue &Q)
{
while(Q.f!=NULL)
{
Q.r=Q.f->next;
delete Q.f;
Q.f=Q.r;
}
}
void kind(int k)
{
switch (k)
{
case'1':cout<<"小汽车";break;
case'2':cout<<"客车";break;
case'3':cout<<"卡车";break;
}
}
void mainmenu()
{
cout<<"\n-----------------------";
cout<<"\n 停车场管理系统 ";
cout<<"\n -------------------- ";
cout<<"\n 1. 汽车到达 ";
cout<<"\n 2. 汽车离开 ";
cout<<"\n 3. 信息查询 ";
cout<<"\n 4. 退出 ";
cout<<"\n 请选择(1.2.3.4):";
}
void Arrival(SeqStack & S,LinkQueue & Q)
{
ElemType X;
char ch[80];int len;
cout<<"请输入汽车牌照号码(不超过9个字符,如:苏 A123456):"<<endl;
while (1)
{
cin>>ch;
len=strlen(ch);
if(len<=90)
break;
else
cout<<"您输入错误,请重新输入!"<<endl;
}
strcpy(x.num,ch);
cout<<"汽车种类:"<<endl;
cout<<"1.小汽车"<<endl;
cout<<"2.客车"<<endl;
cout<<"3.卡车"<<endl;
cout<<"请选择汽车种类(1.2.3):";
while(1)
{
cin>>x.kind;
if(x.kind>='1' && x.kind<='3')break;
else
cout<<"\n 您输入错误!\n\n请选择汽车种类(1.2.3):";
}
cout<<"请输入到达时间:"<<endl;
cout<<"小时(0~23):";
while(1)
{
cin>>x.arr.hour;
if(x.arri.hour>=0 && x.arri.hour<=23)
break;
else
cout<<"请输入错误,小时(0~23):";
}
cout<<"分钟(0~59):";
while(1)
{
cin>>x.arri.min;
if(x.arri.min>=0 && x.arri.min<=59)
break;
else
cout<<"输入错误,分钟(0~59):";
}
if(Push(S,x))
cout<<x.num<<"在"<<S.top+1<<"号位"<<endl;
esle
{
cout<<"停车场内已满,停在便道上等待!"<<endl;
InQueue(Q,x);
q++;
cout<<x.num<<"在便道的"<<q<<"号位"<<endl;
system("pause");
}
void Departure(SeqStack &S,LinkQueue &Q)
{
if(S.top==-1)
{
cout<<"停车场内没有车辆!"<<endl;
system("pause");
return;
}
ElemType x,y;
char ch[80];int len;
cout<<"请输入汽车牌照号码(不超过9个字符,如苏A123456):"<<endl;
while(1)
{
cin>>ch;
len=strlen(ch);
if(len<=9)
break;
else
cout<<"您输入错误,请重新输入!"<<endl;
}
strcpy(x.num,ch);
int i=S.top;
while(i!=-1)
if(strcmp(S.stack[i].num,x.num))
i--;
else break;
if(i!=-1)
{
SeqStack S1;
InitStack(S1);
while(S.top!=i)
{
Pop(S,y);
Puch(S1,y);
}
int h,m,t;
double p;
cout<<"请输入离开时间:"<<endl;
cout<<"小时(0~23):";
while(1)
{
cin>>h;
if(h>=0 && h<=23)
break;
else
cout<<"输入错误,小时(0~23):";
}
if(h<S.stack[i].arri.hour)
h+=24;
cout<<"分钟(0~59):";
while(1)
{
cin>>m;
if(m>=0 && m<=59)
break;
else
cout<<"输入错误,分钟(0~9):";
}
int h1=S.stack[S.top].arri.hour;
int m1=S.stack[S.top].arri.min;
t=h*60+m-h1*60-m1;
switch(S.stack[S.top].kind)
{
case'1':p=0.08;break;
case'2':p=0.15;break;
case'3':p=0.25;
}
cout<<S.stack[S.top].num;
kind(S.stack[S.top].kind;
cout<<"到达时间是"<<S.stack[S.top].arri.hour<<':'<<S.stack[S.top].arri.min<<endl;
cout<<"在停车场内共停留:"<<t<<"分钟!"<<endl;
cout<<"停车费为:"<<t*p<<"元。"<<endl;
Pop(S1,y)
Push(S,y)
cout<<y.num<<"现在位于"<<S.top+1<<"号位置"<<endl;
}
while(S.top<MAX-1)
{
if(q!=0)
{
y=OutQueue(Q);
q--;
cout<<y.num<<"已从便道进入停车场,位于";
cout<<S.top+1<<"号位置。"<<endl;
cout<<"请输入现在时间:"<<endl;
cout<<"小时(0~23):";
while(1)
{
cin>>S.stack[S.top].arri.hour;
int h=S.stack[S.top].arri.min;
if(h>=0 &&h<=23)
break;
else
cout<<"输入错误,小时(0~23):";
while(1)
{
cin>>s.stack[s.top].arri.min;
int m=s.stack[s.top].arri.min;
if(m>=0 && m<=59)
break;
else
cout<<"输入错误,分钟(0~59):";
}
}
else
{
if(! q && s.top==-!)
cout<<"停车场内已空!"<<endl;
break;
}
}
if(q!=0)
cout<<"便道上还有"<<q<<"辆车等待!"<<endl;
}
else
cout<<"没有此车辆!"<<endl;
system("pause");
}
voide infol(seqstack &s)
{
int top=0;
if(s.top!=-1)
{
cout<<"停车场内车辆为:"<<endl;
cout<<"汽车牌照号码 汽车种类 到达时间 位置号"<<endl;
while(top<=s.top)
{
cout<<setw(12)<<s.stack[top].num;
cout<<setw(10);
kind(s.stack[top].kind);
cout<<setw(7)<<s.stack[top].arri.hour<<':';
cout<<setw(2)<<s.stack[top].arri.min;
cout<<setw(8)<<++top<<endl;
}
}
else
cout<<"停车场内没有车辆!"<<endl;
}
void info2(linkqueue &q)
{
lnode *p=q.f->next;
int i=0;
if(p==NULL)
{
cout<<"便道上没有车辆!"<<endl;
return;
}
cout<<"便道上车辆为:"<<endl;
cout<<"汽车牌照号码 汽车种类 到达时间 位置号"<<endl;
while(p!=NULL)
{
cout<<setw(12)<<p->data.num;
cout<<setw(10);
kind(p->data.kind);
cout<<setw(7)<<p->data.arri.hour<<':';
cout<<setw(2)<<p->data.arri.min;
cout<<setw(8)<<++i<<endl;
p=p->next;
}
cout<<"便道上共有"<<q<<"辆汽车等待!"<<endl;
}
void info(seqstack &s,linkqueue &q)
{
char k;
while(1)
{
cout<<"\n----------------";
cout<<"\n| 信息查询 |";
cout<<"\nn| ---------- ";
cout<<"\n| 1.停车场 |";
cout<<"\n| 2.便道 |";
cout<<"\n| 3.返回 |";
cout<<"\n----------------";
cout<<"\n| 请选择(1.2.3):";
while(1)
{
cin>>k;
if(k>='1'&&k<='3')break;
else
cout<<"\n 您输入错误!\n\n请选择(1.2.3):";
}
swich(k)
{
case'1':infol(s);break;
case'2':info2(q);break;
case'3':return;
}
system("pause");
system("cls");
}
}
void main()
{
SeqStack S;
LinkQueue Q;
InitStack(S);
InitQueue(Q);
while(1)
{
mainmenu();
char k;
while(1)
{
cin>>k;
if(k>='1'&& k<='4') break;
else
cout<<"\n 您输入错误! \n\n请选择(1.2.3.4):";
}
swith(k)
{
case'1':cout<<"汽车到达"<<endl;
Arrival(S,Q);break;
case'2':cout<<"汽车离开"<<endl;
Departure(S,Q);break;
case'3':system("cls");
Info(S,Q);break;
case'4':cout<<"退出系统"<<endl;
exit(0);
}
system("cle");
}
DestroyQueue(Q);
}
有如下问题需解决: