1. 程式人生 > >【嵌入式系統學習記錄】小專案:停車場管理系統的體會

【嵌入式系統學習記錄】小專案:停車場管理系統的體會

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

  (8)退出系統。

#ifndef _PARK_H
#define _PARK_H

#define TRUE 10000
#define FALSE 10001

#define P_MAX 3
#define W_MAX 3
#define PRICE 2


struct car
{
	char *carnum;
	char *timein;
	char *timeout;
	int InsertSeconds;
	int total;
	struct car *next;
};
typedef struct car Car;

struct park
{
	Car *top;
	int length;
};
typedef struct park Park;

struct waitcar
{
	Car *front;
	Car *rear;
	int length;
};
typedef struct waitcar Wait;

void Menu();
void Init(Park **p, Park **r, Wait **w, Park **h);
int EmptyStack(Park *p);
int FullStack(Park *p);
int EnterStack(Park *p, Car *c);
Car *PopStack(Park *p);
int TraverStackP(Park *p);
int TraverStackH(Park *p);
int EmptyQueue(Wait *w);
int FullQueue(Wait *w);
Car *PopQueue(Wait *w);
int TraverQueue(Wait *w);
char *now_time(void);
int Seconds();
int InsertCar(Park *p, Wait *w);
int PopCar(Park *p,Park *r, Wait *w, Park *h);
int QueryCar(Park *p, Wait *w, Park *h);
int ListAll(Park *p, Wait *w, Park *h);
#endif
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include<string.h>
#include"park.h"

void Menu()
{
	printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
	printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
	printf("~~~~~~~~~~~~~~~~~~~歡迎使用停車管理系統~~~~~~~~~~~~~~~~~~\n");
	printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
	printf("~~~~~~~~~~~~~~~1、停入車輛     2、車輛離開~~~~~~~~~~~~~~~\n");
	printf("~~~~~~~~~~~~~~~3、查詢車輛資訊 4、顯示所有車輛~~~~~~~~~~~\n");
	printf("~~~~~~~~~~~~~~~5、顯示在場車輛 6、顯示等待中的車輛~~~~~~~\n");
	printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
	printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
	printf("~~~~~~~~~~~~~~~~~~~~~~7、退出系統~~~~~~~~~~~~~~~~~~~~~~~~\n");
	printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");

}

void Init(Park **p, Park **r, Wait **w, Park **h)
{
	(*p) = (Park *)malloc(sizeof(Park));
	(*r) = (Park *)malloc(sizeof(Park));
	(*h) = (Park *)malloc(sizeof(Park));
	(*w) = (Wait *)malloc(sizeof(Wait));

	if(NULL == (*p) || NULL == (*r) || NULL == (*w) || NULL == (*h))
	{
		printf("malloc failure!\n");
	}

	(*p) -> top = NULL;
	(*r) -> top = NULL;
	(*h) -> top = NULL;
	(*p) -> length = 0;
	(*r) -> length = 0;
	(*h) -> length = 0;

	Car *wn = (Car *)malloc(sizeof(Car));
	if(NULL == wn)
	{
		printf("ERROR!\n");
	}
	wn -> next = NULL;
	(*w) -> rear = (*w) -> front = wn;
	(*w) -> length = 0;
}

int EmptyStack(Park *p)
{
	if(NULL == p)
	{
		return FALSE;
	}
	return(NULL == p -> top)? TRUE: FALSE;
}

int FullStack(Park *p)
{
	if(NULL == p)
	{
		return FALSE;
	}
	if(p -> length == P_MAX)
	{
		return TRUE;
	}
	else
	{
		return FALSE;
	}
}

int EnterStack(Park *p, Car *c)
{
	if(NULL == p)
	{
		return FALSE;
	}
	Car *t = (Car *)malloc(sizeof(Car));
	if (NULL == t)
	{
		return FALSE;
	}
	t = c;
	t -> next = p -> top;
	p -> top = t;
	p -> length++;
}

Car *PopStack(Park *p)
{
	if(NULL == p)
	{
		printf("ERROR\n");
	}
	Car *tmp = p -> top;
	p -> top = tmp -> next;
	p -> length--;
	return tmp;
}

