1. 程式人生 > >Linux c 停車場管理系統

Linux c 停車場管理系統

 功能:1 車輛進出登記 2 出入記錄 3 候車場 4 檢視 停車場及候車場車輛資訊及總數 5  檢視停留時間最長的車輛 。。。

程式碼如下

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>

#define SIZE   5
#define SUCCESS   10001
#define FAILURE   10002
#define TURE      10003
#define FAISE     10004
#define price       100



//出入記錄
struct record
{
	
	int num;
	long int time1;
	long int time2;
	float time;
	struct record *next;
	
};

typedef struct record srd;


struct stack  //停車場
{
	
	//記錄車牌號碼
	//記錄進場時間
	//記錄出場時間
    int *num;
	long int *time1;
	long int *time2;
	int top;
	
};

typedef struct stack ssk;

struct wait   //候車場
{
	
	//順序佇列
	int *num;
	long int *time1;
	int front;
	int rear;
};

typedef struct wait ssw;

struct exchange //換車場
{
	//換車用的
	int *num;
	long int *time1;
	long int *time2;
	int top;
};

typedef struct exchange sse;


int ret;
ssk *car;
ssw *wait;
ssk *exchange;
srd *record;
int x = 0;


int initrecord(srd **r)
{
	
	(*r) = (srd *)malloc(sizeof(srd) * 1);
	
	if(NULL == (*r))
	{
		return FAILURE;
	}
	
	(*r)->next = NULL;
	
	return SUCCESS;
	
}

//給候車場進行初始化
int initwait(ssw ** w)
{
	
	//分配空間
	(*w) = (ssw *)malloc(sizeof(ssw));
	
	if(NULL == (*w))
	{
		return FAILURE;
	}
	
	(*w)->num = (int *)malloc(sizeof(int)*100);
	
	if(NULL == (*w)->num)
	{
		return FAILURE;
	}
	
	(*w)->time1 = (long int *)malloc(sizeof(long int)*100);
	
	if(NULL == (*w)->time1)
	{
		return FAILURE;
	}
	
	(*w)->front = (*w)->rear = 0;
	
	return SUCCESS;
	
}



//主介面
void welcome()
{
	
	printf("**************歡迎來到德萊聯盟****************\n");
	printf("**********************************************\n");
	printf("*****************請選擇功能*******************\n");
	printf("**********************************************\n");
	printf("*************1 錄入進入停車場*****************\n");
	printf("**********************************************\n");
	printf("*************2 登記出車***********************\n");
	printf("**********************************************\n");
	printf("*************3 檢視停車場資訊*****************\n");
	printf("**********************************************\n");
	printf("*************4 檢視候車場資訊*****************\n");
	printf("**********************************************\n");
	printf("*************5 檢視候車場一共有多少輛車*******\n");
	printf("**********************************************\n");
	printf("*************6 檢視停留時間最長的車輛*********\n");
	printf("**********************************************\n");
	printf("*************7 查看出入記錄*******************\n");
	printf("**********************************************\n");	
	printf("*************8 退出系統**********************\n");
}



int initpark(ssk **s)//初始化
{
	
	//給s分配空間
	(*s) = (ssk *)malloc(sizeof(ssk) * 1);
	
	if(NULL == (*s))
	{
		return FAILURE;
	}
	(*s)->top = -1;
	
	
	//給num分配空間
	(*s)->num = (int *)malloc(sizeof(int) * SIZE);
	
	if((*s)->num == NULL)
	{
		return FAILURE;
	}
	
	//給time1分配空間
	(*s)->time1 = (long int *)malloc(sizeof(long int) * SIZE);
	
	if((*s)->time1 == NULL)
	{
		return FAILURE;
	}
	
	//給time2分配空間
	(*s)->time2 = (long int *)malloc(sizeof(long int) * SIZE);
	
	if(NULL == (*s)->time2)
	{
		return FAILURE;
	}
	
	return SUCCESS;

}



//停車場遍歷
int cartraver(ssk *s)
{
	
	//入參判斷
	if(s == NULL)
	{
		return FAILURE;
	}
	
	//判斷停車場是否有車
	if(s->top == -1)
	{
		printf("停車場裡沒有車!\n");
	}
	
	int i;
	
	for(i = 0;i <s->top + 1;i++)
	{
		printf("車牌號碼%d  進場時間%s\n",s->num[i],ctime(&(s->time1[i])));
	}
	
	return SUCCESS;
	
}



//候車場遍歷
int waittraver(ssw *w)
{
	
	//判斷候車場是否有車
	if(w->rear == w->front)
	{
		printf("候車場沒有車\n");
	}
	
	// 入參判斷
	if( w == NULL)
	{
		return FAILURE;
	}
	
	int a;
	a = (w->rear - w->front + 100) % 100;
	
	int i;
	
	for(i =w->front;i < w->rear;i++)
	{
		
		printf("候車場的車輛資訊為:\n");
		printf("車牌號碼:%d 進場時間:%s\n",w->num[i],ctime(&(w->time1[i])));
		
	}
	
	return SUCCESS;
	
}


	

//往候車場裡停車
int waiting(ssw *w,ssk *s)
{
	
	while(s->top >= SIZE - 1)
	{
		
		//入參判斷
		if(NULL == w)
		{
			return FAILURE;
		}
		
		printf("請輸入進入候車區車牌號碼:\n");
		scanf("%d",&w->num[w->rear]);
		
		time_t t1;
		time (&t1);
		long int t = t1;            // 標記進入候車場的時間
		char* t2;
		t2 = ctime (&t1); 		// 獲取當前時間
		w->time1[w->rear] = t;
		
		printf("車牌號為%d進入候車區,時間為%s\n",w->num[w->rear],t2);
		w->rear ++;
		
		int a;
		printf("是否結束輸入? 1 是 2 否\n");
		scanf("%d",&a);
		
		if( a == 1)
		{
			break;
		}
		
		else if(a == 2)
		{
			continue;
		}
		
		else
		{
			printf("輸入錯誤!請重新輸入:\n");
		}
		return SUCCESS;
		
	}
}
	
	
//往停車場裡 停車
int push(ssk *s)
{
	
	while(s->top < SIZE)
	{
		
		//入參判斷
		if(NULL == s)
		{
			return FAILURE;
		}
		
		else if(s->top == SIZE - 1)
		{
		
			int h;
			printf("停車場已滿,請等候!\n");
			printf("是否要登記車輛進入候車場?1 是 2 否\n");
			scanf("%d",&h);
			
			if(h == 1)
			{
				waiting(wait,car);
			}
			
			else 
			{
				break;
			}
			
		}
		
		else
		{
			printf("請輸入進入停車場的車牌號碼:\n");
			scanf("%d",&s->num[s->top + 1]);
			
			time_t t1;
			time (&t1);            // 標記進入停車場的時間
			char* t2;
			t2 = ctime (&t1); 		// 獲取當前時間
			s->time1[s->top + 1] = t1;
			
			printf("牌照為%d的車進入,當前時間為%s\n",s->num[s->top + 1],t2);
			s->top++;
			
			int a;
			
			printf("是否繼續輸入進入停車場的車輛資訊?\n");
			printf("   1 繼續輸入       2 返回主介面\n");
			
			scanf("%d",&a);
			
			if(a == 1)
			{
				continue;
			}
			
			else if (a == 2)
			{
				break;
			}
			
			else
			{
				printf("輸入錯誤!\n");
			}
			
		
		}
		
	}
	
	//返會成功
	return SUCCESS;
}



