1. 程式人生 > >用連結串列,棧,佇列實現簡單的停車場專案

用連結串列,棧,佇列實現簡單的停車場專案

學習了連結串列,棧,佇列之後寫了一個停車場專案,但是功能還不是太完善,時間忘了插入,通過諮詢別人,完善了時間函式,完成了這個專案。

問題描述:停車場是一個能放 n 輛車的狹長通道,只有一個大門,汽車按到達的先後次序停放。若車場滿了,車要停在門外的便道上等候,一旦有車走,則便道上第一輛車進入。當停車場中的車離開時,由於通道窄,在它後面的車 要先退出,待它走後在依次進入。汽車離開時按停放時間收費。
基本功能要求:
          (1)建立三個資料結構分別是:停放棧、讓路棧、等候佇列。
          (2)輸入資料模擬管理過程,資料(入或出,車號)
功能描述:進車登記、出車登記、按車牌號查詢停車車輛資訊、查詢出入車記錄、查詢場內車輛資訊、查詢等候車輛資訊、退出系統。
          (1)linux系統編寫(連結串列、棧、佇列);

          (2)進車登記:登記車牌號以及入場時間;

          (3)出車登記:計算出停車時間,記錄車輛車牌;

  (4)按車牌號查詢車輛資訊:停車時間,是否來過停車場,是否還在停車場

          (5)查詢出入記錄:所有車輛,包括已經離開的

          (6)查詢場內車輛資訊:列出所有場內車輛資訊

  (7)查詢等候車輛資訊:顯示等候車輛數量以及所有車牌號

          (8)退出系統。

標頭檔案
#ifndef _PARK_H_
#define _PARK_H_

#define MAXSIZE   3
#define FAILURE   10001

struct node
{
	char id[20];
	int month,day,hour,min,sec;
	time_t start,end;
	char info[10];
	struct node *next;
};
typedef struct node Node;
typedef Node *LinkNode;

struct stack
{
	LinkNode top;
	int count;
};
typedef struct stack Stack;

struct queue
{
	LinkNode front;
	LinkNode rear;
	int flag;
};
typedef struct queue Queue;

int settime(LinkNode n,time_t t);
int timecopy(LinkNode n, LinkNode p);
int InitStack(Stack *L);
int InitQueue(Queue *S);
int EnQueue(Queue *S, LinkNode i);
int EnStack(Stack *L, LinkNode i);
int EnCar(Stack *L, Queue *S, Queue *T);
int OutCar(Stack *L, Stack *P, Queue *S, Queue*T);
int Encar2(Stack *L, LinkNode S, Queue *T);
int SearchInfo(Stack L, Queue T);
int ShowInfo(Queue T);
int EnInfo(Stack L);
int WaitInfo(Queue S);

#endif

介面函式
#include <stdio.h>
#include <stdlib.h>
#include "park.h"
#include <time.h>
#include <string.h>

void show()
{
	system("clear");
	printf("********************************************\n\n");
	printf("*******   WELCOME TO PARKING SYSTEM  *******\n\n");
	printf("********************************************\n\n");
	sleep(1);
	system("clear");
}

void PrintInfo()
{
	printf("***************************************************\n\n");
	printf("*                 1.進車登記                      *\n");
	printf("*                 2.出車登記                      *\n");
	printf("*                 3.按車牌號查詢車輛資訊          *\n");
	printf("*                 4.查詢出入記錄                  *\n");
	printf("*                 5.查詢場內車輛資訊              *\n");
	printf("*                 6.查詢等候車輛資訊              *\n");
	printf("*                 7.退出                          *\n");
	printf("          收費標準:每秒2元!車在候車場不收費!       \n\n");
	printf("***************************************************\n\n");
	printf("請輸入你的選擇 :\n");
}

int settime(LinkNode n,time_t t)
{
 
	struct tm *pt;
	time(&t);
	pt = localtime(&t);
	n->month = pt->tm_mon+1;
	n->day = pt->tm_mday;
	n->hour = pt->tm_hour;
	n->min = pt->tm_min;
	n->sec = pt->tm_sec;
}

