📄 停车场.cpp
字号:
#include<iostream.h>
#include<string.h>
#include<iomanip.h>
#define StackSize 8
typedef struct
{char Car_number[StackSize][10];
int top;
}SqStack;//定义栈的结构
void InitStack(SqStack &S)//初始栈的栈顶
{S.top=-1;
}
int StackEmpty(SqStack &S)//判断栈是否为空
{if(S.top==-1)
return 1;
else
return 0;
}
int StackFull(SqStack &S)//判断栈是否为满
{if(S.top==StackSize-1)
return 1;
else
return 0;
}
void StackPush(SqStack &S,char e[])//压栈
{S.top++;
strcpy(S.Car_number[S.top],e);
}
void StackDisplay(SqStack &S)//显示栈的元素
{cout<<"停车场的车:"<<endl;
int i;
for(i=S.top+1;i<StackSize;i++)
cout<<"|"<<setiosflags(ios::left)<<setw(10)<<""<<"|"<<endl;
for(i=S.top;i>=0;i--)
cout<<"|"<<setiosflags(ios::left)<<setw(10)<<S.Car_number[i]<<"|"<<endl;
cout<<"------------"<<endl;
}
void StackPop(SqStack &S,char e[])//将特定值元素弹出
{int flag=0;int k;
if(StackEmpty(S))
cout<<e<<"号码的车找不到!"<<endl;
else
{for(int i=0;i<=S.top&&!flag;i++)
if(!strcmp(S.Car_number[i],e))
{ k=i;flag=1;}
if(!flag)
cout<<e<<"号码的车找不到!"<<endl;
else
{for(i=k;i<S.top;i++)
strcpy(S.Car_number[i],S.Car_number[i+1]);
S.top--;
}
}
}
#define QueueSize 9
typedef struct
{char Car_number[QueueSize][10];
int front ,rear;
}SQueue;//定义队列的结构
void InitQueue(SQueue &SQ)//初始队列
{SQ.front=SQ.rear=0;
}
int QueueEmpty(SQueue &SQ)//判断队列是否为空
{if(SQ.front==SQ.rear)
return 1;
else
return 0;
}
int QueueFull(SQueue &SQ)//判断队列是否为满
{if((SQ.rear+1)%QueueSize==SQ.front)
return 1;
else
return 0;
}
void EnQueue(SQueue &SQ,char e[])//元素进队尾
{SQ.rear=(SQ.rear+1)%QueueSize;
strcpy(SQ.Car_number[SQ.rear],e);
}
void OutQueue(SQueue &SQ,char e[])//队首出队列
{ SQ.front=(SQ.front+1)%QueueSize;
strcpy(e,SQ.Car_number[SQ.front]);
}
void QueueDisplay(SQueue &SQ)//显示队列中的元素
{cout<<"便道上的车:\n";
for(int i=(SQ.front+1)%QueueSize;i!=(SQ.rear+1)%QueueSize;i=(i+1)%QueueSize)
cout<<SQ.Car_number[i]<<"<---";
cout<<endl;
}
void Special_OutQueue(SQueue &SQ,char e[])//特定元素出队列
{int k,flag=0,j;
if(QueueEmpty(SQ))
cout<<e<<"号码的车找不到!"<<endl;
else
{for(k=(SQ.front+1)%QueueSize;k!=(SQ.rear+1)%QueueSize&&!flag;k=(k+1)%QueueSize)
if(!strcmp(SQ.Car_number[k],e))
{ flag=1; }
if(!flag)
cout<<e<<"号码的车找不到!"<<endl;
else
{k--;
for(j=k;j!=SQ.rear;j=(j+1)%QueueSize)
strcpy(SQ.Car_number[j],SQ.Car_number[(j+1)%QueueSize]);
SQ.rear=(SQ.rear-1)%QueueSize;
}
}
}
void Car_In(SqStack &S,SQueue &SQ)//模拟进车
{char e[10];
cout<<"please input the number of the car which is coming!\n";
cin>>e;
if(!StackFull(S)&&QueueEmpty(SQ))
StackPush(S, e);
else if(StackFull(S)&&!QueueFull(SQ))
EnQueue(SQ,e);
else if(StackFull(S)&&QueueFull(SQ))
cout<<"The stack and the queue are full,please come later!\n ";
}
void Car_Reset(SqStack &S,SQueue &SQ)//自动调整
{ char e[10];
while(!StackFull(S)&&!QueueEmpty(SQ))
{OutQueue(SQ, e);
StackPush(S,e);
}
}
void Car_Out(SqStack &S,SQueue &SQ)//模拟出车
{int choice;char number[10];
cout<<"1:停车场的有车要离开\n2:便道上有车要离开\n";
cin>>choice;
switch(choice)
{case 1:
cout<<"input the car number which is leaving!\n";
cin>>number;
StackPop(S,number);
break;
case 2:
cout<<"input the car number which is leaving!\n ";
cin>>number;
Special_OutQueue(SQ,number);
break;
default:
cout<<"error ";break;
}
Car_Reset(S,SQ);
}
void re_start(SqStack &S,SQueue &SQ)//使停车场和便道车为空
{S.top=-1;
SQ.front=SQ.rear=0;
}
void display_all(SqStack &S,SQueue &SQ)//显示停车场和便道车的情况
{QueueDisplay(SQ);
StackDisplay(S);
}
void di()
{cout<<"\t\t********************************************\n";
cout<<"\t\t1:模拟进车 3:显示停车场和便道车的情况 \n";
cout<<"\t\t2:模拟出车 4:重新开始(停车场和便道车为空) \n";
cout<<"\t\t5:退出\n";
cout<<"\t\t********************************************\n";
}
void main()
{int m;
SqStack S;
InitStack(S);
SQueue SQ;
InitQueue(SQ);
cout<<"\t\t***************************************************\n";
cout<<" \t\t *请勿盗版*\t *请勿盗版*\t *请勿盗版*\n ";
cout<<"\t\t\t\t产品信息:\n\t\t\t\t学号:020410107\n\t\t\t\t姓名:胡小勇\n\t\t\t\t电子邮件:huxiaoyong1983@163.com\n\t\t\t\t电话:0631-5696421\n\t\t\t\tQQ:43808128\n\t\t\t\t地址:哈尔滨工业大学\n";
cout<<" \t\t\t\t使用有问题 请与我联系\n\t\t *请勿盗版*\t *请勿盗版*\t *请勿盗版*\n ";
cout<<"\t\t***************************************************\n";
cout<<"\t\t***************************************************\n";
cout<<"\t\t\t 停车场模拟系统\n";
cout<<"\t\t\t\t\t时间:2004/5/21\n" ;
cout<<"\t\t***************************************************\n";
di();
cin>>m;
while(m!=5)
{switch(m)
{case 1:Car_In(S,SQ);break;
case 2:Car_Out(S,SQ);break;
case 3: display_all(S,SQ);break;
case 4:re_start(S,SQ);break;
default:
cout<<"why do you input the wrong number!\n";
}
di();
cin>>m;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -