- #include<iostream>
- #include<string>
- #define stack_init_size 2
- //#define stackincrement 10
- using namespace std;
- struct stack//栈
- {
- char *base;
- char *top;
- int size;
- };
- struct queue//队列
- {
- char number[20];
- queue *next;
- };
- queue *head=NULL;
- queue *tail=NULL;
- queue *vernier=NULL;
- void creatQueue()//创建队列
- {
- head=tail=vernier=new queue;
- cout<<"输入车的车牌号:"<<endl;
- cin>>tail->number;
- tail->next=NULL;
- }
- void addQueue()//新驶入的车
- {
- tail->next=new queue;
- vernier=tail;
- tail=vernier->next;
- cout<<"输入车的车牌号:"<<endl;
- cin>>tail->number;
- tail->next=NULL;
- }
- void delQueue()//一辆车进入车站
- {
- vernier=head->next;
- head->next=NULL;
- //去掉头
- head=vernier;
- }
- int position(char e[20])//在字符数组中找某一字符的位置
- {
- int i=0;
- //int len=strlen(e);
- //为什么 int len=strlen(e)不行?
- for(i=0;i<strlen(e);i++)
- {
- if(e[i]==',')
- {
- return i;
- break;
- }
- }
- }
- void cut(char a[20],const char b[20],int c,int d)//在字符数组b中的位置c,d之间截取一段存于a
- {
- int i;
- int len1=strlen(b);
- if(d>len1||c>d)
- {
- cout<<"信息有误";
- }
- else
- {
- for(i=c;i<d;i++)
- {
- a[i-c]=b[i];
- }
- }
- }
- bool isEmpty(stack s)//判断是否为空
- {
- if(s.base!=s.top) return false;
- else return true;
- }
- bool isFull(stack s)//判断是否满
- {
- if(s.top-s.base>=s.size) return true;
- else return false;
- }
- bool isMateTwo(char a[20],char b[20])//比较两个字符数组的前两位返回bool
- {
- int i=0;
- for(i=0;i<2;i++)
- {
- if(a[i]!=b[i])
- {
- return false;
- break;
- }
- return true;
- }
- }
- void creatNewStack(stack &s)//创建新栈
- {
- s.base=(char *)malloc(stack_init_size * sizeof(char));
- if(!s.base)exit(OVERFLOW);//s.base=null时
- s.top=s.base;
- s.size=stack_init_size;
- }
- void push(stack &s,char e[20])//添加新项(开入新车)
- {
- *s.top=e[20];
- s.top++;
- }
- void pop(stack &s1,stack &s2)//删除项(s1中栈顶取出并且存在s2中)
- {
- char e[20]="";
- if(s1.top==s1.base)
- {
- cout<<"错误!!"<<endl;
- return ;
- }
- e[20] =*--s1.top;
- push(s2,e);
- /*
- s2.top=s1.top;
- s2.top++;
- s1.top--;
- */
- }
- void main()
- {
- stack s1,s2;
- char e[20]="",e1[20]="",e2[20]="",e3[20]="",e4[20]="",e5[20]="";//赋初值
- int i=0,k=0,h=0,j=0,t=0;
- /*int n;
- cout<<"输入车站所能容纳的车数量:";
- cin>>n;*/
- //创建新站
- creatNewStack(s1);//车站内
- creatNewStack(s2);//车站内的车出来时后面的车停放
- //输入汽车进站还是出站
- while(1)
- {
- char in_out;
- int num,time1,time2;
- cout<<"输入汽车进出信息(“1”表示到站,“2”表示出站,“3”表示输入结束):";
- cin>>in_out;
- if(in_out=='1')//输入到站执行
- {
- //先判断车站是否满
- if(isFull(s1))
- {
- cout<<"车站已满,请在便道上等候"<<endl;
- //进行便道上队列的排放
- if(head==NULL)//先判断便道上有没有车
- {
- creatQueue();
- }
- else
- {
- addQueue();
- }
- }
- else
- {
- cout<<"输入到站车辆的车牌号与到站时间(车牌号为两位,中间以逗号隔开):";
- cin>>e;//以字符数组的形式存储汽车的信息
- //执行添加项函数
- int len2=strlen(e);
- k=position(e);
- cut(e1,e,0,k);//子数组来表示车牌号
- if(strlen(e1)>2)
- {
- cout<<"车牌号为两位!"<<endl;
- return ;
- }
- cut(e2,e,k+1,len2);//子数组来表示到站时间
- //将字符型数组转换为整数
- num=atoi(e1);
- time1=atoi(e2);
- cout<<"车牌号码:"<<num<<" "<<"到站时间:"<<time1<<endl;
- push(s1,e);
- h++;
- }
- }
- else if(in_out=='2')//输入出站执行
- {
- if(isEmpty(s1))//车站空则没车能开出
- {
- cout<<"车站空!"<<endl;
- return;
- }
- cout<<"输入出站车辆的车牌号码与出站时间(车牌号为两位,中间以逗号隔开):";
- cin>>e;//以字符数组的形式存储汽车的信息
- //执行删除项函数
- int len2=strlen(e);
- k=position(e);
- cut(e1,e,0,k);//子数组来表示车牌号
- if(strlen(e1)>2)
- {
- cout<<"车牌号为两位!"<<endl;
- return ;
- }
- cut(e2,e,k+1,len2);//子数组来表示到站时间
- //将字符型数组转换为整数
- num=atoi(e1);
- time2=atoi(e2);
- //cout<<"车牌号码:"<<num<<" "<<"出站时间:"<<time2<<endl;
- t=h;
- for(i=0;i<h;i++)//将一个栈中的数据有序移到另外一个栈
- {
- e3[20]=*s1.top;
- if(!isMateTwo(e1,e3))
- {
- pop(s1,s2);
- j++;
- }
- else
- {
- cout<<"出站车辆的车牌号码与出站时间为:"<<e3<<endl;
- e3[20]=NULL;//去掉选中的值
- s1.top--;
- cout<<"收取费用(一单位时间一元):"<<(time2-time1)*1<<"元"<<endl;
- break;
- }
- t--;
- }
- h=t;
- t=j;
- for(i=0;i<j;i++)
- {
- pop(s2,s1);
- h++;
- t--;
- }
- j=t;
- //便道上第一辆车开进
- cout<<"便道上车牌号为"<<head->number<<"的车开进车站"<<endl;
- strcat(head->number,",");
- strcat(head->number,e2);
- push(s1,head->number);
- delQueue();//删去便道中该车牌号的车
- }
- else if(in_out=='3')
- {
- cout<<"结束输入";
- break;
- }
- else
- {
- cout<<"输入有误!请重新输入";
- }
- }
- system("pause");
- }
- //该片段来自于http://www.codesnippet.cn/detail/241020136661.html
来源: http://www.codesnippet.cn/detail/241020136661.html