int pop(ssk *s,ssk *e,ssw *w,srd *r)
{
	//出車
	int a;
	int p = 0;// 用來判斷是否找到查詢的車輛
	
	//將要離開停車場的車牌號
	printf("請輸入出停車場的車牌號:\n");
	scanf("%d",&a);

	int k = 0;

	//當停車場內有車時進行迴圈
	while(s->top > -1)
	{
		
		// 入參判斷
		if(NULL == s)
		{
			return FAILURE;
		}
		
		//入參判斷
		if(e == NULL)
		{
			printf("候車場出現錯誤!");
		}
		
		// 找到要出去的車
		if(a == s->num[s->top])
		{
				
			if(s->top == -1)
			{
					return FAILURE;
			}

			time_t t1;      
			time (&t1); 
			
			// 標記離開停車場的時間
			s->time2[0] = t1;             
			char* t2;
			// 獲取當前時間
			t2 = ctime (&t1);	
			
			srd *pp = r;
			srd *qq = (srd *)malloc(sizeof(srd)*1);
			
			if(qq == NULL)
			{
				return FAILURE;
				break;
			}
			
			qq->num = s->num[s->top];
			qq->time1 = s->time1[s->top];
			qq->time2 = s->time2[0];
			qq->time = (difftime(s->time2[0],s->time1[s->top]))/60;
			qq->next = pp->next;
			pp->next = qq;
			
			
			
			//計費
			int money;
			money = ((difftime(s->time2[0],s->time1[s->top]))/60 - 0.5) * price;
			
			//如果時間超過了30秒
			if ((difftime(s->time2[0],s->time1[s->top]))/60 > 0.5)
			{
				printf("車牌號:%d於 %s出場停留時間%.1f分鐘\n",s->num[s->top],t2,(difftime(s->time2[0],s->time1[s->top]))/60);
				printf("時間超過了30秒;應收$%d\n",money);
			}
			
			//如果時間沒有超過30秒
			else 
			{
				printf("車牌號:%d於 %s出場停留時間%.1f分鐘\n",s->num[s->top],t2,(difftime(s->time2[0],s->time1[s->top]))/60);
				printf("時間沒有超過30秒 不收費!!\n");
			}
			
			
			//top減1
			s->top --;
			p++;    //找到車輛 p值發生改變
			
			break;
			
		} 
		
		
		//把其餘的車停到交換車場
		e->num[e->top + 1] = s->num[s->top];
		e->time1[e->top + 1] = s->time1[s->top];
		e->time2[e->top + 1] = s->time2[s->top];
		
		
		s->top --;
		e->top ++;
		k++;

		
	}
	
	
	if(p == 0)
	{
		printf("沒有這輛車的資訊!\n");
		
	}
		
		
	// 把交換車場裡的車換回去
	while(e->top > -1) 
	{
		
		if(e == NULL)
		{
			return FAILURE;
		}
		
		s->num[s->top + 1] = e->num[e->top];
		s->time1[s->top + 1] = e->time1[e->top];
		s->time2[s->top + 1] = e->time2[e->top];
		
		e->top --;
		s->top ++;
		
		
	}
	
	//當停車場內未滿時 從候車場停入車輛
	while(s->top <= 3)
	{
		
		//當停車場車輛已滿
		if(s->top > 3)
		{
			return FAILURE;
			break;
		}
		
		//如果候車場沒有車輛
		if(w->front == w->rear)
		{
			break;
		}
		
		//入參判斷
		if(NULL == w)
		{
			return FAILURE;
		}
		
		//停車場進車
		int aa;
		printf("是否有車輛進入停車場? 1 是 2 否\n");
		scanf("%d",&aa);
		
		if( aa == 1)
		{
			//如果輸入1 則開始錄入進入停車場的車輛資訊
			push(car);
		}
		
		else 
		{
			//如果沒有車輛進入停車場,則繼續進行迴圈,繼續向下執行
			continue;
		}
			
		int c;
		
		//候車場車輛數量減 1
		c = w->num[w->front];
		w->front = (w->front + 1) % 100;
		
		x++;
		return SUCCESS;
	}
}



int waitlength(ssw *w)
{
	
	//入參判斷
	if(NULL == w)
	{
		return FAILURE;
	}
	
	//如果候車場沒有車輛
	if(w->front == w->rear)
	{
		printf("候車場沒有車輛!\n");
	}
	
	int a;
	
	//檢視候車場有多少車輛
	a = (w->rear - w->front + 100) % 100;
	printf("候車場一共有%d輛車!\n",a);
	
	return SUCCESS;
	
}


//出入記錄 沒有數量限制
int traverrecord(srd *r)
{
	
	if(r == NULL)
	{
		return FAILURE;
	}
	
	srd *qq = r;
	
	
	while(qq->next)
	{
		qq =qq->next;
		printf("車牌號碼:%d\n",qq->num);
		printf("進場時間:%s\n",ctime(&(qq->time1)));
		printf("出場時間: %s\n",ctime(&(qq->time2)));
		printf("停留時間:%.2f分鐘\n",qq->time);
		printf("**********************************\n");
	}
	
	return SUCCESS;
}

