用栈和队列实现停车场

声明两个栈一个是停车栈一个是让路栈,当停车栈存满时,自动进入等候队列。当车进入停车场和离开停车场时都分别记录下时间,保存在栈和链表中,用链表去保存完整的信息。该项目有三个难点:1、出车时,也就是当车需要出去时需要通过让路栈实现出车。原理是要出中间某个元素就把这个元素之上的所有元素依次出栈并压入让路栈,然后保存一下时间并把他的所有信息保存到链表之中。最后把让路栈中的元素在出栈并压入原有栈中。此时需要检测是否栈中有空余,若有将等候队列中的车即队头元素删除并进停车栈; 2、时间函数,这个花了我好长时间最后采用了周围人提供的方法实现的; 3、需要注意当车进入登记时需检测车牌是否与队列与场内的车牌相同,相同需要提示出错,查询车辆信息时,要有顺序应该先查栈内信息在查队列,最后查询链表,当栈内查询到之后自动跳过队列的查询。代码如下:

头文件包括一些函数声明

#ifndef  _PARK_H
#define  _PARK_H

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>

#define  success  10000
#define  failure  10001
#define  TRUE     10002
#define  FALSE    10003

#define BLACK                "\e[0;30m"
#define L_BLACK              "\e[1;30m"
#define RED                  "\e[0;31m"
#define L_RED                "\e[1;31m"
#define GREEN                "\e[0;32m"
#define L_GREEN              "\e[1;32m"
#define BROWN                "\e[0;33m"
#define YELLOW               "\e[1;33m"
#define BLUE                 "\e[0;34m"
#define L_BLUE               "\e[1;34m"
#define PURPLE               "\e[0;35m"
#define L_PURPLE             "\e[1;35m"
#define CYAN                 "\e[0;36m"
#define L_CYAN               "\e[1;36m"
#define GRAY                 "\e[0;37m"
#define WHITE                "\e[1;37m"
#define NONE                 "\e[0m"


struct information
{
    int no;
    char carnum[20];
    long t1;
    long t2;
    char intime[64];
    char outtime[64];
    long usetime;
    struct information *next;
};
typedef struct information Info;

struct parkinfo
{
    int no;
    char carnum[20];
    long t1;
    long t2;
    char outtime[64];
    char intime[64];
};
typedef struct parkinfo PInfo;

struct park
{
    int top;
    PInfo *pinfo;
};
typedef struct park Park;

struct node
{
    int no;
    char carnum[20];
    long t1;
    char intime[64];
    struct node *next;
};
typedef struct node Node;

struct wait
{
    Node *front;
    Node *rear;
};
typedef struct wait Wait;


int ParkInit(Park **p);
int WaitInit (Wait **p);
int InfoLinkInit(Info **p);
int Push(Park *p, PInfo *info);
int EmptyStack(Park *p);
int Pop(Park *p);
int StackClear(Park *s);
int StackDestroy(Park **p);
int QueueInsert(Wait *p, Node *info);
int DeleteQueue(Wait *p);
int QueueEmpty(Wait *q);
int ClearQueue(Wait *p);
int DestroyQueue(Wait **q);
int LinkInsert(Info *q, Info *e);
int LinkLength(Info *l);
int LinkEmpty(Info *q);
int ClearLink(Info *q);
int DestroyLink(Info **q);
int EnterCar(Park *park, Wait *wait);

#endif

这是main.c

#include "park.h"
int count = 1;
void Hello()
{
    printf("************************************************\n");
    printf("*****              菜   单                 *****\n");
    printf("************************************************\n");
    printf("*****         1、进车登记                  *****\n");
    printf("*****         2、出车登记                  *****\n");
    printf("*****         3、查询车辆信息              *****\n");
    printf("*****         4、查询出入记录              *****\n");
    printf("*****         5、查询场内车辆信息          *****\n");
    printf("*****         6、查询等候车辆信息          *****\n");
    printf("*****         7、退出系统                  *****\n");
    printf("************************************************\n");
}

int EnterCar(Park *park, Wait *wait)
{
    system("clear");
    char num[20];
    printf("请填写车辆信息(车牌号)\n");
    scanf("%s", num);
    int i;
    for(i = 0; i <= park->top; i++)
    {
        if(strcmp(num, park->pinfo[i].carnum) == 0)
        {
            printf("您输入的车牌已存在,如有疑惑请拨打 110!\n");
            sleep(1);
            return 0;
        }
    }
    Node *n = wait->front->next;
    while(n)
    {
        if(strcmp(num, n->carnum) == 0)
        {
            printf("您输入的车牌已存在,如有疑惑请拨打 110\n");
            return 0;
        }
        n = n->next;
    }

    printf("正在为您查询空闲车位.....\n");
    sleep(1);
    char flag = '*';
    char quit = 'a';
    while (flag == '*')
    {
        if(park->top == 9)
        {
            printf("停车位已满,请移至等候队列,注意礼让!\n");
            printf("如需退出本停车场,请按0,任意键继续!\n");
            scanf(" %c", &quit);
            if(quit == '0')
            {
                return;
            }
            Node *node = (Node *)malloc(sizeof(Node));
            strcpy(node->carnum, num);
            time_t t;
            t = time(NULL);
            node->t1 = time(&t);
            strcpy (node->intime, ctime(&t));
            node->no = count;
            count++;
            node->next = NULL;
            QueueInsert(wait, node);
            printf("车牌为%s 的车辆已进入等候队列!\n",node->carnum);
        }
        else
        {
            printf("您好!第%d个车位空闲\n",park->top + 2);
            printf("如需退出本停车场,请按0,任意键继续!\n");
            scanf(" %c", &quit);
            if(quit == '0')
            {
                return;
            }
            PInfo *pinfo = (PInfo *)malloc(sizeof(PInfo));
            strcpy(pinfo->carnum, num);
            time_t t;
            t = time(NULL);
            pinfo->t1 = time(&t);
            strcpy (pinfo->intime, ctime(&t));
            pinfo->no = count;
            count++;
            Push(park, pinfo);
            free(pinfo);
            printf("车牌为%s 的车辆已成功停靠!\n",park->pinfo[park->top].carnum);
            sleep(1);
        }
        printf("输入任意键返回!\n");
        scanf(" %c",&flag);
    }
    return success;
}

int OutCar(Park *park, Wait *wait, Info *info, Park *let)
{
    system("clear");
    char num[20];
    char quit = 'a';
    int i, j, top1 = park->top;
    char flag = '*';
    while(flag == '*')
    {
        printf("请登记需要驶出的车牌号!\n");
        scanf("%s", &num);
        printf("如需返回请按0,任意键继续!\n");
        scanf(" %c", &quit);
        if(quit == '0')
        {
            return;
        }
        for(i = 0; i <= top1; i++)
        {
            if(strcmp(park->pinfo[i].carnum, num) == 0)
            {
                break;
            }
        }
        if(i <= top1)
        {
            PInfo *pinfo = (PInfo *)malloc(sizeof(PInfo));
            for(j = 0; j < top1 - i; j++)
            {
                *pinfo = park->pinfo[park->top];
                Push(let, pinfo);
                Pop(park);
            }
            /*把栈里的信息转到链表中*/
            *pinfo = park->pinfo[park->top];
            time_t t;
            t = time(NULL);
            pinfo->t2 = time(&t);
            strcpy(pinfo->outtime, ctime(&t));
            Pop(park);
            Info *in = (Info *)malloc(sizeof(Info));
            in->no = pinfo->no;
            in->t1 = pinfo->t1;
            in->t2 = pinfo->t2;
            strcpy(in->carnum, pinfo->carnum);
            strcpy(in->intime, pinfo->intime);
            strcpy(in->outtime, pinfo->outtime);
            in->usetime = in->t2 - in->t1;
            LinkInsert(info, in);
            for(j = 0; j < top1 - i; j++)
            {
                *pinfo = let->pinfo[let->top];
                Push(park, pinfo);
                Pop(let);
            }
            free(pinfo);
            printf("已离开\n");
            sleep(1);
            PInfo *p2 = (PInfo *)malloc(sizeof(PInfo));
            int ret = QueueEmpty(wait);
            if(ret != TRUE)
            {
                printf("等候队列车牌号为%s 的车辆驶入!\n",wait->front->next->carnum);
                sleep(1);
                p2->no = wait->front->next->no;
                strcpy(p2->carnum, wait->front->next->carnum);
                time_t t2;
                t2 = time(NULL);
                p2->t1 = time(&t2);
                strcpy(p2->intime, ctime(&t2));
                Push(park, p2);
                free(p2);
                DeleteQueue(wait);
            }
        }
        else
        {
            printf("没有这辆车!\n");
        }
        printf("按任意键退出!\n");
        scanf(" %c", &flag);
    }
    return 0;
}

