1、问题描述:设停车场内只有一个可停放n辆汽车的狭长通道,且只有一个大门可以供汽车进出。汽车在停车场内按照车辆到达的先后顺序,依次由北向南排列,大门在最南段。若停车场内车位已经停满,则后来的车位只能停在过道上等候。一旦停车场内有车离开,停靠在过道上的车辆就能开进停车场停放。根据每辆车在停车场的停靠时间计费。请设计一个停车场管理系统。实验代码如下:#include #include #include #include #include #include #include using namespace std;#define MAXNUM 5#define PRICE 2.0struct carch
2、ar num10;struct tm intime;struct tm outime;double expense;int length;int position;/the stack of car/typedef structcar carlistMAXNUM;int top;Seqstack;void Stackinit(Seqstack * s)s-top=-1;int Isempty(Seqstack * s)if(s-top=-1)return 1;elsereturn 0;int Isfull(Seqstack *s)if(s-top=MAXNUM-1)return 1;elser
3、eturn 0;void Stackpush(Seqstack *s,car car1)if(! Isfull(s)s-top+;s-carlists-top=car1;elsecoutits full nowcarlists-top;s-top-;return car1;car Stackgettop(Seqstack *s)car car1;if(! Isempty(s)car1=s-carlists-top;return car1;/the queue of car int tunnel/struct carnodecar data;struct carnode *next;struct
4、 carnodequeuecarnode *head;carnode *rear;void carnodequeueinit(carnodequeue *q)if(!(q-head = (carnode *)malloc(sizeof(carnode)coutfailed to malloc!rear=q-head;q-head-next=NULL;q-rear-next=NULL;int carnodequeueisempty(carnodequeue *q)if(q-rear=q-head)return 1;elsereturn 0;void carnodequeuein(carnodeq
5、ueue *q,car cc)carnode *p;if(!(p=(carnode *)malloc(sizeof(carnode)coutfailed to malloc!data=cc;p-next=NULL;q-rear-next=p;q-rear=p;car carnodequeueout(carnodequeue *q)carnode *p;car cc;if(q-head != q-rear)p=q-head-next;if(p-next=NULL)cc=p-data;q-rear=q-head;free(p);elseq-rear-next=p-next;cc=p-data;fr
6、ee(p);return cc;/ the separator of cout /void separator(int n,char ch,char newline)for(int i=0;in;i+)coutch;if(newline = 1)coutendl;/ print the time struct /void printdata(struct tm gm_data)coutgm_data.tm_mon/gm_data.tm_mday gm_data.tm_hour+8:gm_data.tm_min:gm_data.tm_secendl;/ show park /void showp
7、ark(Seqstack *s)struct tm gm;coutthe park information is follow:endl;separator(40,-,1);if(Isempty(s)coutthe park is empty !endl;elsecoutposition number intime endl;for(int i=0;itop;i+)coutcarlisti.position carlisti.numcarlisti.intime);cout the total is top+1 cars.top = MAXNUM-1)coutand the park is f
8、ull now!endl;elsecoutand the park have top position is empty !head-next;cout the ailse information is follow: endl;separator(30,-,1);coutnumber the intime endl;while(p != NULL)coutdata.numdata.intime);p=p-next;elsecoutthe ailse is empty !endl;separator(30,-,1);coutendl;/ show park and ailse /void sh
9、owall(Seqstack *s,carnodequeue *q)showpark(s);showaisle(q);/ in park /void Inpark(Seqstack *s,carnodequeue *q)car cc;struct tm *gm_data;time_t seconds;time(&seconds); /get the timegm_data=gmtime(&seconds);coutthe number of car cc.num;cc.intime=*gm_data;if(!Isfull(s) & carnodequeueisempty(q)cc.positi
10、on=(s-top)+2;Stackpush(s,cc);showpark(s);else if(Isfull(s) | !carnodequeueisempty(q)coutthe park if full,the car can only park int the ailse .endl;cc.position=MAXNUM;carnodequeuein(q,cc);showall(s,q);/ the out park /void Outpark(Seqstack *s,carnodequeue *q)struct tm *gm_data;time_t seconds;Seqstack
11、p;char nowtime10;int i,pos;car cc;Stackinit(&p);if(Isempty(s)coutthe park is empty,no car need to leave .endl;exit(0);elsecoutthe park information is follow.endl;showpark(s);coutwhich one need to leave ?pos;if(pos 0 & postop+1)for(i=s-top+1;ipos;i-)cc=Stackpop(s);cc.position=cc.position-1;Stackpush(
12、&p,cc);cc=Stackpop(s);time(&seconds);gm_data = gmtime(&seconds);cc.outime=*gm_data;cc.length=mktime(&cc.outime)-mktime(&cc.intime);cc.expense=(cc.length/3600 + 1)*PRICE;coutthe information which car is to leave is follow.endl;coutcc.num cc.length cc.expenseendl;while(! Isempty(&p)cc=Stackpop(&p);Sta
13、ckpush(s,cc);while(!Isempty(s) & !carnodequeueisempty(q)cc=carnodequeueout(q);time(&seconds);gm_data=gmtime(&seconds);cc.intime=*gm_data;Stackpush(s,cc);elsecoutthe car position is wrong!endl;主函数调用为:#include stdafx.h#include example34_car.hint main()Seqstack Park;carnodequeue Aisle;Stackinit(&Park);
14、carnodequeueinit(&Aisle);char choice;do coutendlendl;separator(10,-,0);coutthe management of parkendl;separator(30,-,1);cout1. the car in endl;cout2. the car out endl;cout3. watch the park information endl;cout0. exitendl;separator(56,-,1);coutthe total is MAXNUM position and can park in the ailse if park is full .please choicechoice;switch(choice)case 1:Inpark(&Park,&Aisle);break;case 2:Outpark(&Park,&Aisle);break;case 3:showall(&Park,&Aisle);break; while (choice != 0);return 0;实验结果展示:
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2