C实现停车场管理

/***************************************************************************
项目要求
停车场管理
问题描述:停车场是一个能放n辆车的狭长通道,
只有一个大门,汽车按到达的先后次序停放。若
车场满了,车要停在门外的便道上等候,一旦有
车走,则便道上第一辆车进入。当停车场中的车
离开时,由于通道窄,在它后面的车要先退出,
待它走后再依次进入。汽车离开时按停放时间收费。
基本功能要求:
(1) 建立三个数据结构分别是:停放栈、让路
栈、等候队列。
(2) 输入数据模拟管理过程,数据(入或出,车号)。
***************************************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<time.h>   
#define D (24*60*60)  
#define H (60*60)  
#define M (60)
#define OK 1
#define ERROR 0
#define MAX_STACK_SIZE 10      /*  栈向量大小  */
typedef int StackData;
typedef int QueueData;
typedef int ElemType;
typedef struct Node
{
    int No;             /*  车号  */
	int Timeinit;		  /*  进入停车场的时间*/
}Node;
typedef struct QueueNode    /*  队列结点*/
{
    struct Node data;	     
    struct QueueNode* next;	      
} QueueNode;
typedef struct LinkQueue    /*  链式队列结构体 */
{
    struct QueueNode *rear, *front;
} LinkQueue;


typedef struct SqStackNode   /*  链式栈结构体 */
{  
    int top;
	int bottom;
    struct Node stack_array[MAX_STACK_SIZE+1] ;
}SqStackNode ;

//***************************************************************
SqStackNode* InitStack()                          /*   初始化栈*/
{    
	SqStackNode *S=(SqStackNode *)malloc(sizeof(SqStackNode));
    S->bottom=S->top=0;  
	return (S);
}
int FullStack(SqStackNode *S)                    /* 满栈  */
{
    return S->top==MAX_STACK_SIZE;
}
int pushStack(SqStackNode *S,Node data)       /* 入栈  */
{  
    if(FullStack(S))
    {
		return	ERROR;			      /*  栈满,返回错误标志	 */
    }
    S->top++ ;                     
    (S->stack_array[S->top]).No=data.No  ;       
    (S->stack_array[S->top]).Timeinit=data.Timeinit;      
    return OK;                        /*  压栈成功    */
}
int popStack(SqStackNode *S,Node *data)               /*弹出栈顶元素*/
{  
    if(S->top==0)
    {
		return ERROR;		          /*  栈空,返回错误标志    */
    }
    (*data).No=(S->stack_array[S->top]).No; 
    (*data).Timeinit=(S->stack_array[S->top]).Timeinit; 
    S->top--;  
    return OK;  
}
int FinfStack(SqStackNode *S,Node data)    /*   搜索栈内元素data*/
{
    int i;
    if(S->top==0)
    {
	 	return ERROR;		          /*  栈空,返回错误标志    */
	}
	
	for(i=1;i<=S->top;i++)
	{
	    if(S->stack_array[i].No == data.No)
	    {
	        return OK;
	    }
	}
	return ERROR;	 
}



//**************************************************** 
LinkQueue* InitQueue (void)           /*  初始化队列 */
{
    LinkQueue *Q=( LinkQueue * ) malloc( sizeof ( LinkQueue ) );
    Q->rear=Q->front=NULL;
	return Q;
}
 int QueueEmpty ( LinkQueue *Q )    /*   空队列*/
 {
    return Q->front == NULL;
}

int GetFrontQueue ( LinkQueue *Q, Node *data )  /* 取队首  */
{
    if ( QueueEmpty (Q) ) return 0;		
    (*data).No = (Q->front->data).Timeinit;  return 1;		
}
int EnQueue ( LinkQueue **Q, Node data)    /*   入队*/
{
    QueueNode *p = ( QueueNode * ) malloc( sizeof ( QueueNode ) );
    (p->data).No = data.No; 
    (p->data).Timeinit = data.Timeinit; 
	p->next = NULL;
    if ( (*Q)->front == NULL ) 
    {
		(*Q)->front = (*Q)->rear = p;
    }
    else
	{
	    
		(*Q)->rear = (*Q)->rear->next = p;
	}
    return 1;
}
int DeQueue ( LinkQueue **Q, Node *data)   /*   出对*/
{
    if ( QueueEmpty (*Q) ) 
	{
	    return 0;	
	}
    QueueNode *p = (*Q)->front;		
    (*data).No = p->data.No;                       
    (*data).Timeinit = p->data.Timeinit;  
    (*Q)->front = (*Q)->front->next; 
    if ((*Q)->front == NULL) (*Q)->rear = NULL;
    free (p);
    return 1;				
}
/*********************************************************/
int now_time(void)   /*   获取当日时间,单位秒*/
{  
    time_t t1;   
    time(&t1);   
    int time=t1%D;  
    return time;  
} 