int QueryCar(Park *park, Wait *wait, Info * info)
{
    char quit = 'a';
    system("clear");
    printf("欢迎进入车辆信息查询系统,如需退出请按0,任意键继续\n");
    scanf(" %c", &quit);
    if(quit == '0')
    {
        return 0;
    }
    int i;
    char num[20];
    printf("请输入需要查询的车牌号:");
    scanf("%s", num);
    printf("\n\n");
    char flag = '*';
    while(flag == '*')
    {
        printf("正在为您查询场内车辆......\n");
        sleep(1);
        for(i = 0; i <= park->top; i++)
        {
            if(strcmp(num, park->pinfo[i].carnum) == 0)
            {
                printf("车牌号:%s 进入时间:%s \n",park->pinfo[i].carnum, park->pinfo[i].intime);
                break;
            }
        }
        if(i == park->top + 1)
        {
            printf("场内没有此车牌的车!\n");
            printf("为您查询等候队列车辆!\n");
            sleep(1);
            Node *w = wait->front->next;
            while(w)
            {
               if( strcmp(w->carnum, num) == 0)
               {
                    printf("该车正在等候停车\n");
                    printf("车牌号:%s 进入时间:%s \n",w->carnum, w->intime);
                    break;
               }
               w = w->next;
            }
            if(w == NULL)
            {
                printf("等候队列也无此车牌的车!\n");
            }
        }
        int k = 0;
        Info *L = info->next;
        printf("正在为您查询历史记录\n");
        sleep(1);
        while(L)
        {
            if(strcmp(num, L->carnum) == 0)
            {
                printf("车牌号:%s \n进入时间:%s离开时间:%s停留时间: %d\n",L->carnum, L->intime, L->outtime, L->usetime);
                k++;
            }
            L = L->next;
        }
        if(k == 0)
        {
            printf("历史记录为空!\n");
        }
        printf("输入任意键退出!\n");
        scanf(" %c", &flag);
    }
}

int AccRecord(Park *park, Info *inf)
{
    char quit = 'a';
    system("clear");
    printf("欢迎进入车辆出入记录查询系统,如需退出请按0,任意键继续\n");
    scanf(" %c", &quit);
    if(quit == '0')
    {
        return 0;
    }
    int i;
    char flag = '*';
    while(flag == '*')
    {
        printf("正在为您查询场内车辆记录......\n\n");
        sleep(1);
        for(i = 0; i <= park->top; i++)
        {
            printf("车牌号:%s 进入时间:%s \n",park->pinfo[i].carnum, park->pinfo[i].intime);
        }
        if(i == 0)
        {
            printf("场内暂无车辆!\n");
        }
        printf("正在为您查询历史往来车辆信息......\n\n");
        sleep(1);
        Info *L = inf->next;
        int k = 0;
        printf("Link length is %d\n",LinkLength(inf));
        while(L)
        {
                printf("车牌号:%s\n进入时间: %s离开时间:%s停留时间: %d\n",L->carnum, L->intime, L->outtime, L->usetime);
                L = L->next;
                k++;
        }
        if(k == 0)
        {
            printf("历史记录为空!\n");
        }
        printf("任意键退出!\n");
        scanf(" %c", &flag);
    }
    return 1;
}

