停车场模拟
2015-12-14 18:53
127 查看
#include <stdio.h> #include <malloc.h> #define N 3 #define M 4 #define Price 2 typedef struct { int CarNo ; int CarTime ; int top; } SqStack; typedef struct { int CarNo[M]; int front,rear; } SqQueue; void InitStack(SqStack *&s) { s=(SqStack *)malloc(sizeof(SqStack)); s->top=-1; } int StackEmpty(SqStack *s) { return(s->top==-1); } int StackFull(SqStack *s) { return(s->top==N-1); } int Push(SqStack *&s,int e1,int e2) { if (s->top==N-1) return 0; s->top++; s->CarNo[s->top]=e1; s->CarTime[s->top]=e2; return 1; } int Pop(SqStack *&s,int &e1,int &e2) { if (s->top==-1) return 0; e1=s->CarNo[s->top]; e2=s->CarTime[s->top]; s->top--; return 1; } void DispStack(SqStack *s) { int i; for (i=s->top; i>=0; i--) printf("%d ",s->CarNo[i]); printf("\n"); } void InitQueue(SqQueue *&q) { q=(SqQueue *)malloc (sizeof(SqQueue)); q->front=q->rear=0; } int QueueEmpty(SqQueue *q) { return(q->front==q->rear); } int QueueFull(SqQueue *q) { return ((q->rear+1)%M==q->front); } int enQueue(SqQueue *&q,int e) { if ((q->rear+1)%M==q->front) return 0; q->rear=(q->rear+1)%M; q->CarNo[q->rear]=e; return 1; } int deQueue(SqQueue *&q,int &e) { if (q->front==q->rear) return 0; q->front=(q->front+1)%M; e=q->CarNo[q->front]; return 1; } void DispQueue(SqQueue *q) { int i; i=(q->front+1)%M; printf("%d ",q->CarNo[i]); while ((q->rear-i+M)%M>0) { i=(i+1)%M; printf("%d ",q->CarNo[i]); } printf("\n"); } int main() { int comm; int no,e1,time,e2; int i,j; SqStack *St,*St1; SqQueue *Qu; InitStack(St); InitStack(St1); InitQueue(Qu); do { printf("输入指令(1:到达 2:离开 3:显示停车场 4:显示候车场 0:退出):"); scanf("%d",&comm); switch(comm) { case 1: /*汽车到达*/ printf("输入车号和时间(设车号和时间均为整数): "); scanf("%d%d",&no,&time); if (!StackFull(St)) { Push(St,no,time); printf(" >>停车场位置:%d\n",St->top+1); } else { if (!QueueFull(Qu)) { enQueue(Qu,no); printf(" >>候车场位置:%d\n",Qu->rear); } else printf(" >>候车场已满,不能停车\n"); } break; case 2: /*汽车离开*/ printf("输入车号和时间(设车号和时间均为整数): "); scanf("%d%d",&no,&time); for (i=0; i<=St->top && St->CarNo[i]!=no; i++); if (i>St->top) printf(" >>未找到该编号的汽车\n"); else { for (j=i; j<=St->top; j++) { Pop(St,e1,e2); Push(St1,e1,e2); } Pop(St,e1,e2); printf(" >>%d汽车停车费用:%d\n",no,(time-e2)*Price); while (!StackEmpty(St1)) { Pop(St1,e1,e2); Push(St,e1,e2); } if (!QueueEmpty(Qu)) { deQueue(Qu,e1); Push(St,e1,time); } } break; case 3: /*显示停车场情况*/ if (!StackEmpty(St)) { printf(" >>停车场中的车辆:"); DispStack(St); } else printf(" >>停车场中无车辆\n"); break; case 4: /*显示候车场情况*/ if (!QueueEmpty(Qu)) { printf(" >>候车场中的车辆:"); DispQueue(Qu); } else printf(" >>候车场中无车辆\n"); break; case 0: /*结束*/ if (!StackEmpty(St)) { printf(" >>停车场中的车辆:"); DispStack(St); } if (!QueueEmpty(Qu)) { printf(" >>候车场中的车辆:"); DispQueue(Qu); } break; default: printf(" >>输入的命令错误\n"); break; } } while(comm!=0); return 0; }
相关文章推荐
- 一步一步教你刷【极客学院VIP会员】(二)之抓包分析
- LTE物理层几个基本概念的定义和相互关系 http://blog.sina.com.cn/s/blog_696122b50100n6df.html
- 【设计模式】——工厂比较
- c语言中的字节对齐
- Apache Commons 工具集使用简介
- 文件写入HDFS的流程
- 【LEETCODE】290-Word Pattern
- 全志A20音频模块的调试
- Fiddler显示IP的设置方法
- 解读机器学习基础概念:VC维的来龙去脉
- [HTML5]如何使用移动设备的方向定位器
- 在手机相册(ios设备相册)中创建相册
- IOS开发之NSDictionary里setValue:forKey与setObject:forKey、objectForKey与valueForKey的差异
- codeforces 601B 斜率+单调栈
- 限制Text-Area域中的字符的个数
- 2015.12.14 宏定义 枚举 动态内存分配
- 入侵指定网站的一些方法(思路篇)
- python基础一
- Git 远程服务器搭建
- CentOS 7.1安装GNOME,开启VNC Server