停车场(队列堆栈基础练习)

#define N 2
#define M 5
#define True 1
#define False 0
#define NULL 0
typedef struct
{
    int num;
    int arrtime;
}ELEMTP;
typedef struct stack
{
    ELEMTP elem[N];
    int top;
}SqStack;

typedef struct node
{
    int num;
    struct node *next;
}QNode;
typedef struct
{
    QNode *front,*rear;
}LQueue;

void InitStack_Sq(SqStack *s);
int Push_Sq(SqStack *s,ELEMTP x);
ELEMTP Pop_Sq(SqStack *s);
void InitQueue_L(LQueue *q);
void EnQueue_L(LQueue *q,int num1);
int DelQueue_L(LQueue *q);

void InitStack_Sq(SqStack *s)
{
    s->top = 0;
}

int Push_Sq(SqStack *s,ELEMTP x)
{
    if(s ->top == N)
    {
        return False;
    }
    else
    {
        s->elem[s->top] = x;
        s->top++;
        return True;
    }
}

ELEMTP Pop_Sq(SqStack *s)
{
    ELEMTP x;
    if(s->top == 0)
    {
        x.num = 0;
        x.arrtime = NULL;
        return x;
    }
    else
    {
        s->top--;
        return s->elem[s->top];
    }

}
    
void InitQueue_L(LQueue *q)
{
    q->front = (QNode *)malloc(sizeof(QNode));
    q->rear = q->front;
    q->front->next = NULL;
    q->front->num = 0;
}
void EnQueue_L(LQueue *q,int num1)
{
    QNode *p;
    p = (QNode *)malloc(sizeof(QNode));
    p->next = NULL;
    p->num = num1;
    q->rear->next = p;
    q->rear = p;
    q->front->num++;
}
int DelQueue_L(LQueue *q)
{
    QNode *p;
    int n;
    if(q->front == q->rear)
    {
        return NULL;
    }
    else
    {
        p = q->front->next;
        q->front->next = p->next;
        if(p->next == NULL)
        {
            q->rear = q->front;
        }
        n = p->num;
        free(p);
        q->front->num--;
        return n;
    }
}

void Arrive(SqStack *s1,LQueue *q,ELEMTP x)
{
    int f;
    f = Push_Sq(s1,x);
    if(f == False)
    {
        EnQueue_L(q,x.num);
        printf("第%d号车停在便道第%d车位上\n",x.num,q->front->num);
    }
    else
    {
        printf("第%d号车停在停车场第%d车位上\n",x.num,s1->top);
    }
}

void Delive(SqStack *s1,SqStack *s2,LQueue *q,ELEMTP x)
{
    int n,f = False;
    ELEMTP y;
    QNode *p;
    while((s1->top>0)&&(f != True))
    {
        y = Pop_Sq(s1);
        if(y.num != x.num)
        {
            n = Push_Sq(s2,y);
        }
        else
        {
            f = True;
        }
    }
    if(y.num == x.num)
    {
        printf("第%d号车应收费%d元\n",x.num,(x.arrtime - y.arrtime)*M);
        while(s2->top>0)
        {
            y = Pop_Sq(s2);
            f = Push_Sq(s1,y);
        }
        n = DelQueue_L(q);
        if(n != NULL)
        {
            y.num = n;
            y.arrtime = x.arrtime;
            f = Push_Sq(s1,y);
            printf("第%d号车停在停车场第%d号车位上\n",y.num,s1->top);
        }
    }
    else
    {
        while(s2->top > 0)
        {
            y = Pop_Sq(s2);
            f = Push_Sq(s1,y);
        }
        p = q->front;
        f = False;
        while(f ==False && p->next != NULL)
        {
            if(p->next->num != x.num)
            {
                p = p->next;
            }
            else
            {
                p->next = p->next->next;
                q->front->num--;
                if(p->next == NULL)
                {
                    q->rear = q->front;
                }
                printf("第%d号车离开便道\n",x.num);
                f = True;
            }
            if(f == False)
            {
                printf("输入错误,没此车\n");
            }

        }
    }
}

void Display(SqStack *s1,LQueue *q)
{
    int k;
    QNode *p;
    printf("停车场状况:\n");
    if(s1->top != 0)
    {
        printf("车道 车号\n");
        for(k = 0;k < s1->top; k++)
        {
            printf("%d %d\n",k+1,s1->elem[k].num);
        }
    }
    else
    {
        printf("停车厂没车\n");
    }
    printf("便道状况:\n");
    if(q->front->num)
    {
        printf("车道 车号\n");
        for(k = 1, p = q->front->next;p;p = p->next)
        {
            printf("%d %d\n",k++,p->num);
        }
    }
    else
    {
        printf("便道没有车\n");
    }
}


void main()
{
    char ch1,ch2;
    SqStack *s1,*s2;
    LQueue *q;
    ELEMTP x;
    int flag;
    s1 = (SqStack *)malloc(sizeof(SqStack));
    s2 = (SqStack *)malloc(sizeof(SqStack));
    q = (LQueue *)malloc(sizeof(LQueue));
    InitStack_Sq(s1);
    InitStack_Sq(s2);
    InitQueue_L(q);
    flag = True;
    while(flag)
    {
        printf("请输入您的选择: \n");
        printf("S--显示停车场状况:\n");
        printf("A--车辆到达\n");
        printf("D--车辆离开\n");
        printf("E--程序结束\n");
        ch1 = getchar();
        switch(ch1)
        {
            case 'S':
                Display(s1,q);
            break;    
            case 'A':
                printf("输入数据: 车牌号,到达时间:");
                scanf("%d,%d",&x.num,&x.arrtime);
                Arrive(s1,q,x);
            break;
            case 'D':
                printf("输入数据: 车牌号,离开时间: ");
                scanf("%d,%d",&x.num,&x.arrtime);
                Delive(s1,s2,q,x);
            break;
            case 'E':
                flag = False;
                printf("程序正常结束: ");
                break;
            default:
                printf("\n");
        }
        
        ch2 = getchar();    


    }


}

猜你喜欢

转载自blog.csdn.net/Chris_xi/article/details/9896529