⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 停车场.cpp

📁 这是我数据结构课程设计的原代码,已经经过调试通过了的
💻 CPP
字号:
#include <stdio.h>
#include <stdlib.h>
#define maxsize 3
#define price 0.05
typedef struct 
{  
	int hour;
	int minute;
}time;
typedef struct 
{  	
	int num;	 	
	time arrtime;
}carnode;/*车信息(车牌号,到达时,分)结构体*/
typedef struct 
{	
	carnode stack[maxsize];
	int top;
}seqstackcar;/*模拟停车场的栈*/ 
typedef struct node
{ 	
	int num;	
	struct node *next;
}queuenode;
typedef struct 
{ 
	queuenode *front,*rear;
}linkqueuecar;/*模拟停车场外的便道的队列*/ 

void initseqstack(seqstackcar *s)
{ 	 s->top=-1;	}/*建一个为空的栈*/ 

int push(seqstackcar *s,carnode x)
{
    if(s->top==maxsize-1)
    {		printf("Error !");		return 0;		}
    else 
    {		 s->stack[++s->top]=x;		return 1;		}
}/*入栈 栈满时返回0 还可以入栈返回1*/ 

carnode pop(seqstackcar *s)
{
	carnode x;
 	if(s->top==-1)
  	{
   		x.num=0;
     	x.arrtime.hour=0;
      	x.arrtime.minute=0;
       	return x;
   	}
    else
    {
    	s->top--;
     	return s->stack[s->top+1];
    }
}/*栈空时返回x的信息 栈不为空时出栈*/

void initlinkqueue(linkqueuecar *q)
{
	q->front=(queuenode*)malloc(sizeof(queuenode));
 	if(q->front!=NULL)
  	{
   		q->rear=q->front;
     	q->front->next=NULL;
      	q->front->num=0;
   	}
}/*建一个空的队列*/

void enlinkqueue(linkqueuecar *q,int x)
{
	queuenode *p;
 	p=(queuenode*)malloc(sizeof(queuenode));
  	p->num=x;
   	p->next=NULL;
   	q->rear->next=p;
   	q->rear=p;
    q->front->num++;
}/*入队*/

int delinkqueue(linkqueuecar *q)
{
	queuenode *p;
    int n;
    if(q->front==q->rear)	 return 0;
    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;
    }
}/*队为空时返回0 队不为空时返回队头的车牌号*/

void arrive(seqstackcar *stop,linkqueuecar *lq,carnode x)
{
	int f;
 	f=push(stop,x);/*判断停车场上是否有位置*/
  	if(f==0)
   	{
  		enlinkqueue(lq,x.num);
    	printf("%d 号车停在便道 %d 号车位上 \n",x.num,lq->front->num);
    }/*没位置就停在便道上*/ 
    else
    {
    	printf("%d 车停在第 %d 号车位上\n",x.num,stop->top+1);
    }/*有位置就入停车场*/
}/*进停车场程序*/

void leave(seqstackcar *s1,seqstackcar *s2,linkqueuecar *p,carnode x)
{
	int n,f=0;
 	carnode y;
  	queuenode *q;
   	while(s1->top>-1 && f!=1)/*停车场有车时倒车直到轮到指定车*/
    {
   		y=pop(s1);
     	if(y.num!=x.num)	 
	 		n=push(s2,y);/*还没轮到知道指定车时,之前的车进辅助栈*/
    	else			
			f=1;
   	}
    if(y.num==x.num)/*若在停车场找到指定车*/
	{	 
		printf("第%d 号车收费 %2.1f 元\n",y.num,((x.arrtime.hour-y.arrtime.hour)
		*60+x.arrtime.minute-y.arrtime.minute)*price);
  		while(s2->top>-1)
    	{ 	
			y=pop(s2);		
			f=push(s1,y);		
		}/*辅助栈的车回停车场*/
  		n=delinkqueue(p);
    	if(n!=0)
     	{
      		y.num=n;
      		y.arrtime=x.arrtime;
        	f=push(s1,y);
         	printf("%d 号车停在第%d 号车位上\n",y.num,s1->top+1);
     	}/*若便道上有车 便道头的车进停车场*/
	}
	else/*若在停车场找不到指定车*/
 	{
  		while(s2->top>-1)
    	{	 
			y=pop(s2);
			f=push(s1,y);		
		}/*辅助栈的车回停车场*/
  		q=p->front;
    	f=0;
     	while(f==0 && q->next !=NULL)
      	{
       		if(q->next->num!=x.num)	 q=q->next;
         	else
          	{
           		q->next=q->next->next;
             	p->front->num--;
              	if(q->next==NULL)	 
			  		p->rear=p->front;
       			printf("%d号车离开便道 \n",x.num);
          		f=1;
         	}
       	}/*在便道上找指定车 找到并返回1*/
       if(f==0)
		    printf("Error;\n");/*在便道上也找不到显示error*/
  	}
}/*出停车场(+便道)程序*/

main()
{
	char ch;/*控制操作符*/
 	seqstackcar s1,s2;/*s1为模拟停车场的栈 s2为辅助栈*/
 	linkqueuecar p;/*模拟便道的队列*/
 	carnode x;/*车的信息*/
 	int flag;/*判断是否输入完的判断符*/
 	initseqstack(&s1);
 	initseqstack(&s2);
 	initlinkqueue(&p);
 	flag=1;
 	printf("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*欢迎使用停车场管理系统*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
	printf("\n使用说明:1.'A'表示到来一辆车'L'表示离开一辆车'E'表示输入完"); 
 	printf("\t\t\t\t  2.输入格式为:A,1253,8,27    输入完时直接按E即可");
	printf("\t\t\t\t  3.停车场每分钟收费0.05元 便道上不收费\n"); 
 	while(1)
  	{
   		printf("输入数据:'A','L','E',车牌号,到达/离开时间\n");
   		scanf("%c,%d,%d,%d",&ch,&x.num,&x.arrtime.hour,&x.arrtime.minute);
     	getchar();
      	switch(ch)
       	{
        	case 'A':arrive(&s1,&p,x); break;
         	case 'L':leave(&s1,&s2,&p,x);break;
          	case 'E':flag=0;printf("exit\n");break;
           	default:printf("Error\n");
       	}
        if(flag==0) break;
  	}                 
	system("PAUSE");
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -