1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
| #include<stdio.h> #include<stdlib.h> #include<malloc.h> typedef int Status;
typedef struct Car1{ int number; int arrive_time; }CarNode;
typedef struct{ CarNode *ebp; CarNode *esp; int stacksize; }Park;
typedef struct Car2{ int number,arrive_time; struct Car2 *next; }*CarPtr;
typedef struct{ CarPtr front; CarPtr rear; int length; }Shortcut;
int SInit(Park &P){ P.ebp=(CarNode*)malloc(5*sizeof(Car1)); if(!P.ebp) exit(-2); P.esp=P.ebp; P.stacksize=0; return 1; }
int Push(Park &P,CarNode e){ *P.esp++=e; ++P.stacksize; return 1; }
int Pop(Park &P,CarNode &e){ if(P.esp==P.ebp) printf("Th3 p@rk1ng lot 1s 3mpty!\n"); else{ e=*--P.esp; P.stacksize--; } return 1; }
int InitQueue(Shortcut &S){ S.front=S.rear=(CarPtr)malloc(sizeof(Car2)); if(!S.front||!S.rear) exit(-2); S.front->next=NULL; S.length=0; return 1; }
int EnQueue(Shortcut &S,int number,int arrive_time){ CarPtr p; p=(CarPtr)malloc(sizeof(Car2)); if(!p) exit(-2); p->number=number; p->arrive_time=arrive_time; p->next=NULL; S.rear->next=p; S.rear=p; ++S.length; return 1; }
int DeQueue(Shortcut &S,CarPtr &w){ if(S.length == 0) printf("Th3 s1d3w@y 1s 3mpty!\n"); else{ w = S.front->next; S.front->next=S.front->next->next; --S.length; } return 1; }
int Arrival(Park &P,Shortcut &S){ int number,arrive_time; printf("请输入车牌号:"); scanf("%d",&number); printf("请输入进场时刻:"); scanf("%d",&arrive_time); if(P.stacksize<5){ CarNode c; c.number=number; c.arrive_time=arrive_time; Push(P,c); printf("该车停在%d号车位.\n",P.stacksize); } else{ EnQueue(S,number,arrive_time); printf("停车场已满,停在便道的第%d个位置.\n",S.length); } return 1; }
int Leave(Park &P,Park &P1,Shortcut &S){ int number,le_time,flag=1,money,arrive_time; printf("请输入车牌号:"); scanf("%d",&number); printf("请输入离开时刻:"); scanf("%d",&le_time); CarNode e,m; CarPtr w; while(P.stacksize){ Pop(P,e); if(e.number==number){ flag=0; money=(le_time-e.arrive_time)*2; arrive_time=e.arrive_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.arrive_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); } return 1; }
int main(){ int m=1; char flag; Park P,Q; Shortcut S; SInit(P); SInit(Q); InitQueue(S); while(m){ printf("请选择(A(arrive),D(depature),E(end)): "); 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("3rr0r!Please 1nput ag@1n\n"); break; } while (flag != '\n') scanf("%c",&flag); } }
|