Parking(LinkQueue **Q,SqStackNode *S) /*   停车*/
{
    int i,time_now;
	Node data;
	printf("Input the Car No:\n");
	scanf(" %d",&data.No);

	for(i=1;i<=S->top;i++)
	{
		
		if(S->stack_array[i].No == data.No)/*   车号已存在*/
		{
		    printf("The Car is existed\n");
			return ;
		}
	}

	EnQueue(Q,data);/*   进去等待队列*/
	while(!QueueEmpty(*Q))
	{
	    if(FullStack(S))  /*   停放栈满*/
	    {
			printf("Please Wait...\n");
	        break;
	    }
		else /*  停放栈未满 */
		{
		    DeQueue(Q,&data);/*  等待队列车出对 */
			data.Timeinit=now_time();/*   记录当前时间*/
			pushStack(S,data);/*   进入停放栈*/
			printf("Park Success\n");
		}
	}
	return ;
}
leaving(SqStackNode *S,SqStackNode *B,LinkQueue **Q)/*   离开*/
{
    if(S->bottom == S->top)/*   停放栈空*/
    {
        printf("Parking is Empty:\n");
    }
	else
	{
	    Node data;
        int i,h,m,s;
		float charge; 
		int time_now,parking_time;
		printf("Leaving No:\n");
		scanf(" %d",&i);
	    data.No=i;
		if(!FinfStack(S,data))/*   停放栈内无此车*/
		{
		    printf("Do not find the car\n");
			return ;
		}
		else/*   停放栈内有此车*/
		{
			while(S->stack_array[S->top].No != i)/*   此车后的车依次出栈入让路栈*/
			{
				popStack(S,&data);
				pushStack(B,data);
			}
			popStack(S,&data);/*   此车出停放栈*/
			time_now=now_time();
			parking_time=time_now-data.Timeinit;/*   计算停车时间*/
			
			h = parking_time/H;
	    	parking_time = parking_time%H;
	    	m = parking_time/M;
	    	s = parking_time%M;
			charge = 6*h+0.1*(m+1);/*   计算停车收费*/
			printf("The leaving car:%d   Parking time:%d:%d:%d   Charge($6/h):$%g\n",data.No,h,m,s,charge);
			
			while(B->bottom != B->top)/*   让路栈内的车依次出栈入停放栈*/
			{
				popStack(B,&data);
				pushStack(S,data);
			}
			while(!FullStack(S)&&(!QueueEmpty(*Q)))/*   停放栈未满且等待队列未空*/
			{
				DeQueue(Q,&data); /*   等待队列车出队*/
				data.Timeinit=now_time();
				pushStack(S,data);/*   出队的车入停放栈*/
			}	
		}
	
	}
}
situation(SqStackNode *S,LinkQueue **Q)/*   查看停车场当前情况*/
{
    Node data;
    int i;
    int time_now,parking_time;
    int h,m,s;
    struct QueueNode *p;
	int wait_count=0;
	p=(*Q)->front;
	if(p == NULL)/*   等待队列空*/
	{
	    printf("Waiting car :0\n");
	}
	else/*   等待队列未空*/
	{
	    do
	    {
	        wait_count++;
			p=p->next;
	    }while(p!=NULL);/*   计算等待队列内车数*/
		printf("Waiting car :%d\n",wait_count);
	}
	
	printf("Car No:   ");
	for(i=1;i<=S->top;i++)
	{
	    printf("%-10d",S->stack_array[i].No);
	    
	    if(S->stack_array[i].No == data.No)
	    {
	        return OK;
	    }
	}
	printf("\nPark time:");
	for(i=1;i<=S->top;i++)
	{
	    time_now = now_time();
		parking_time = time_now - S->stack_array[i].Timeinit;/*   计算截止当前停车时间*/
		h = parking_time/H;
		parking_time = parking_time%H;
		m = parking_time/M;
		s = parking_time%M;
		printf("%02d:%02d:%02d  ",h,m,s);
	}
	printf("\n");

}

int main()
{
    int i;
	Node data;
	SqStackNode *park;/*   停放栈*/
	SqStackNode *back;/*   让路栈*/
    LinkQueue *wait;  /*   等待队列*/
	park=InitStack();
	back=InitStack();
	wait=InitQueue();
	while(1)
	{
		system("clear\n");
		printf("----------Welcome to our Car Parking----------\n");
		printf("                1.Parking   \n");
		printf("                2.leaving   \n");
		printf("                3.situation \n");
		printf("                4.exit      \n");
		scanf(" %d",&i);
		switch(i)
		{
			case 1:/*   停车*/
				{
					system("clear\n");
					Parking(&wait,park);
					setbuf(stdin,NULL);
					getchar();
					break;
				}
			case 2:/* 离开 */
			    {
					leaving(park,back,&wait);
					setbuf(stdin,NULL);
					getchar();
					break;
				}
			case 3:/*   查看停车情况*/
				{
					
					system("clear\n");
					situation(park,&wait);
					setbuf(stdin,NULL);
					getchar();
					break;
				}
			case 4:/*   退出*/
				{
					return 0;
				}
			default:
				{
					break;
				}
		}
	}
	return 0;	
}


    原文作者:停车场模拟问题
    原文地址: https://blog.csdn.net/ccj2020/article/details/7771332
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