int TraverStackP(Park *p)
{
	if(NULL == p)
	{
		return FALSE;
	}
	Car *c = p -> top;
	while(c)
	{
		printf("車牌號%s 入場時間為%s 正在計時\n", c -> carnum, c -> timein);
		c = c -> next;
	}
	printf("\n");
}

int TraverStackH(Park *p)
{
	if(NULL == p)
	{
		return FALSE;
	}
	Car *c = p -> top;
	while(c)
	{
		printf("車牌號%s 入場時間為%s 離開時間為%s 費用為%d\n", c -> carnum, c -> timein, c -> timeout, c -> total);
		c = c -> next;
	}
	printf("\n");
}
int EmptyQueue(Wait *w)
{
	if(NULL == w)
	{
		return FALSE;
	}
	return(w -> length == 0)? TRUE: FALSE;
	
}
int FullQueue(Wait *w)
{
	if(NULL == w)
	{
		return FALSE;
	}
	if(w -> length == W_MAX)
	{
		return TRUE;
	}
	else
	{
		return FALSE;
	}
}

Car *PopQueue(Wait *w)
{
	if(NULL == w || w -> front == w -> rear)
	{
		printf("ERROR\n");
	}

	Car *tmp = w -> front -> next;
	w -> front -> next = tmp -> next;
	if (tmp == w -> rear)
	{
		w -> rear = w -> front;
	}
	return tmp;
}

int TraverQueue(Wait *w)
{
	if(NULL == w)
	{
		return FALSE;
	}
	Car *c = w -> front ->next;
	while(c)
	{
		printf("車牌%s正在等待入棧\n", c -> carnum);
		c = c -> next;
	}
	printf("\n");
}

char *now_time()
{
	time_t timep;
	time(&timep);
	return ctime(&timep);
}

int Seconds()
{
	time_t timep;
	return time(&timep);
}

int InsertCar(Park *p, Wait *w)
{
	if(NULL == p || NULL == w)
	{
		printf("error!\n");
	}
	if(FullStack(p) == TRUE)
	{
		if(FullQueue(w) == TRUE)
		{
			printf("等候區已滿!\n");
		}
		else
		{
			Car *c = (Car *)malloc(sizeof(Car));
			c -> carnum = (char *)malloc(sizeof(char) * 8) ;
			if(NULL == c)
			{
				return FALSE;
			}
			printf("請輸入車牌之後進入等待佇列:\n");
			scanf("%s", c -> carnum);
			c -> next = NULL;
			w -> rear -> next = c;
			w -> rear = c;
			w -> length++;
			printf("已進入等待佇列!\n");
		}
		printf("車庫已滿!\n");
	}
	else
	{
		Car *c = (Car *)malloc(sizeof(Car));
		c -> carnum = (char *)malloc(sizeof(char) * 8) ;
		c -> timein = (char *)malloc(sizeof(char) * 30) ;
		c -> timeout = (char *)malloc(sizeof(char) * 30) ;
		if(NULL == c)
		{
			return FALSE;
		}

		printf("請輸入車牌號:\n");
		scanf("%s", c -> carnum);
		
		strcpy(c -> timein, now_time());
		c -> InsertSeconds = Seconds();
		c -> total = 0;
		c -> next = p -> top;
		p -> top = c;
		p -> length++;
		printf("%s已經成功進入停車棧\n",c -> carnum);
	}
}

