用栈和队列实现的停车场管理系统

xiaoxiao2021-02-28  86

停车场管理 问题描述:停车场是一个能放 n 辆车的狭长通道,只有一个大门,汽车按到达 的先后次序停放。若车场满了,车要停在门外的便道上等候,一旦有车走,则便 道上第一辆车进入。当停车场中的车离开时,由于通道窄,在它后面的车要先退 出,待它走后在依次进入。汽车离开时按停放时间收费。 基本功能要求: (1) 建立三个数据结构分别是:停放栈、让路栈、等候队列。 (2) 输入数据模拟管理过程,数据(入或出,车号)。

#ifndef __PARK_H__

#define __PARK_H__ #define FALSE  0 #define TRUE   1 #define TIME  60 #define SIZE  10 #include "error.h" #include <time.h> //typedef int QueData; typedef struct _quedata { int num; time_t t_start; time_t t_end; }QueData; typedef struct _parkqueue { QueData data[SIZE]; int front; int rear; }ParkQueue; typedef struct _stack { QueData data[SIZE]; int top; }Stack; void Park(Stack *q, ParkQueue *p); void Leave(Stack *q, ParkQueue *p, Stack *s); void Look(); void Quit(); void Look(Stack *q, ParkQueue *p); int GetFront(ParkQueue *q, int *x); int QueueEmpty(ParkQueue *q); int DeQueue(ParkQueue *q, int *x); void MainInterface(); int InitStack(Stack *s); void InitQueue(ParkQueue *q); void Choice(int x,Stack *pq, ParkQueue *wq, Stack *s); int QueueFull(ParkQueue *q); int EnQueue(ParkQueue *q, int x); int StackFull (Stack *s); int Push (Stack *s, int x); int Pop (Stack *s, int *x, float *t); int StackEmpty (Stack *s); int GetTop(Stack *s, int *x); int Push2 (Stack *s, int x, float t);

#endif  //__PARK_H__

#include "Park.h"

#include <stdio.h>

