第七周项目6- 停车场模拟

问题描述及代码:
[cpp] view plain copy
1.	/*    
2.	*烟台大学计控学院     
3.	*作    者:朱建豪    
4.	*完成日期:2016年10月14日 
5.	*问题描述:设停车场是一个可停放n辆汽车的狭长死胡同,南边封口,汽车只能从北边进出(这样的停车场世间少有)。汽车在停车场内按车辆到达时间的先后顺序,最先到达的第一辆车停放在车场的最南端,依次向北排开。若车场内已停满n辆汽车,则后来的汽车只能在门外的候车场上等候,一旦有车开走,则排在候车场上的第一辆车即可开入。当停车场内某辆车要离开时,在它之后进入的车辆必须先退出车场为它让路(假定停车场内设有供车辆进出的便道,所有的司机也必须在车内随时待命),待该辆车开出大门外,其他车辆再按原次序进入车场。每辆停放在车场的车在它离开停车场时,要按停留的时间长短交纳费用。试为停车场编制按上述要求进行管理的模拟程序。  
6.	           提示:以栈模拟停车场,以队列模拟车场外的候车场,有车离开时,供车辆进出的便道也应该用栈表示。按照从键盘读入的输入数据序列进行模拟管理。汽车到达和离开时,每一组输入数据包括汽车牌照号码(设为整数)以及到达或离去的时刻(为简单化,也设为整数,如1,代表停车场开始营业的第1小时)。对每一组输入数据进行操作后的输出信息为:若是车辆到达,则输出汽车在停车场内或修车场上的停车位置;若是车辆离去,则输出汽车在停车场内停留的时间和应交纳的费用(在候车场上停留的时间不收费)。栈以顺序结构实现,队列以链表结构实现。  
7.	            
8.	            
9.	*/  
[cpp] view plain copy
1.	#include <stdio.h>  
2.	#include <malloc.h>  
3.	#define N 3                 /*停车场内最多的停车数*/  
4.	#define M 4                 /*候车场内最多的停车数*/  
5.	#define Price 2             /*每单位时间停车费用*/  
6.	typedef struct  
7.	{  
8.	    int CarNo[N];           /*车牌号*/  
9.	    int CarTime[N];         /*进场时间*/  
10.	    int top;                /*栈指针*/  
11.	} SqStack;                  /*定义顺序栈类型,用于描述停车场*/  
12.	  
13.	typedef struct  
14.	{  
15.	    int CarNo[M];           /*车牌号*/  
16.	    int front,rear;         /*队首和队尾指针*/  
17.	} SqQueue;                  /*定义循环队类型,用于描述候车场*/  
18.	  
19.	/*以下为顺序栈的基本运算算法*/  
20.	void InitStack(SqStack *&s)  
21.	{  
22.	    s=(SqStack *)malloc(sizeof(SqStack));  
23.	    s->top=-1;  
24.	}  
25.	int StackEmpty(SqStack *s)  
26.	{  
27.	    return(s->top==-1);  
28.	}  
29.	int StackFull(SqStack *s)  
30.	{  
31.	    return(s->top==N-1);  
32.	}  
33.	int Push(SqStack *&s,int e1,int e2)  
34.	{  
35.	    if (s->top==N-1)  
36.	        return 0;  
37.	    s->top++;  
38.	    s->CarNo[s->top]=e1;  
39.	    s->CarTime[s->top]=e2;  
40.	    return 1;  
41.	}  
42.	int Pop(SqStack *&s,int &e1,int &e2)  
43.	{  
44.	    if (s->top==-1)  
45.	        return 0;  
46.	    e1=s->CarNo[s->top];  
47.	    e2=s->CarTime[s->top];  
48.	    s->top--;  
49.	    return 1;  
50.	}  
51.	void DispStack(SqStack *s)  
52.	{  
53.	    int i;  
54.	    for (i=s->top; i>=0; i--)  
55.	        printf("%d ",s->CarNo[i]);  
56.	    printf("\n");  
57.	}  
58.	  
59.	/*以下为循环队列的基本运算算法*/  
60.	void InitQueue(SqQueue *&q)  
61.	{  
62.	    q=(SqQueue *)malloc (sizeof(SqQueue));  
63.	    q->front=q->rear=0;  
64.	}  
65.	int QueueEmpty(SqQueue *q)  
66.	{  
67.	    return(q->front==q->rear);  
68.	}  
69.	int QueueFull(SqQueue *q)       /*判断队满*/  
70.	{  
71.	    return ((q->rear+1)%M==q->front);  
72.	}  
73.	int enQueue(SqQueue *&q,int e)      /*进队*/  
74.	{  
75.	    if ((q->rear+1)%M==q->front)    /*队满*/  
76.	        return 0;  
77.	    q->rear=(q->rear+1)%M;  
78.	    q->CarNo[q->rear]=e;  
79.	    return 1;  
80.	}  
81.	int deQueue(SqQueue *&q,int &e)     /*出队*/  
82.	{  
83.	    if (q->front==q->rear)          /*队空的情况*/  
84.	        return 0;  
85.	    q->front=(q->front+1)%M;  
86.	    e=q->CarNo[q->front];  
87.	    return 1;  
88.	}  
89.	void DispQueue(SqQueue *q)      /*输出队中元素*/  
90.	{  
91.	    int i;  
92.	    i=(q->front+1)%M;  
93.	    printf("%d ",q->CarNo[i]);  
94.	    while ((q->rear-i+M)%M>0)  
95.	    {  
96.	        i=(i+1)%M;  
97.	        printf("%d ",q->CarNo[i]);  
98.	    }  
99.	    printf("\n");  
100.	}  
101.	//main函数用于模拟停车场的工作  
102.	int main()  
103.	{  
104.	    int comm;  
105.	    int no,e1,time,e2;  
106.	    int i,j;  
107.	    SqStack *St,*St1;  //St是停车场,St1是在有车离开时,记录为该车移开位置的车辆  
108.	    SqQueue *Qu;   //Qu是候车场  
109.	    InitStack(St);  
110.	    InitStack(St1);  
111.	    InitQueue(Qu);  
112.	    do  
113.	    {  
114.	        printf("输入指令(1:到达 2:离开 3:显示停车场 4:显示候车场 0:退出):");  
115.	        scanf("%d",&comm);  
116.	        switch(comm)  
117.	        {  
118.	        case 1:     /*汽车到达*/  
119.	            printf("输入车号和时间(设车号和时间均为整数): ");  
120.	            scanf("%d%d",&no,&time);  
121.	            if (!StackFull(St))         /*停车场不满*/  
122.	            {  
123.	                Push(St,no,time);  
124.	                printf("  >>停车场位置:%d\n",St->top+1);  
125.	            }  
126.	            else                        /*停车场满*/  
127.	            {  
128.	                if (!QueueFull(Qu))     /*候车场不满*/  
129.	                {  
130.	                    enQueue(Qu,no);  
131.	                    printf("  >>候车场位置:%d\n",Qu->rear);  
132.	                }  
133.	                else  
134.	                    printf("  >>候车场已满,不能停车\n");  
135.	            }  
136.	            break;  
137.	        case 2:     /*汽车离开*/  
138.	            printf("输入车号和时间(设车号和时间均为整数): ");  
139.	            scanf("%d%d",&no,&time);  
140.	            for (i=0; i<=St->top && St->CarNo[i]!=no; i++);  //在栈中找  
141.	            if (i>St->top)  
142.	                printf("  >>未找到该编号的汽车\n");  
143.	            else  
144.	            {  
145.	                for (j=i; j<=St->top; j++)  
146.	                {  
147.	                    Pop(St,e1,e2);  
148.	                    Push(St1,e1,e2);        /*倒车到临时栈St1中*/  
149.	                }  
150.	                Pop(St,e1,e2);              /*该汽车离开*/  
151.	                printf("  >>%d汽车停车费用:%d\n",no,(time-e2)*Price);  
152.	                while (!StackEmpty(St1))    /*将临时栈St1重新回到St中*/  
153.	                {  
154.	                    Pop(St1,e1,e2);  
155.	                    Push(St,e1,e2);  
156.	                }  
157.	                if (!QueueEmpty(Qu))        /*队不空时,将队头进栈St*/  
158.	                {  
159.	                    deQueue(Qu,e1);  
160.	                    Push(St,e1,time);       /*以当前时间开始计费*/  
161.	                }  
162.	            }  
163.	            break;  
164.	        case 3:     /*显示停车场情况*/  
165.	            if (!StackEmpty(St))  
166.	            {  
167.	                printf("  >>停车场中的车辆:"); /*输出停车场中的车辆*/  
168.	                DispStack(St);  
169.	            }  
170.	            else  
171.	                printf("  >>停车场中无车辆\n");  
172.	            break;  
173.	        case 4:     /*显示候车场情况*/  
174.	            if (!QueueEmpty(Qu))  
175.	            {  
176.	                printf("  >>候车场中的车辆:"); /*输出候车场中的车辆*/  
177.	                DispQueue(Qu);  
178.	            }  
179.	            else  
180.	                printf("  >>候车场中无车辆\n");  
181.	            break;  
182.	        case 0:     /*结束*/  
183.	            if (!StackEmpty(St))  
184.	            {  
185.	                printf("  >>停车场中的车辆:"); /*输出停车场中的车辆*/  
186.	                DispStack(St);  
187.	            }  
188.	            if (!QueueEmpty(Qu))  
189.	            {  
190.	                printf("  >>候车场中的车辆:"); /*输出候车场中的车辆*/  
191.	                DispQueue(Qu);  
192.	            }  
193.	            break;  
194.	        default:    /*其他情况*/  
195.	            printf("  >>输入的命令错误\n");  
196.	            break;  
197.	        }  
198.	    }  
199.	    while(comm!=0);  
200.	    return 0;  
201.	}  
运行结果:
<img src="https://img-blog.csdn.net/20161014112246828?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
知识点总结:
该程序运用了顺序栈和队列两种数据结构,其中停车场是栈,汽车先进后出,所以如果前面的汽车要出来,必须是后面的都出来之后才能够出来,可是出栈的汽车还要再进去,所以说将出栈的汽车保留到另一个栈中,前面的汽车走了再依次进入。这是停车场的运行方式,候车场是队列,即先来候车的汽车先去停车,所以这就运用了队列先进先出的特点。无论是候车室还是停车场,栈满与队满同样输出不能再进入车辆,车辆的停车费则只是根据时间的差距来计算
学习心得:
一个问题的解决需要好多知识的综合,所以还是需要把每部分都学会

    原文作者:停车场模拟问题
    原文地址: https://blog.csdn.net/zhuzhuzhujianhao/article/details/52814442
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