int PopCar(Park *p, Park *r, Wait *w, Park *h)
{
	if(NULL == p || NULL == r || NULL == w || NULL == h)
	{
		return FALSE;
	}
	char *goalnum = (char *)malloc(sizeof(char) * 20);
	printf("請輸入需要離開的車牌號:\n");
	scanf("%s", goalnum);
	Car *n = p -> top;
	int count = 0;
	while(n != NULL && strcmp(goalnum, n -> carnum) != 0)
	{
		count++;
		n = n -> next;
	}
	n = p -> top;

	if(count == p -> length)
	{
		printf("該車輛不在停車場內!\n");
	}
	else
	{
		while(n)
		{
			if(strcmp(goalnum, n -> carnum) == 0)
			{
				strcpy(n -> timeout, now_time());
				n -> total = (Seconds() - n -> InsertSeconds) * PRICE;
				EnterStack(h, PopStack(p));
				printf("%s已經退出停車棧,費用為%d元\n", n -> carnum, n -> total);
				break;
			}
			EnterStack(r, PopStack(p));
			printf("%s已經退出停車棧,暫時進入讓路棧\n",n -> carnum);
			n =  p-> top;//關鍵問題!!!!!!!
		}
		while(FullStack(p) == FALSE && EmptyStack(r) == FALSE) 
		{
			EnterStack(p, PopStack(r));
			printf("%s已經回到停車棧\n", p -> top -> carnum);
			if(EmptyStack(r) == TRUE)
			{
				printf("讓路棧已空!\n");
				break;
			}
		}
	}
	while( EmptyQueue(w) == FALSE && FullStack(p) == FALSE)
	{
		EnterStack(p, PopQueue(w));
		printf("%s已經從候車佇列進入停車棧!\n", p -> top -> carnum);
		p -> top -> timein = now_time();
	}
}

int QueryCar(Park *p, Wait *w, Park *h)
{
	int flag = 0;
	char *goal = (char *)malloc(sizeof(char));
	if(NULL == p || NULL == h || NULL == w)
	{
		return FALSE;
	}
	printf("請輸入想要查詢的車牌號:\n");
	scanf("%s",goal);
	Car *p1 = p -> top;
	Car *w1 = w -> front -> next;
	Car *h1 = h -> top;
	while(p1)
	{
		if(strcmp(goal, p1 -> carnum) == 0)
		{
			printf("車牌為%s的車輛正在計時停車,入場時間為%s\n",p1 -> carnum, p1 -> timein);
			flag = 1;
			break;
		}
		p1 = p1 -> next;
	}
	while(w1)
	{
		if(strcmp(goal, w1 -> carnum) == 0)
		{
			printf("車牌為%s的車輛正在等候佇列\n",w1 -> carnum);
			flag = 1;
			break;
		}
	}
	while(h1)
	{
		if(strcmp(goal, h1 -> carnum) == 0)
		{
			printf("車輛%s已離開,入場時間%s,離開時間%s,計費%d元\n",h1 -> carnum, h1 -> timein, h1 -> timeout, h1 -> total);
			flag = 1;
			break;
		}
	}
	if(flag == 0)
	{
		printf("沒有找到這輛車!\n");
	}

}

int ListAll(Park *p, Wait *w, Park *h)
{
	if(NULL == p || NULL == w || NULL == h)
	{
		return FALSE;
	}
	TraverStackP(p);
	TraverStackH(h);
	TraverQueue(w);
}

#include"park.h"
#include<stdio.h>
#include<stdlib.h>

int main()
{
	system("clear");
	Park *park;
	Park *road;
	Wait *waitcar;
	Park *history;
	Init(&park, &road, &waitcar, &history);

	int choice;
	while(1)
	{
		Menu();
		printf("請選擇:");
		scanf("%d",&choice);
		getchar();
		switch(choice)
		{
			case 1:
				InsertCar(park, waitcar);
				break;
			case 2:
				PopCar(park, road, waitcar, history);
				break;
			case 3:
				QueryCar(park, waitcar, history);
				break;
			case 4:
				ListAll(park, waitcar, history);
				break;
		/*	case 5:
				ListCarIn(park);
				break;
			case 6:
				ListCarWait(wait);
				break;*/
			case 7:
				printf("歡迎下次使用!\n");
				sleep(1);
				exit(1);
				break;
			default:
				printf("請重新選擇!\n");
				break;
		}
	}
	
	return 0;
}

跟通訊錄相比,本人在該專案中主要通過對初始化、入棧、出棧、進佇列、出佇列等介面函式的呼叫實現了對整個停車場的管理操作。對比通訊錄,在這個專案中,資料結構更加的多元化,不是單一的單向連結串列,而是結合了鏈式佇列、鏈式棧、結構體指標等。在函式呼叫時,實參與形參的數量和型別都變得豐富,資料型別的一致是實現功能的關鍵。對於棧中top指標和佇列中front和rear都有了更深入的理解。