int InterInfo(Park *park)
{
    char quit = 'a';
    system("clear");
    printf("欢迎进入查看场内车辆信息系统,如需退出请按0,任意键继续\n");
    scanf(" %c", &quit);
    if(quit == '0')
    {
        return 0;
    }
    int i;
    char flag = '*';
    while(flag == '*')
    {
        printf("场内车辆信息如下:\n");
        for(i = 0; i <= park->top; i++)
        {
            printf("车牌号:%s 进入时间:%s \n",park->pinfo[i].carnum, park->pinfo[i].intime);
        }
        if(i == 0)
        {
            printf("场内暂无车辆!\n");
        }

        printf("任意键退出!\n");
        scanf(" %c", &flag); 
    }
    return 1;
}

int  WaitInfo(Wait *wait)
{
    char quit = 'a';
    system("clear");
    printf("欢迎进入查看等候车辆信息系统,如需退出请按0,任意键继续\n");
    scanf(" %c", &quit);
    if(quit == '0')
    {
        return 0;
    }
    int i = 0;
    char flag = '*';
    while(flag == '*')
    {
        printf("正在为您查询等候车辆信息请稍后......\n");
        sleep(1);
        Node *N = wait->front->next;
        while(N)
        {
            i++;
            printf("车牌号:%s 进入时间:%s \n",N->carnum, N->intime);
            N = N->next;
        }
        if(i == 0)
        {
            printf("现在没有正在等候的车辆!\n");
        }
        printf("任意键退出!\n");
        scanf(" %c", &flag); 
    }
    return 1;
}

int main()
{
    system("clear");
    char choice = 'a';
    int ret;
    Park *park = NULL;
    Park *let = NULL;
    ret = ParkInit(&park);
    if(ret == failure)
    {
        printf("Park Init failure!\n\n");
        exit(1);
    }
    else
    {
        printf("停车系统初始化成功,请稍后......\n\n");
        sleep(1);
    }
    ret = ParkInit(&let);
    if(ret == failure)
    {
        printf("give way strack Init failure!\n\n");
        exit(1);
    }
    else
    {
        printf("让路系统初始化成功,请稍后......\n\n");
        sleep(1);
    }
    Wait *wait = NULL;
    ret = WaitInit(&wait);
    if(ret == failure)
    {
        printf("wait queue Init failure!\n\n");
        exit(1);
    }
    else
    {
        printf("等候系统初始化成功,请稍后......\n\n");
        sleep(1);
    }

    Info *inf = NULL;
    ret = InfoLinkInit(&inf);
    if(ret == failure)
    {
        printf("Infomation Init failure!\n");
        exit(1);
    }
    else
    {
        printf("信息记录系统初始化成功!\n");
        sleep(1);
    }
    printf("即将进入停车管理系统......\n");
    sleep(1);


    while(1)
    {
        system("clear");
        Hello();
        printf("please input your choice!\n");
        scanf(" %c", &choice);
        switch(choice)
        {
            case '1':
                EnterCar(park, wait);
                break;
            case '2':
                OutCar(park, wait, inf, let);
                break;
            case '3':
                QueryCar(park, wait, inf);
                break;
            case '4':
                AccRecord(park, inf);
                break;
            case '5':
                InterInfo(park);
                break;
            case '6':
                WaitInfo(wait);
                break;
            case '7':
                printf("感谢使用,期待下次光临!\n");
                StackClear(park);
                StackClear(let);
                StackDestroy(&park);
                StackDestroy(&let);
                ClearQueue(wait);
                DestroyQueue(&wait);
                ClearLink(inf);
                DestroyLink(&inf);
                sleep(1);
                exit(1);
                break;
            default :
                printf("输入有误,请重新输入!\n");
                sleep(1);
                break;

        }
        
    }
    return 0;
}

下面是其中包含的一些基础子函数比如初始化之类的:

#include"park.h"
/*     初 始 化 操 作        */
int ParkInit(Park **p)
{
    if(p == NULL)
    {
        return failure;
    }
    (*p) = (Park *)malloc(sizeof(Park));
    if(*p == NULL)
    {
        return failure;
    }
    (*p)->top = -1;
    (*p)->pinfo = (PInfo *)malloc(sizeof(PInfo) * 10);
    if((*p)->pinfo == NULL)
    {
        return failure;
    }
    return success;
}