int longestcar(ssk *s)
{
	if(NULL == s)
	{
		return FAILURE;
	}
	if(s->top == -1)
	{
		return FAILURE;
	}
	time_t t1;      
	time (&t1); 
			
	// 標記離開停車場的時間
	s->time2[0] = t1;             
	char* t2;
	// 獲取當前時間
	t2 = ctime (&t1);
	
	int i, j,k;
	float temp = 0;
	int flag = 0;
	float a[] = {0};

	
	if(s->top == 0)
	{
		temp = (difftime(s->time2[0],s->time1[s->top]))/60;
		
		printf("目前停留時間最長的是%d,停留時間為%.2f\n",s->num[s->top],temp);
		return SUCCESS;
	}
	
	if(s->top == 1)
	{
		
		float aa= (difftime(s->time2[0],s->time1[0]))/60;
		float bb= (difftime(s->time2[0],s->time1[1]))/60;
		
		if( aa > bb)
		{
			
			printf("目前停留時間最長的是%d,停留時間為%.2f\n",s->num[0],aa);
			
		}
		
		else
		{
			
			printf("目前停留時間最長的是%d,停留時間為%.2f\n",s->num[1],bb);
			
		}
		
		return SUCCESS;
	}
	if(s->top >= 2)
	{
		int c = s->top;
		
		for(i = 0;i <= c;i++)
		{
			a[i] = (difftime(s->time2[0],s->time1[i]))/60;
		}
		
		temp = a[0];
		
			for (j = 1; j <= c; j++)//從第二個開始依次進行比較,
			{                     //選出最小值得下標
									//把較小值的
				if (a[j] > temp)
				{
					temp = a[j];  //把較da值的值放到temp裡
					flag = j;  //把較da值的下標放到flag裡
				}
			}
		
		
	
		printf("目前停留時間最長的是%d,停留時間為%.2f\n",s->num[flag],temp);
		
		return SUCCESS;
		
	}
}

int main()
{

	//主函式
	
	//初始化是否成功
	ret = initpark(&car);
	if(ret == FAILURE)
	{
		printf("初始化停車場失敗\n");
	}
	
	//初始化是否成功
	ret = initpark(&exchange);
	if(ret == FAILURE)
	{
		printf("初始化換車場失敗\n");
	}
	
	
	//初始化執行是否成功
	ret = initwait(&wait);
	
	if(ret == FAILURE)
	{
		printf("初始化候車區失敗\n");
	}
	
	ret = initrecord(&record);
	
	if(ret == FAILURE)
	{
		printf("初始化記錄失敗!\n");
	}
	
	else
	{
		printf("初始化記錄成功!\n");
	}
	int a;
	
	// 清屏
	system("clear");
	
	//進行迴圈
	while(1)
	{
		welcome();
		
		printf("請選擇功能:\n");
		scanf("%d",&a);
		
		switch(a)
		{
			
			//進車是否成功
			case 1:
			system("clear");
			ret = push(car);
			
			if(ret == FAILURE)
			{
				printf("錄入資訊失敗!\n");
			}
			
			else
			{
			printf("錄入資訊成功!\n\n\n");
			}
			break;
			
			//出車是否成功
			case 2:
			system("clear");
			
			ret = pop(car,exchange,wait,record);
			if(ret == FAILURE)
			{
				printf("出車失敗!\n");
			}
			
			else
			{
				printf("出車成功!\n\n\n");
			}
			break;
			
			//檢視停車場車輛資訊
			case 3:
			
			system("clear");
			ret = cartraver(car);
			
			if(ret == FAILURE)
			{
				printf("檢視停車場資訊失敗!\n");
			}
			else
			{
				printf("檢視停車場資訊成功!\n\n\n");
			}
			break;
			
			//檢視候車場車輛資訊
			case 4:
			
			system("clear");
			ret = waittraver(wait);
			
			if(ret == FAILURE)
			{
				printf("檢視候車場資訊失敗\n");
			}
			
			else
			{
				printf("檢視候車場資訊成功\n\n\n");
			}
			break;
			
			//退出系統
			case 5:
			
			system("clear");
			ret = waitlength(wait);
			
			if(ret == FAILURE)
			{
				printf("檢視失敗!\n");
			}
			
			else
			{
				printf("檢視成功\n\n\n");
			}
			break;
			
			case 6:
			system("clear");
			ret = longestcar(car);
			
			if(ret == FAILURE)
			{
				printf("獲取時間最長的車輛失敗!\n");
			}
			
			else
			{
				printf("獲取時間最長的車輛成功!\n");
			}
				
			break;
			case 7:
			
			ret = traverrecord(record);
			if(ret == SUCCESS)
			{
				printf("檢視記錄成功\n");
			}
			else
			{
				printf("檢視記錄失敗\n");
			}
				break;
			case 8:
				system("clear");
				printf("退出系統成功!\n");
				sleep(1);
				exit(0);
				break;
		}
	}
	
		return 0;

}


int initwait (ssw **w);
void welcome();
int initpark (ssk **s);
int waittraver (ssw *w);
int waiting (ssw *w,ssk *s);
int push (ssk *s);
int pop (ssk *s,ssk *e,ssw *w,srd *r);
int waitlength(ssw *w);
int initrecord(srd **r);
int traverrecord(srd *r);