【數據結構】關於停車場的管理

#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;
 clock_t start;
}Nodestack;
typedef struct
{
 Nodestack* top;
 Nodestack* base;
 int size;
}SQStack;
typedef struct node
{
 char data;
 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);
}

char gettop(SQStack &l)
{
 if(l.top!=l.base)
 { 
   Nodestack p;
   p=*(l.top-1);
   return p.ID;
  }
 return 0;
}
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;
 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;
	  temp_2=temp_3.ID;
      printf("%c %ld\n",temp_2,temp_1);
	 }
 }
}
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)
//對尾入棧
{
 node *p;
 p=(node*)malloc(sizeof(node));
 p->data=e;
 l.rear->next=p;
 p->next=NULL;
 l.rear=p;
}

char dequeue(Linkqueue &l)
//對頭出棧
{
 node *p;
 if(l.front==l.rear) return 0;
 char q;
 p=l.front->next;
 q=(*p).data;
 l.front->next=p->next;
 if(p==l.rear) l.front=l.rear;
 free(p);
 return q;
}
void traversequeue(Linkqueue &l)
//遍歷隊列
{
 node *p=l.front->next;
 if(queueempty(l)==1) printf("隊列裏沒有元素");
 while(p!=NULL&&queueempty(l)==0)
 {
	 printf("%c",(*p).data);
     p=p->next;
 }
}
/////////////////////////////////////////////////////////////////////////////
void function1(SQStack &l,Linkqueue &s,int n)
{
  char temp;
  static int max=0;
  clock_t start;
  Nodestack nodes;
  //node nodeq;
  printf("請輸入需要停車的車牌號:\t");
  scanf("%c",&temp);
  getchar();
  max=lenghtstack(l);
  if(max<n)
  {
   start=clock();
   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,temp_1;
 double staytime;
 Nodestack temp_2,del;
 clock_t start,finish;
 printf("請輸入離開車輛的車牌號:");
 scanf("%c",&car_id);
 getchar();
 temp_1=gettop(l);
 while(temp_1!=car_id)
 {
  if(stackempty(l)==1) 
  {
	  printf("不存在車牌號爲%c的車輛",car_id);
	  return 0;
  }
  else
  {
   popstack(l,temp_2);
   push(m,temp_2);
   temp_1=gettop(l);
  }//else
 }//while
 if(temp_1==car_id)
 {
  popstack(l,del);
  while(stackempty(m)==0)
  {
   popstack(m,temp_2);
   push(l,temp_2);
  }
  if(queueempty(s)==0)
   {
    temp_1=dequeue(s);
    start=clock();
    temp_2.ID=temp_1;
    temp_2.start=start;
    push(l,temp_2);
   }
  finish=clock();
  staytime=(double)(finish-del.start)/CLOCKS_PER_SEC;
  printf("車牌號:%c\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");
	traverstack(l);
	printf("\n");
    Sleep(2000);
}
////////////////////////////////////
void meun()
{
 system("cls");
 printf("\t\t停車場管理目錄\n");
 printf("\tA: 停車\n");
 printf("\tD: 離開\n");
 printf("\tC: 查看車場情況\n");
 printf("\tE: 退出系統\n");
}
///////////////////////////////////////////////////////////////////////////
void main()
{
 SQStack stack1,stack2;
 Linkqueue queue;
 
 int n;//車庫的容量
 char choice;
 Initstack(stack1);
 Initstack(stack2);
 Initqueue(queue);

 printf("請輸入停車場的容量:\t");
 scanf("%d",&n);
 getchar();
 meun();
 scanf("%c",&choice);
 getchar();
 while(choice!='E')
 {
  switch(choice)
  {
    case 'A':function1(stack1,queue,n); break;
    case 'D':function2(stack1,queue,stack2); break;
	case 'C':function3(stack1,queue);break;
    default:;
  }
  if(choice=='E')
	   printf("歡迎下次使用!\n");
   meun();
   scanf("%c",&choice);
   getchar();
 }
}
弊端是輸入的車牌號只是一位字符,可以做適當的優化
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章