数据结构实践 停车场模拟 栈和队列综合

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

                       

本文是针对数据结构基础系列网络课程(3):栈和队列的实践项目。

  设停车场是一个可停放n辆汽车的狭长死胡同,南边封口,汽车只能从北边进出(这样的停车场世间少有)。汽车在停车场内按车辆到达时间的先后顺序,最先到达的第一辆车停放在车场的最南端,依次向北排开。若车场内已停满n辆汽车,则后来的汽车只能在门外的候车场上等候,一旦有车开走,则排在候车场上的第一辆车即可开入。当停车场内某辆车要离开时,在它之后进入的车辆必须先退出车场为它让路(假定停车场内设有供车辆进出的便道,所有的司机也必须在车内随时待命),待该辆车开出大门外,其他车辆再按原次序进入车场。每辆停放在车场的车在它离开停车场时,要按停留的时间长短交纳费用。试为停车场编制按上述要求进行管理的模拟程序。 
  这里写图片描述

 

提示:以栈模拟停车场,以队列模拟车场外的候车场,有车离开时,供车辆进出的便道也应该用栈表示。按照从键盘读入的输入数据序列进行模拟管理。汽车到达和离开时,每一组输入数据包括汽车牌照号码(设为整数)以及到达或离去的时刻(为简单化,也设为整数,如1,代表停车场开始营业的第1小时)。对每一组输入数据进行操作后的输出信息为:若是车辆到达,则输出汽车在停车场内或修车场上的停车位置;若是车辆离去,则输出汽车在停车场内停留的时间和应交纳的费用(在候车场上停留的时间不收费)。栈以顺序结构实现,队列以链表结构实现。

[参考解答]

#include <stdio.h>#include <malloc.h>#define N 10                    /*停车场内最多的停车数*/#define M 10                    /*候车场内最多的停车数*/#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){    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");}//main函数用于模拟停车场的工作int main(){    int comm;    int no,e1,time,e2;    int i,j,t;    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            {                t = St->top - i;  //需要出栈的车辆数目                for (j=0; j<t; j++)  //for (j=i; j<=St->top; j++)1楼评论讲的原错误写法                {                    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;}
   
   
  • 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
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow

这里写图片描述

猜你喜欢

转载自blog.csdn.net/hgdfguj/article/details/83821271