#include <time.h> void MainInterface() { system("clear"); printf("\t*************停车管理系统******************\n"); printf("\t*                                         *\n"); printf("\t*        1.停车                           *\n"); printf("\t*                                         *\n"); printf("\t*        2.离开                           *\n"); printf("\t*                                         *\n"); printf("\t*        3.查看停车场情况                 *\n"); printf("\t*                                         *\n"); printf("\t*        4.退出                           *\n"); printf("\t*                                         *\n"); printf("\t*******************************************\n"); printf("\n"); } void Choice(int x, Stack *pq, ParkQueue *wq, Stack *s) { switch(x) { case 1: MainInterface(); Park(pq,wq); break; case 2: MainInterface(); Leave(pq,wq,s); break; case 3: MainInterface(); Look(pq,wq); break; case 4: break; default: printf("错误输入\n"); break; } } void Park(Stack *q, ParkQueue *p) { if(q == NULL || p == NULL) { errno = ERROR; return ; } int x;                              //1 printf("\t请输入车牌号:\n"); scanf("%d",&x); if(StackFull(q) != TRUE) { if(Push(q,x) == TRUE) { printf("\t停车成功\n"); return; } } else { if(EnQueue(p,x) == TRUE) { printf("\t在候车区等待\n"); return; } else { printf("\t候车区车位已满\n"); return; } } } void Leave(Stack *q, ParkQueue *p, Stack *s) { if(q == NULL || p == NULL || s == NULL) { errno = ERROR; return ; } int n;                         //1 printf("\t请输入车牌号:\n"); scanf("%d",&n); if(StackEmpty(q)) { printf("\t当前停车场内没有车辆\n"); return; } else { int x;       //1 float t; GetTop(q, &x); if(x == n)        //1 {   Pop(q, &x, &t); printf("\t离开成功\n");        printf("\t车牌号:%d    停车时间:%.2f分钟\n", x, t/TIME); while(QueueEmpty(p) != TRUE && StackFull(q) != TRUE) { DeQueue(p,&x); Push(q,x); } } else { while(x != n)       //1 { if(StackEmpty(q) == TRUE) { printf("\t没有该车牌号\n"); break; } Pop(q, &x, &t); Push2(s, x, t); } if(x != n)           //1 { while(StackEmpty(s) != TRUE) { Pop(s, &x, &t); Push2(q, x, t); } } if(x == n)             //1 { Pop(s, &x, &t); printf("\t离开成功\n"); printf("\t车牌号:%d    停车时间:%.2f分钟\n", x, t/TIME); while(StackEmpty(s) != TRUE) { Pop(s, &x, &t); Push2(q, x, t); } while(QueueEmpty(p) != TRUE && StackFull(q) != TRUE) { DeQueue(p,&x); Push(q,x); } } } } } void Look(Stack *q, ParkQueue *p) { int i; printf("\t停车场共%d个车位,当前停车场共有%d辆车,等待区共有%d辆车\n" ,SIZE,q->top+1,p->rear-p->front); for(i=0; i<=q->top; i++) { q->data[i].t_end = time(NULL); } for(i=0; i<=q->top; i++) { printf("\t车牌号:%d    停车时间:%.2f分钟\n",q->data[i].num,difftime(q->data[i].t_end,q->data[i].t_start)/TIME) ; } return; } void Quit() { return; } int GetTop(Stack *s, int *x)         //1 { if(s == NULL) { errno = ERROR; return FALSE; } if(StackEmpty(s)) { errno = EMPTY_STACK; return FALSE; } *x = s->data[s->top].num;          //1 return TRUE; } int QueueEmpty(ParkQueue *q) { if(q == NULL) { errno = ERROR; return FALSE; } return (q->front == q->rear); } int DeQueue(ParkQueue *q, int *x)    //1 { if(q == NULL) { errno = ERROR; return FALSE; } if(QueueEmpty(q)) { errno = EMPTY_QUE; return FALSE; } q->front = (q->front+1)%SIZE; *x = q->data[q->front].num;         //1 return TRUE; } int QueueFull(ParkQueue *q) { if(q == NULL) { errno = ERROR; return FALSE; } return (q->front == (q->rear+1)%SIZE); } int EnQueue(ParkQueue *q, int x)     //1 { if(q == NULL) { errno = ERROR; return FALSE; } if(QueueFull(q)) { errno = FULL_QUE; return FALSE; } q->rear = (q->rear+1)%SIZE; q->data[q->rear].num = x;               //1 return TRUE; } int StackFull (Stack *s) { if(s == NULL) { errno = ERROR; myError("StackFull"); return FALSE; } return (s->top == SIZE-1); } int Push (Stack *s, int x) { if(s == NULL) { errno = ERROR; return FALSE; } if(StackFull(s)) { errno = FULL_STACK; return FALSE; }                                                  s->data[++s->top].num = x;            //1 s->data[s->top].t_start = time(NULL); //1                              return TRUE; } void InitQueue(ParkQueue *q) { if(q == NULL) { errno = ERROR; return ; } q->front = 0; q->rear  = 0; } int InitStack(Stack *s) { if(s == NULL) { errno = ERROR; return FALSE; } s->top = -1; return TRUE; } int Pop (Stack *s, int *x, float *t) { if(s == NULL) { errno = ERROR; return FALSE; } if(StackEmpty(s)) { errno = EMPTY_STACK; return FALSE; } *x = s->data[s->top].num;                //1     s->data[s->top].t_end = time(NULL);    //1     *t = difftime(s->data[s->top].t_end,s->data[s->top].t_start);      //1 s->top--; return TRUE;  } int StackEmpty (Stack *s) { if(s == NULL) { errno = ERROR; myError("StackEmpty"); return FALSE; } return (s->top == -1); } int Push2 (Stack *s, int x, float t) { if(s == NULL) { errno = ERROR; return FALSE; } if(StackFull(s)) { errno = FULL_STACK; return FALSE; }                                                  s->data[++s->top].num = x;            //1                            s->data[s->top].t_start = time(NULL) - t; return TRUE; }

#include <stdio.h> #include "Park.h" int main() { Stack pq; ParkQueue wq; Stack  s; InitStack(&s); InitStack(&pq); InitQueue(&wq); int i; /*for(i=0; i<5; i++) { Push(&pq,i); }*/ MainInterface(); while(1) { int x; scanf("%d",&x); Choice(x, &pq, &wq, &s); if(x == 4) break; } system("clear"); return 0; }

转载请注明原文地址: https://www.6miu.com/read-24406.html

最新回复(0)