第七周项目6 停车场
来源:互联网 发布:少儿电脑绘画软件 编辑:程序博客网 时间:2024/05/01 01:35
/* *Copyright (c) 2015,烟台大学计算机学院 *All rights reserved. *文件名称:tingchechang.cpp *作者:朱希康 *完成日期:2015年10月28日 *版本号:vc++6.0 * *问题描述:停车场 *输入描述:车牌号和停车时间 *程序输出:停车费用,停车场中听的车和候车场候车*/
#ifndef HEAD_H_INCLUDED#define HEAD_H_INCLUDED#include <stdio.h>#include <malloc.h>#define N 3 /*停车场内最多的停车数*/#define M 4 /*候车场内最多的停车数*/#define Price 2 /*每单位时间停车费用*/typedef struct{ int CarNo[N]; /*车牌号*/ int CarTime[N]; /*进场时间*/ int top; /*栈指针*/} SqStack; /*定义顺序栈类型,用于描述停车场*/typedef struct{ int CarNo[M]; /*车牌号*/ int front,rear; /*队首和队尾指针*/} SqQueue; /*定义循环队类型,用于描述候车场*/void InitStack(SqStack *&s);int StackEmpty(SqStack *s);int StackFull(SqStack *s);int Push(SqStack *&s,int e1,int e2);int Pop(SqStack *&s,int &e1,int &e2);void DispStack(SqStack *s);void InitQueue(SqQueue *&q);int QueueEmpty(SqQueue *q);int QueueFull(SqQueue *q);int enQueue(SqQueue *&q,int e);int deQueue(SqQueue *&q,int &e);void DispQueue(SqQueue *q); /*输出队中元素*/#endif // HEAD_H_INCLUDED
#include "head.h"int main(){ int comm; int no,e1,time,e2; int i,j; SqStack *St,*St1; //St是停车场,St1是在有车离开时,记录为该车移开位置的车辆 SqQueue *Qu; //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); /*倒车到临时栈St1中*/ } Pop(St,e1,e2); /*该汽车离开*/ printf(" >>%d汽车停车费用:%d\n",no,(time-e2)*Price); while (!StackEmpty(St1)) /*将临时栈St1重新回到St中*/ { Pop(St1,e1,e2); Push(St,e1,e2); } if (!QueueEmpty(Qu)) /*队不空时,将队头进栈St*/ { 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;}
#include "head.h"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");}
运行结果:
0 0
- 第七周项目6--停车场
- 第七周项目6 停车场
- 第七周项目(6):停车场模拟
- 第七周-项目6-停车场模拟
- 第七周 项目6-停车场模拟
- 第七周项目6-停车场模拟
- 第七周项目6 - 停车场模拟
- 第七周项目6-停车场模拟
- 第七周项目6--停车场模拟
- 第七周 项目6 停车场模拟
- 第七周实践项目6--停车场模拟
- 第七周项目6--停车场模拟
- 第七周项目6-停车场模拟
- 第七周 项目6 停车场模拟
- 第七周项目6 停车场模拟
- (第七周项目6)停车场模拟
- 第七周项目6-停车场模拟
- 第七周项目6--停车场模拟
- 数据结构之自建算法库——稀疏矩阵的三元组表示
- 猴子选大王(数组版)
- Vim 的配置
- 1004成绩排名
- 第九周上机实践项目3——稀疏矩阵的三元组表示的实现及应用(1)
- 第七周项目6 停车场
- 第7周项目4-队列数组
- 2015-10-30【项目1 - 猴子选大王(数组版)】
- 百度编辑器路径设置问题
- 1005继续3n+1猜想
- 第八周项目5-计数的模式匹配
- 第八周项目5--计数的模式匹配
- 稀疏矩阵的三元组表示的实现及应用(1)——建立稀疏矩阵三元组表示的算法库
- 1006换个格式输出整数