标签:acm
///设定停车场有(Size)个车位 ///有趣
///同学们要是参考的话麻烦改一下 这个是要交作业的 如有雷同 如何是好? #include <bits/stdc++.h> using namespace std; int n; int Size = 5; typedef struct Car { int num; time_t time; int price; } CarNode; typedef struct QNode { CarNode data; struct QNode *next; } QNode , *QueuePtr; typedef struct { QueuePtr front; QueuePtr rear; } LinkQueue;///注意这里队列的使用方法 CarNode car[5]; void initcar() { for(int i=1; i<=n; i++) { cout<<"请输入第"<<i<<"辆车的车牌号"<<endl; cin>>car[i].num; car[i].time = time(NULL); car[i].price = 0; } } void InitQueue(LinkQueue *Q) { Q->front = Q->rear = (QueuePtr)malloc(sizeof(QNode)); if(!Q->front) exit(0); Q->front->next = NULL; } void EnQueue(LinkQueue *Q, CarNode e) { QueuePtr p; p = (QueuePtr)malloc(sizeof(QNode)); if(!p) exit(0); p->data = e; p->next = NULL; Q->rear->next = p; Q->rear = p; } void Pop(LinkQueue *Q, CarNode *e) { if(Q->front == Q->rear) return; QNode *p; p = Q->front->next; *e = p->data; Q->front->next = p->next; if(Q->rear == p) Q->rear = Q->front; free(p); } bool QEmpty(LinkQueue *Q) { return Q->front == Q->rear; } void carin(LinkQueue *Q) { if(n < Size) { cout<<"请输入车牌信息:"<<endl; cin>>car[++n].num; car[n].time = time(NULL); } else { CarNode e; cout<<"请输入车牌信息:"<<endl; cin>>e.num; e.price = 0; cout<<"抱歉,停车场车位已满,请您耐心等待"<<endl; EnQueue(Q, e); } } void carout(LinkQueue *Q) { int Num; cout<<"请输入车牌信息:"<<endl; cin>>Num; int i; for(i=1; i<=n; i++) if(car[i].num == Num) break; int Time = time(NULL) - car[i].time; cout<<"该车停车期间共需缴纳停车费"<<Time<<"元"<<endl; car[i].num = -1; Size++; while(n<Size && !QEmpty(Q)) { CarNode e; Pop(Q, &e); car[++n] = e; } } void display() { for(int i=1; i<=n; i++) { if(car[i].num != -1) printf("车牌:%d\n花费:%d\n",car[i].num, car[i].price); } cout<<n<<endl; } void Qdisplay(LinkQueue *Q) { QueuePtr p = Q->front->next; while(p) { cout<<p->data.num<<" "; p = p->next; } cout<<endl; } void work(LinkQueue *Q) { int a; while(cin>>a) { if(!a) return; else switch(a) { case 1: carin(Q); break; case 2: carout(Q); break; case 3: display(); break; case 4: Qdisplay(Q); break; } cout<<"1:车辆进入\n2:车辆离开\n3:显示停车场内车辆信息\n4:显示等待进入的车辆\n0:退出系统\n请选择操作:\n"; } } int main() { LinkQueue Q; InitQueue(&Q); cout<<"请输入停车场最初的车辆数目(温馨提示:输入数目不能大于"<<Size<<"):"<<endl; cin>>n; cout<<"请依次输入车辆车牌信息:"<<endl; initcar(); display(); cout<<"现在还有"<<Size - n<<"个停车位可以使用"<<endl; cout<<"有如下操作:\n1:车辆进入\n2:车辆离开\n3:显示停车场内车辆信息\n0:退出系统\n请选择操作:\n"; work(&Q); return 0; }
标签:acm
原文地址:http://blog.csdn.net/dojintian/article/details/44873653