【数据结构】 停车场管理优化

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
#include<windows.h>
#define SIZE 100
#define PRICE 10
typedef struct
{
 char ID[20];
 clock_t start;
}Nodestack;
typedef struct
{
 Nodestack* top;
 Nodestack* base;
 int size;
}SQStack;

typedef struct node
{
	char ID[20];
 struct node *next;
}node;
typedef struct
{
 node *front;
 node *rear;
}Linkqueue;
///////////////////////////////////////////////
void Initstack(SQStack &l)
{
 l.base=(Nodestack *)malloc(sizeof(Nodestack)*SIZE);
 l.top=l.base;
 l.size=SIZE; 
}
void push(SQStack &l,Nodestack p)
{
 *(l.top)=p;
 l.top++;
}
void popstack(SQStack &l ,Nodestack &a)
{
 a=*(--l.top);
}

void gettop(SQStack &l,char q[20])
{
 if(l.top!=l.base)
 { 
   Nodestack p;
   p=*(l.top-1);
   strcpy(q,p.ID);
  }
}
int stackempty(SQStack &l)
{//空返回1
 if(l.top==l.base) return 1;
 else return 0;
}
void traverstack(SQStack l)
{
 long temp_1;
 char temp_2[20];
 int i=0;
 Nodestack temp_3;
 if(stackempty(l)==1) printf("栈中无元素\n");
 else
 {
	 for(i=0;l.top-i!=l.base;i++)//非空时执行
	 {
      temp_3=*(l.top-i-1);
	  temp_1=temp_3.start;
	  strcpy(temp_2,temp_3.ID);
      printf("%s %ld\n",temp_2,temp_1);
	 }
 }
}
int cmpstackID(SQStack l,char b[20])
{
 Nodestack p;
 int i=0;
 if(stackempty(l)==0)
 {
  for(i=0;l.top-i!=l.base;i++)
  {
   p=*(l.top-i-1);
   if(strcmp(p.ID,b)==0)
	   return 1;
  }
 }
 return 0;
}
int lenghtstack(SQStack l)
{  
  int i=0;
  while(l.top-i!=l.base)
  {   
    i++;  
  }  
  return i; 
} 
/////////////////////////////////////
void Initqueue(Linkqueue &l)
//初始化队列
{
 l.front=l.rear=(node *)malloc(sizeof(node));
 if(!l.front)  exit(0);
 l.front->next=NULL;
}
int queueempty(Linkqueue &l)
//判断队列是否为空,空返回1,非空返回0
{
 if(l.front==l.rear) return 1;
 else return 0;
}
int lenghtqueue(Linkqueue &l)
//返回队列的长度
{
 int i=0;
 node *p;
 p=l.front->next;
 while(p!=NULL)
 {
	 i++;
     p=p->next;
 }

 return i;
}
void enqueue(Linkqueue &l,char e[20])
//对尾入队
{
 node *p;
 p=(node*)malloc(sizeof(node));
 strcpy(p->ID,e);
 l.rear->next=p;
 p->next=NULL;
 l.rear=p;
}

