【數據結構】 停車場管理優化

#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語言版編寫 修改了上一程序的弊端


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章