int timecopy(LinkNode n, LinkNode p)
{
	n->start = p->start;
	n->month = p->month;
	n->day = p->day;
	n->hour = p->hour;
	n->min = p->min;
	n->sec = p->sec;
}

int InitStack(Stack *L)
{
	L->top = NULL;
	L->count = 0;

}

int InitQueue(Queue *S)
{
	S->front = (LinkNode)malloc(sizeof(Node));
	S->rear = S->front;
	S->rear->next = NULL;
	S->flag = 0;
}

int EnQueue(Queue *S,LinkNode i)
{
	S->rear->next = i;
	S->rear = i;
	i->next = NULL;
	S->flag++;
}

int EnStack(Stack *L, LinkNode i)
{
	i->next = L->top;
	L->top = i;
	L->count++;
}


int EnCar(Stack *L, Queue *S, Queue *T)
{
	time_t t;
	struct tm *pt;
	LinkNode n = (LinkNode)malloc(sizeof(Node));
	LinkNode m = (LinkNode)malloc(sizeof(Node));
	printf("請輸入你的車牌號:\n");
	scanf("%s",n->id);

	if(L->count >= MAXSIZE)
	{
		printf("停車場已滿,請進入候車場等待!\n");
		EnQueue(S,n);
		return FAILURE;
	}

	time(&t);
	n->start = t;
	pt = localtime(&t);

    settime(n,t);
	printf("停車時間是:%d月%d日%d:%d:%d\n",n->month,n->day,n->hour,n->min,n->sec);
	
	timecopy(m,n);
	strcpy(m->info,"Parking!");
	strcpy(m->id, n->id);
	EnQueue(T,m);
	EnStack(L,n);
	printf("停車成功!\n");

}

int OutCar(Stack *L, Stack *P, Queue *S,Queue *T)
{
	long int ti;
	LinkNode m = (LinkNode)malloc(sizeof(Node));
	time_t t;
	struct tm *pt;
	char i[20];
	printf("請輸入你要開走的車牌號:\n");
	scanf("%s",i);

	while(L->top)
	{
		LinkNode p= L->top;
		if(strcmp((p->id),i) == 0)
		{
			break;	
		}
		L->top = p->next;
		EnStack(P,p);
	}

	  if(L->top == NULL)
	{
		while(P->top)
		{
			LinkNode k = P->top;
			P->top = k->next;
			EnStack(L,k);
		}
		printf("此車不在此停車場!\n");
		return 0;
	}

	LinkNode l = L->top;

	time(&t);
	l->end = t;
	pt = localtime(&t);

	settime(l,t);
	ti = l->end - l->start;
	printf("|*****************************|\n");
	printf("|            賬單             |\n");
	printf("|恭喜你成功開走了這輛車!     |\n");
	printf("|離開時間是:%d月%d日%d:%d:%d! |\n",l->month,l->day,l->hour,l->min,l->sec);
	printf("|停車時長:%d s!              |\n", ti);
	printf("|停車費用:%d元!              |\n", ti*2);
	printf("|*****************************|\n");

	timecopy(m,l);
	strcpy(m->info,"Leave! ");
	strcpy(m->id, l->id);
	EnQueue(T,m);
	L->top = l->next;
	free(l);
	L->count--;
	
	while(P->top)
	{
		LinkNode k = P->top;
		P->top = k->next;
		EnStack(L,k);
	}

	if((S->flag) > 1)
	{
		printf("候車場車牌為%s的車可以停車!\n",S->front->next->id);
		LinkNode j = S->front->next;
		S->front->next = j->next;
		Encar2(L,j,T);
		S->flag--;
		return;

	}
	else if(S->flag == 1)
	{

		printf("候車場車牌為%s的車可以停車!\n",S->front->next->id);
		LinkNode j = S->front->next;
		S->front->next = j->next;
		S->rear = S->front;
		Encar2(L,j,T);
		S->flag--;
		return 0;
	}
	else 
		return;


}

