数据结构大作业之停车场管理系统

安排:
1、 分析停车场的数据属性,并依据停车场管理的功能要求,确定模拟系统设计方案;
2、 完成停车场管理的数据结构设计工作,包括栈和队列的逻辑结构和存储结构等;
3、 完成功能设计工作,包括车辆的信息输出、计时收费、动态排队情况等;
4、 使用C或C++程序设计语言编写实现算法程序;
5、 完成大作业报告电子版。

要求:
熟悉理解栈和队列的逻辑结构和存储结构,设计实现停车场管理的模拟系统,其主要内容如下:设停车场是一个可以停放n辆汽车的狭长通道,且只有一个大门可供汽车进出,其模型如下图1所示。汽车在停车场内按车辆到达时间的先后顺序,依次由北向南排列(大门在最南端,最先到达的第一辆汽车停放在车场的最北端),若车场内已停满n辆车,那么后来的汽车只能在门外的便道上等候,一旦有车开走,则排在便道上的第一辆车即可开入;当停车场内某辆汽车要离开时,在它之后进入的车辆必须先退出停车场按顺序开入临时停放道为其让路,待其开出大门外后,再按原次序进入车场,每辆停放在停车场的汽车在它离开停车场时必须按其停留的时间长短缴纳费用(从进入停车场开始计费)。

源代码:

#include<stdio.h>
#include<malloc.h>
#include<time.h>
#include <string.h> 
#include<stdlib.h>
#define N 3//停车场内最多的停车数
#define M 4//候车场内最多的停车数
#define Price 2//每单位的停车费用

void load()                    
{
    int i, count = 0;
    char str[50], str1[50];
    time_t t1, *t = &t1;
    /*t1=time(NULL)或t1=time(0)
    将空指针传递给time()函数
    并将time()返回值赋给变量t1*/
    printf("\n\n\n\n\n\n");
    printf("\t*                                                *\n");
    printf("\t*                                                *\n");
    printf("\t*                                                *\n");
    printf("\t*                                                *\n");
    printf("\t*           * * * * *     * * * * *              *\n");
    printf("\t*           *       *     *       *              *\n");
    printf("\t*           *       *     *       *        * * * *\n");
    printf("\t*           *       *     *       *        *     *\n");
    printf("\t*           *       *     *       *        *     *\n");
    printf("\t*           *       *     *      * *       *     *\n");
    printf("\t* * * * *   * * * * *     * * *  * * *     * * * *\n");
    i = 6;
    do {
        t1 = time(NULL);   /*t是空指针(NULL),直接返回当前时间。*/
        strcpy(str, ctime(t));
        if (count == 6) break;
        while (strcmp(str, str1) != 0)
            /*strcmp函数是string compare(字符串比较)的缩写
            当s1<s2时,返回为负数;
            当s1=s2时,返回值 = 0;
            当s1>s2时,返回正数。*/
        {
            strcpy(str1, str);//这里的目的是一秒输出一个>然后转换页面
            printf(" >");
            count++;
            break;
        }
    } while (1);
    system("cls");//清屏操作  
}

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;
}

bool StackEmpty(SqStack *s)//判断栈空
{
    return(s->top == -1);
}

bool StackFull(SqStack *s)//判断栈满
{
    return(s->top == N - 1);
}

bool Push(SqStack *&s,int e1,int e2)
{
    if(s->top == N - 1)
        return false;
         s->top++;
         s->CarNo[s->top] = e1;
         s->CarTime[s->top] = e2;
         return true;
}

bool Pop(SqStack *&s,int &e1,int &e2)//出栈
{
    if(s->top == -1)
        return false;
    e1 = s->CarNo[s->top];
    e2 = s->CarTime[s->top];
    s->top--;
    return true;
}

void DispStack(SqStack *s)
{
    for(int i = s->top;i >= 0;i--)
    printf("   %d\n",s->CarNo[i]);
    printf("\n");
}


//*********************************
//队列的操作

void InitQueue(SqQueue *&q) //初始化队列
{
    q = (SqQueue * )malloc(sizeof(SqQueue));
    q->front = q->rear = 0;
}

bool QueueEmpty(SqQueue *q)//判断队列空
{
    return(q->front == q->rear);
}

bool QueueFull(SqQueue *q)//判断栈满
{
    return((q->rear + 1)% M == q->front);//循环队列,牺牲第一个元素空间,当头指针在尾指针下一个位置时,队列为满
}

bool enQueue(SqQueue *&q,int e)//进队
{
    if((q->rear + 1)% M == q->front)
        return false;
    q->rear = (q->rear + 1) % M;
    q->CarNo[q->rear] = e;
    return true;
}

bool deQueue(SqQueue *&q,int &e)//出队操作
{
    if(q ->front == q->rear)
        return false;
    q ->front = (q->front + 1)%M;
    e = q->CarNo[q->front];
    return true;
}

void DispQueue(SqQueue *q)//展示队列的元素
{
    int 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,i,j;
    int no,e1,time,e2;
    SqStack *St,*St1;
    SqQueue *Qu;
    InitStack(St);
    InitStack(St1);
    InitQueue(Qu);
    load();
    do
    {
        printf("\t\t请按数字输入指令(1:进入停车场 2:离开停车场 3:停车场 4:候车场 0:退出):");
        scanf("%d",&comm);
        switch(comm)
        {
        case 1:
            printf("输入车号:");
            scanf("%d",&no);
            printf("输入到达时间:");
            scanf("%d",&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",&no);
            printf(" 请输入离开的时间:");
            scanf("%d",&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("  停车场中的车辆车牌号为:\n");
                DispStack(St);
                printf("\n");
            }
            else
                printf("   停车场中无车辆\n");
            break;
        case 4://输出候车场的车辆信息
            if(!QueueEmpty(Qu))
            {
                printf("   候车场车辆的车牌号为:");
                DispQueue(Qu);
                printf("\n");
            }
            else
                printf("   候车场中无车辆\n");
                 break;
        case 0:
            if(!StackEmpty(St))//输出停车场车辆的信息
            {
                printf("  停车场中的车辆车牌号为:");
                DispStack(St);
                printf("\n");
            }
            if(!QueueEmpty(Qu))
            {
                printf("   候车场车辆的车牌号为:");
                DispQueue(Qu);
                printf("\n");
            }
            break;
        default:
            printf("   您输入的数字有误,请重新输入");
            break;
        }
    }while(comm!=0);
    return 1;
    }

截图展示:

停车场管理系统架构 停车场管理系统方案_数据结构

停车场管理系统架构 停车场管理系统方案_数据结构_02

停车场管理系统架构 停车场管理系统方案_#include_03

停车场管理系统架构 停车场管理系统方案_停车场管理系统架构_04

停车场管理系统架构 停车场管理系统方案_停车场管理系统架构_05


停车场管理系统架构 停车场管理系统方案_程序设计_06