int dequeue(Linkqueue &l,char q[20])
//对头出队
{
 node *p;
 if(l.front==l.rear) return 0;
 p=l.front->next;
 strcpy(q,(*p).ID);
 l.front->next=p->next;
 if(p==l.rear) l.rear=l.front;
 free(p);
 return 0;
}
void traversequeue(Linkqueue &l)
//遍历队列
{
 node *p=l.front->next;
 if(queueempty(l)==1) printf("队列里没有元素");
 while(p!=NULL&&queueempty(l)==0)
 {
	 printf("%s   ",(*p).ID);
     p=p->next;
 }
}
int cmpqueueID(Linkqueue &l,char b[20])
{//queue中存在b返回1,否则返回0
 node *p;
 if(queueempty(l)==0)
 {
  p=l.front->next;
  while(p!=NULL&&queueempty(l)==0)
  {
   if(strcmp((*p).ID,b)==0)
	   return 1;
   p=p->next;
  }
 }
 return 0;
}
/////////////////////////////////////////////////////////////////////////////
void function1(SQStack &l,Linkqueue &s,int n)
{
  char temp[20];
  static int max=0;
  clock_t start;
  Nodestack nodes;
  printf("请输入需要停车的车牌号:\t");
  do
  {
  scanf("%s",&temp);
  getchar();
  if(cmpstackID(l,temp)==1||cmpqueueID(s,temp)==1)
  {
	  printf("该车已经停好,无需再停!");
	  Sleep(2000);
	  return ;
  }
  }while(cmpstackID(l,temp)==1||cmpqueueID(s,temp)==1);
  max=lenghtstack(l);
  if(max<n)
  {
   start=clock();
   strcpy(nodes.ID,temp);
   nodes.start=start;
   push(l,nodes); 
   printf("在停车场的第%d个位置,进入时间为:%ld",max+1,start);
  }
  else
  {
   start=clock();
   enqueue(s,temp); 
   printf("在便道的第%d个位置,进入时间为:%ld",lenghtqueue(s),start);
  }
 Sleep(2000);
}
////////////////////////////////////////////////////////////////////////////
int function2(SQStack &l,Linkqueue &s,SQStack &m)
{
 char car_id[20],temp_1[20];
 double staytime;
 Nodestack temp_2,del;
 clock_t start,finish;
 printf("请输入离开车辆的车牌号:");
 scanf("%s",&car_id);
 getchar();
 gettop(l,temp_1);
 while(strcmp(temp_1,car_id)!=0)
 {
  if(stackempty(l)==1) 
  {
	  printf("不存在车牌号为%s的车辆",car_id);
	  while(stackempty(m)==0)
	  {
        popstack(m,temp_2);
        push(l,temp_2);
	  }
	  Sleep(2000);
	  return 1;
  }
  else
  {
   popstack(l,temp_2);
   push(m,temp_2);
   gettop(l,temp_1);
  }//else
 }//while
 if(strcmp(temp_1,car_id)==0)
 {
  popstack(l,del);
  while(stackempty(m)==0)
  {
   popstack(m,temp_2);
   push(l,temp_2);
  }
  if(queueempty(s)==0)
   {
    dequeue(s,temp_1);
    start=clock();
    strcpy(temp_2.ID,temp_1);
    temp_2.start=start;
    push(l,temp_2);
   }
  finish=clock();
  staytime=(double)(finish-del.start)/CLOCKS_PER_SEC;
  printf("车牌号:%s\t停留时长:%f sec\t应付款:¥%f\t",del.ID,staytime,staytime*PRICE );
 }
 Sleep(2000);
 return 0;
}
////////////////////////////////////////////////
void function3(SQStack &l,Linkqueue &s)
{
	printf("便道上的车辆:\n");
    traversequeue(s);
	printf("\n");
	printf("停车道上的车辆:\n");
	printf("车牌号 停车时间(相对系统启动)\n");
	traverstack(l);
	printf("\n");
    Sleep(2000);
}
////////////////////////////////////////////////////////////////////////
void meun()
{
 system("cls");
 printf("\t*************************\n");
 printf("\t*      停车场管理目录   *\n");
 printf("\t*  A: 停车              *\n");
 printf("\t*  D: 离开              *\n");
 printf("\t*  C: 查看车场情况      *\n");
 printf("\t*  E: 退出系统          *\n");
 printf("\t*************************\n");
}
//////////////////////////////////////////////////////////////////////////
void main()
{
 SQStack stack1,stack2;
 Linkqueue queue;
 
 int n;//车库的容量
 char choice;
 Initstack(stack1);
 Initstack(stack2);
 Initqueue(queue);

 printf("请输入停车场的容量:\t");
 scanf("%d",&n);
 getchar(); 
 while(1)
 {
  meun();
  scanf("%c",&choice);
  getchar();
  switch(choice)
  {
    case 'a':
    case 'A':function1(stack1,queue,n); break;
	case 'd':
    case 'D':function2(stack1,queue,stack2); break;
	case 'c':
	case 'C':function3(stack1,queue);break;
	case 'e':
	case 'E': printf("欢迎下次使用!\n");
		      exit(0);
    default:;
  }   
 }
}

严格按照严蔚敏数据结构c语言版编写 修改了上一程序的弊端

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