您好,欢迎访问三七文档
当前位置:首页 > 财经/贸易 > 资产评估/会计 > 数据结构(C语言版)停车场管理(完美版)
#includestdio.h#includestdlib.h#includemalloc.h#defineOK1#defineOVERFLOW-2#defineSIZE2//Í£³µ³¡Î»ÖÃÊýtypedefintStatus;typedefstructCar1{intnumber;//³µºÅintar_time;//µ½´ïʱ¼ä}CarNode;typedefstruct{CarNode*base;//¶ÑÕ»µ×CarNode*top;//¶ÑÕ»¶¥intstacksize;}Park;typedefstructCar2{intnumber;//³µºÅintar_time;//µ½´ïʱ¼ästructCar2*next;}*CarPtr;typedefstruct{//±ãµÀCarPtrfront;//±ãµÀµÄ¶ÓÁеĶÔÍ·CarPtrrear;//±ãµÀµÄ¶ÓÁеĶÓβintlength;}Shortcut;StatusInitStack(Park&P){//³õʼ»¯Í£³µ³¡P.base=(CarNode*)malloc(SIZE*sizeof(Car1));if(!P.base)exit(OVERFLOW);P.top=P.base;P.stacksize=0;returnOK;}StatusPush(Park&P,CarNodee){//³µ½øÈëÍ£³µ³¡*P.top++=e;++P.stacksize;returnOK;}StatusPop(Park&P,CarNode&e){//³µÀ뿪ͣ³µ³¡if(P.top==P.base)printf(Í£³µ³¡Îª¿Õ);else{e=*--P.top;--P.stacksize;}returnOK;}StatusInitQueue(Shortcut&S){//³õʼ»¯±ãµÀS.front=S.rear=(CarPtr)malloc(sizeof(Car2));if(!S.front||!S.rear)exit(OVERFLOW);S.front-next=NULL;S.length=0;returnOK;}StatusEnQueue(Shortcut&S,intnumber,intar_time){//³µ½øÈë±ãµÀCarPtrp;p=(CarPtr)malloc(sizeof(Car2));if(!p)exit(OVERFLOW);p-number=number;p-ar_time=ar_time;p-next=NULL;S.rear-next=p;S.rear=p;++S.length;returnOK;}StatusDeQueue(Shortcut&S,CarPtr&w){//³µÀ뿪±ãµÀif(S.length==0)printf(ͨµÀΪ¿Õ);else{w=S.front-next;S.front-next=w-next;--S.length;}returnOK;}//¶Ô½øÕ»³µÁ¾µÄ´¦ÀíStatusArrival(Park&P,Shortcut&S){intnumber,ar_time;printf(ÇëÊäÈë³µÅƺţº);scanf(%d,&number);printf(ÇëÊäÈë½øÍ£³µ³¡µÄʱ¿Ì£º);scanf(%d,&ar_time);if(P.stacksizeSIZE){CarNodec;c.number=number;c.ar_time=ar_time;Push(P,c);printf(¸Ã³µÍ£ÔÚµÚ%dºÅ³µµÀ\n,P.stacksize);}else{EnQueue(S,number,ar_time);printf(Í£³µ³¡ÒÑÂú£¬ÔÝʱͣÔÚ±ãµÀµÄµÚ%d¸öλÖá£\n,S.length);}returnOK;}//¶ÔÀëÕ»³µÁ¾µÄ´¦ÀíStatusLeave(Park&P,Park&P1,Shortcut&S){intnumber,le_time,flag=1,money,ar_time;printf(ÇëÊäÈë³µÅƺÅ:);scanf(%d,&number);printf(ÇëÊäÈëÀ뿪ͣ³µ³¡µÄʱ¿Ì);scanf(%d,&le_time);CarNodee,m;CarPtrw;while(P.stacksize){Pop(P,e);if(e.number==number){flag=0;money=(le_time-e.ar_time)*2;ar_time=e.ar_time;break;}Push(P1,e);}while(P1.stacksize){Pop(P1,e);Push(P,e);}//³µ´ÓÍ£³µ³¡Öгöif(flag==0){if(S.length!=0){DeQueue(S,w);m.ar_time=le_time;m.number=w-number;Push(P,m);free(w);printf(³µÅƺÅΪ%dµÄ³µÒÑÓɱãµÀ½øÈëÍ£³µ³¡\n,m.number);}printf(Í£³µ·ÑΪ%d£¬Õ¼ÓóµÎ»ÊýΪ%d\n,money,P.stacksize);}else{printf(Í£³µ³¡²»´æÔÚ³µÅƺÅΪ%dµÄ³µ\n,number);}returnOK;}intmain(){intm=1;charflag;//Ñ¡ÏîParkP,Q;ShortcutS;InitStack(P);InitStack(Q);InitQueue(S);while(m){printf(AÆû³µ½ø³µ³¡\n);printf(DÆû³µ³ö³µ³¡\n);printf(EÍ˳ö³ÌÐò\n);printf(ÇëÑ¡Ôñ(A,D,E):);scanf(%c,&flag);switch(flag){case'A':case'a':Arrival(P,S);break;//³µ½øÈëÍ£³µ³¡case'D':case'd':Leave(P,Q,S);break;//³µÀ뿪ͣ³µ³¡case'E':case'e':m=0;break;default:printf(Inputerror!\n);break;}while(flag!='\n')scanf(%c,&flag);}return0;}
本文标题:数据结构(C语言版)停车场管理(完美版)
链接地址:https://www.777doc.com/doc-3397824 .html