int WaitInit (Wait **p)
{
    if(p == NULL)
    {
        return failure;
    }
    (*p) = (Wait *)malloc(sizeof(Wait));
    if((*p) == NULL)
    {
        return failure;
    }
    Node *q = (Node *)malloc(sizeof(Node));
    (*p)->front = (*p)->rear = q;
    q->next = NULL;
    return success;
}
int InfoLinkInit(Info **p)
{
    (*p) = (Info *)malloc(sizeof(Info));
    if((*p) == NULL || p == NULL)
    {
        return failure;
    }
    (*p)->next = NULL;
    return success;
}
/* 栈 的 操 作                */
int Push(Park *p, PInfo *info)
{
    if(p == NULL || info == NULL || p->top == 9)
    {
        return failure;
    }

    p->top++;
    p->pinfo[p->top] = *info;
    return success;
}


int EmptyStack(Park *p)
{
    if(p == NULL)
    {
        return failure;
    }
    return (p->top == -1) ? TRUE :FALSE;
}
int Pop(Park *p)
{
    if(p == NULL || p->top == -1)
    {
        return failure;
    }
    p->top--;
    return success;
    
}
int StackClear(Park *s)
{
    if (NULL == s)
    {
        return failure;
    }
    s->top = -1;
    return success;
}

int StackDestroy(Park **p)
{
    if((*p) == NULL || p == NULL)
    {
        return failure;
    }

    free ((*p)->pinfo);
    free((*p));
    return success;
}
/*    队 列 操 作                   */
int QueueInsert(Wait *p, Node *info) //info->next = NULL;
{
    if(p == NULL)
    {
        return failure;
    }
    Node *q = info;
    p->rear->next = q;
    q->next = NULL;
    p->rear = q;
    return success;
}

int DeleteQueue(Wait *p)
{
    if(p == NULL || p->rear == p->front)
    {
        return failure;
    }
    Node *q = p->front->next;
    p->front->next = q->next;
    if(q->next == NULL)
    {
        p->rear = p->front;
    }
    free (q);
    return success;
}

int QueueEmpty(Wait *q)
{
    if (q == NULL)
    {
        return failure;
    }
    return (q->front == q->rear) ? TRUE : FALSE;
}

int ClearQueue(Wait *p)
{
    if(p == NULL || p->front == p->rear)
    {
        return failure;
    }
    Node *q = p->front->next;
    while(q)
    {
        p->front->next = q->next;
        free(q);
        q = p->front->next;
    }
    p->rear = p->front;
    return success;
}

int DestroyQueue(Wait **q)
{
    if(q == NULL || (*q) == NULL)
    {
        return failure;
    }
    free((*q)->front);
    free(*q);
    (*q) = NULL;
    return success;
}
 /*  链 表 操 作                */ 
int LinkInsert(Info *q, Info *e)
{
    Info *l = q;
    if(l == NULL || e == NULL)
    {
        return failure;
    }
    while (l->next)
    {
        l = l->next;
    }

    l->next = e;
    e->next = NULL;
    return success;
}


int LinkLength(Info *l)
{
    if (NULL == l)
    {
        return failure;
    }
    int len = 0;
    Info *p = l->next;
    while (p)
    {
        len++;
        p = p->next;
    }
    return len;
}
int LinkEmpty(Info *q)
{
    return (q->next == NULL) ? TRUE : FALSE;
}


int ClearLink(Info *q)
{
    if(q == NULL)
    {
        return failure;
    }
    Info *p = q->next;
    while (p)
    {
        q->next = p->next;
        free(p);
        p = q->next;
    }
    return success;
}

int DestroyLink(Info **q)
{
    if(q == NULL || (*q) == NULL)
    {
        return failure;
    }
    free(*q);
    (*q) = NULL;
    return success;
}

猜你喜欢

转载自blog.csdn.net/LX370ZZZ/article/details/81710741