int Encar2(Stack *L, LinkNode S, Queue *T)
{

	LinkNode m = (LinkNode)malloc(sizeof(Node));
	time_t t;
	struct tm *pt;

	time(&t);
	S->start = t;
	pt = localtime(&t);

	settime(S,t);
	printf("停車時間是:%d月%d日%d:%d:%d\n",S->month,S->day,S->hour,S->min,S->sec);
	
	timecopy(m,S);
	strcpy(m->info,"Parking!");
	strcpy(m->id, S->id);
	EnQueue(T,m);
	EnStack(L,S);
	printf("停車成功!\n");
}

int SearchInfo(Stack L, Queue T)
{
	char n[10];
	printf("請輸入你想查詢的車牌號:\n");
	scanf("%s",n);
	
	
	LinkNode p = T.front->next;
	while(p)
	{
		if(strcmp(p->id,n) == 0)
		{
			break;
		}
		p = p->next;
	}

	if(p == NULL)
	{
		printf("沒有此車!\n");
		return;
	}

	LinkNode l = p;
	printf("車牌號:%s 狀態:%s 時間:%d月%d日%d:%d:%d\n",l->id,l->info,l->month,l->day,l->hour,l->min,l->sec);
	
	p = p->next;	
	if(p != NULL)
	{
			
		while(p)
	
		{
			if(strcmp(p->id,n) == 0)
			{
				printf("車牌號:%s 狀態:%s 時間:%d月%d日%d:%d:%d\n",p->id,p->info,p->month,p->day,p->hour,p->min,p->sec);
				break;
			}
			p = p->next;
		}
		
	}


	LinkNode q = L.top;
	while(q)
	{
		if(strcmp(q->id,n) == 0)
		{
			printf("車還在停車場中!\n");
			break;
		}
		q = q->next;
	}

	if(q == NULL)
	{
		printf("車不在停車場中!\n");
	}

}

int ShowInfo(Queue T)
{
	if(T.front->next == NULL)
	{
		printf("沒有車輛資訊可以查詢!\n");
		return;
	}
	
	LinkNode q = T.front->next;
	
	while(q)
	{
		printf("車牌號:%s 狀態:%s 時間:%d月%d日%d:%d:%d\n",q->id,q->info,q->month,q->day,q->hour,q->min,q->sec);
		q = q->next;
	}

}

int EnInfo(Stack L)
{
	LinkNode p = L.top;

	while(p != NULL)
	{
		printf("車牌號:%s 停車時間:%d月%d日%d:%d:%d\n",p->id,p->month,p->day,p->hour,p->min,p->sec);
		p = p->next;
	}
}

int WaitInfo(Queue S)
{
	printf("現在有%d輛車在等待!\n",S.flag);

	if(S.flag == 0)
	{
		return 0;
	}
	LinkNode p = S.front;
	
	while(p->next != NULL)
	{
		printf("車牌號:%s\n",p->next->id);
		p = p->next;
	}
}

主函式
#include <stdio.h>
#include <stdlib.h>
#include "park.h"
int main()
{
	Stack in,tmp;
	Queue wait,record;
	
	InitQueue(&record);
	InitStack(&in);
	InitStack(&tmp);
	InitQueue(&wait);

	char choice[8] = {0};
	
	show();
	
	while (1)
	{
		PrintInfo();
		
		scanf("%s", choice);

		switch (atoi(&choice[0]))
		{
			case 1:
				printf("1.進車登記\n");
				EnCar(&in,&wait,&record);
				break;
			case 2:
				printf("2.出車登記\n");
				OutCar(&in,&tmp,&wait,&record);
				printf("***********************************************\n\n");
				break;
			case 3:
				printf("3.按車牌號查詢車輛資訊\n");
				SearchInfo(in,record);
				printf("***********************************************\n\n");
				break;
			case 4:
				printf("4.查詢出入記錄\n");
				ShowInfo(record);
				printf("***********************************************\n\n");
				break;
			case 5:
				printf("5.查詢場內車輛資訊\n");
				EnInfo(in);
				printf("***********************************************\n\n");
				break;
			case 6:
				printf("6.查詢等候車輛資訊\n");
				WaitInfo(wait);
				printf("***********************************************\n\n");
				break;
			case 7:
				exit(1);
				break;
			default:
				printf("ERROR!\n");
				break;
		}
	}

	return 0;	
}