停车场管理系统

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

方案:设定两个栈,一个队列。第一个栈为停车场内,停车位,主要负责初始化。第二个栈负责,当有车要离开,后面的车

必须先退出,保存退出的车的车号和停的时间,等车离开,在将他们压入第一个栈,即停车位。队列负责

候车区一块,满足先来先服务的原则,若停车位满,只能停入候车区。当停车位不满时,候车区有车在等。

就将队列的第一辆车停入。

 主函数park.c

#include <stdio.h>
#include <stdlib.h>
#include "Queue.h"                           //队列
#include "temp_stack.h"                      //栈

void display1( )                             //查看停车场中的车号和停车时间
{
    inpark *p = S;
    inpark *q = S;
    int seconds;
    seconds = time(NULL);
    printf("车  位:");
    while(p)
    {
       printf("%d\t",p->data.num);
       p = p->next;
    }
    printf("\n");
    printf("停车时间:");
    while(q)
    {  
       printf("%d秒\t",seconds-(q->data.seconds));   //停车时间采用最初的时间
       q = q->next;                                  //减去查看的时间,得到已停的
    }                                                //时间
    
    printf("\n");
}
outpark *stop(outpark *Q,int x)                      //停车
{
    
    if(stackfull())                                   //如果停车位已满,只能停入
    {                                                 //候车区
       printf("停车位已满,停入候车区\n");
       Q = inqueue(Q,x);
       return Q;
    }
    else
    {
       push(x);                                        //停车位没满直接停入
    }
    return Q;
}
outpark *leave(outpark *Q,int x)                       //某辆车离开
{
    car ch;
    int temp;
    outpark *Q1=NULL;
    while( (S!=NULL) && (S->data.num)!=x)              //通过出栈
    {                                                  //找到其所在的停车位
        get(&ch);                                       
        pop( );                                        //找车先让后面不是的车退出
        push2(ch);                                     //压入一个新栈,保存其车号和
    }                                                  //最初停靠时间
    if(S == NULL)                                       //找不到该车号
    {
       printf("停车场内没有这个车号\n");
    }
    else
    {
       pop( );                                
       while(!stackEmpty2( ))                           //如果离开的不是第一辆车
       {                                                //要把原先在其后面倒出去的车
           get2(&ch);                                   //再停进来
	          pop2();
           push1(ch);
       }
       if(!queueEmpty(Q))                               //如果候车区有车等,就将最前面的
       {                                                 //停入
           getqueue(Q,&temp);
	         Q = dequeue(Q);
	         push(temp);
       }
    }
    return Q;
}
int main()
{
    outpark *Q = NULL;
    inpark *p = NULL;
    outpark *q = NULL;
    int ch;
    int x;
    int i=0,j=0;
    while(1)                                            
    {
         p = S;
         q = Q;
	       i=j=0;
         while(q!=NULL)                                    //查看候车区有多少辆车
	    {
	     i++;
	     q = q->next;
	    }
	       while(p!=NULL)                                    //查看停车位有多少辆车
	    {
	     j++;
	     p = p->next;
	    }
         printf("********目前停车场状况********\n");
	 printf("停车场共有10个车位,当前停车场共有%d辆,等待区共有%d辆车。\n",j,i);
	 printf("***********************\n");
	 printf("------------Welcome to our Car Parking-----------\n");
	 printf("1-----------停车\n");
	 printf("2-----------离开\n");
	 printf("3-----------查看停车场停车情况\n");
	 printf("4-----------退出\n");
	 scanf("%d",&ch);
	 if(ch==4)
	 {
	     break;
	 }
	 switch(ch)
	 {
	     case 1:
	          {
		    printf("请输入停入的车号:");
		    scanf("%d",&x);
		    Q = stop(Q,x);
		    break;
		  }
	     case 2:
	          {
		    printf("请输入离开的车号:");
		    scanf("%d",&x);
		    Q = leave(Q,x);
		    break;
		  }
	     case 3:
	          {
		    display1();
		    break;
		  }
	     default:
	          {
		    printf("输入有误\n");
		  }
            }
	 }

    return 0;
}


第一个栈stack1.h:

#include <time.h>
typedef struct car
{
     int num;
     int seconds;
}car;
typedef struct inpark
{
     car data;
     struct inpark *next;
}inpark;
inpark *S = NULL;
int stackEmpty()
{
     return S==NULL;
}
int stackfull()
{
     int i=0;
     inpark *p = S;
     while(p)
     {
         i++;
	 p = p->next;
     }
     return i>9;
}
void push(int i)
{
     if(stackfull())
     {
         printf("停车位满\n");
     }
     else
     {
         inpark *p = (inpark *)malloc(sizeof(inpark));
	 p->data.num = i;
	 p->data.seconds = time(NULL);
	 p->next = S;
	 S = p;
     }
    
}
void push1(car x)
{
     if(stackfull())
     {
         printf("停车位满\n");
     }
     else
     {
         inpark *p = (inpark *)malloc(sizeof(inpark));
	 p->data = x;
	 p->next = S;
	 S = p;
     }
}
void pop( )
{
     
     if(stackEmpty())
     {
         printf("车位为空\n");
     }
     else
     {
         inpark *p = S;
         S = S->next;
	 free(p);

     }
}
int get(car *x)
{
     if(stackEmpty())
     {
         return 0;
     }
     else
     {
         *x = S->data;
	 return 1;
     }
}


第二个栈temp_stack.h:

#include "stack1.h"
inpark *S2 = NULL;
int stackEmpty2()
{
     return S2==NULL;
}
int stackfull2()
{
     int i=0;
     inpark *p = S2;
     while(p)
     {
         i++;
	 p = p->next;
     }
     return i>9;
}
void push2(car x)
{
     if(stackfull2())
     {
         printf("停车位满\n");
     }
     else
     {
         inpark *p = (inpark *)malloc(sizeof(inpark));
	 p->data = x;
	 p->next = S2;
	 S2 = p;
     }
    
}
void pop2( )
{
     
     if(stackEmpty2())
     {
         printf("车位为空\n");
     }
     else
     {
         inpark *p = S2;
         S2 = S2->next;
	 free(p);

     }
}
int get2(car *x)
{
     if(stackEmpty2())
     {
         return 0;
     }
     else
     {
         *x = S2->data;
	 return 1;
     }
}


队列Queue.h:

typedef struct outpark
{
    int num;
    struct outpark *next;
}outpark;
int queueEmpty(outpark *Q)
{
     return Q==NULL;
}

outpark *inqueue(outpark *Q,int x)
{
     outpark *p = (outpark *)malloc(sizeof(outpark));
     outpark *q = Q;
     p->num = x;
 
        if(q!=NULL)
	{
         while((q->next)!=NULL)
	 {
	    q = q->next;
	 }
	 p->next = q->next;
	 q->next = p;
	}
	else
	{
	   p->next = q;
	   Q = p;
	}
         return Q;
}
outpark *dequeue(outpark *Q)
{
     outpark *p;
     if(queueEmpty(Q))
     {
        return Q;
     }
     p = Q;
     Q = Q->next;
     free(p);
     return Q;
}
int getqueue(outpark *Q,int *x)
{
     if(queueEmpty(Q))
     {
         return 0;
     }
     else
     {
         *x = Q->num;
	 return 1;
     }
}


 

 

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值