#include"stdio.h"
#include"stdlib.h"
#include"quene.h"
#include"time.h"
#define max 10
/****************************************************
copyright: self_chou
Filename: carpark.c
AUthour : self_chou Version: 1.0 Date: 2012.07
Description: 見截圖
Function List:
棧和隊列出入棧(隊)函數;
disp(); 界面函數
parkcat(); 顯示停車場信息
leave(); 車輛調度函數
park(); 車輛調度函數
parktime(); 停車時間計算
*******************************************************/
int current = 1; //停車場中當前車輛數
char temp[30]; //系統時間緩衝區
int wait = 0; //等待區車輛數
void park()
{
printf("正在爲你查詢是否有空位...........\n");
printf("\n");
if( current > max) //車位已滿
{
time_t tm = time(NULL);
printf("沒有空位,請等待\n");
strcpy(temp,ctime(&tm)); //記錄車輛到達時間
inque(0,temp);
wait++;
return;
}
printf("有車位空閒,請停在%d號車位\n",current); //有車位空閒
time_t tm = time(NULL);
strcpy(temp,ctime(&tm));
push(current,temp,&toppark);
current++;
}
void parkcat()
{
stack *p = toppark;
printf("正在爲你查詢停車場信息\n");
if( p == NULL)
{
printf("當前停車場中,沒有車輛\n");
return;
}
printf("車位號: ");
while( p != NULL)
{
printf("%4d",p->num); //打印車輛實時信息
p = p->next;
}
printf("\n");
p = toppark;
printf("停車時間:");
while(p != NULL)
{
printf("%4d",parktime(p));
p = p->next;
}
printf("\n");
}
void leave(int del)
{
stack *p = toppark;
printf("%d車位的車正在離開............\n",del);
sleep(1);
while( p->num != del )
{
push(p->num,p->reach,&toptemp); //在目標車後的車輛,進讓路棧
p = p->next;
pop(&toppark);
}
printf("%d車位的車,總共停車%d分鐘,收費%d元\n",p->num,parktime(p),(parktime(p)/5));
sleep(1); //收費情況
pop(&toppark);
current--;
p = toptemp;
while( p != NULL ) //目標車離開後,讓路棧中的車,再次進入
{
push(p->num-1,p->reach,&toppark);
p = p->next;
pop(&toptemp);
}
printf("正在查看等待區是否有車輛.........\n");
if( head != NULL)
{
wait--;
stack *q = head;
printf("該車在等待%d分鐘後找到了車位\n",parktime(q));
deque();
time_t tm = time(NULL); //記錄到達時間,和所停車位號
strcpy(temp,ctime(&tm));
push(current,temp,&toppark);
current++;
}
else
{
printf("\n");
printf("沒有車輛等待\n");
}
}
int parktime(stack *p) //停車時間用系統時間的分位和秒位記
{
time_t tm = time(NULL);
strcpy(temp,ctime(&tm));
int time1 = ( (temp[14] - '0')*10 + (temp[15] - '0') ) * 60 + (temp[17] - '0')*10 + temp[18] - '0';
int time2 = ( (p->reach[14] - '0')*10 + (p->reach[15] - '0') ) * 60 + (p->reach[17] - '0')*10 + p->reach[18] - '0';
return time1 - time2;
}
void disp()
{
int get;
while(1)
{
system("clear");
printf("本停車場共有%d個車位,當前共有%d個車位空閒,%d車等待\n",max,(max-current+1)>0?max-current+1:0,wait);
printf("***************welcome to our car parking*************\n");
printf("1--------------停車\n");
printf("2--------------離開\n");
printf("3--------------查看停車場停車情況\n");
printf("4--------------退出\n");
printf("請輸入:");
scanf("%d",&get);
switch(get)
{
case 1:
{
system("clear");
park();
sleep(1);
break;
}
case 3:
{
system("clear");
parkcat();
sleep(3);
break;
}
case 2:
{
system("clear");
if(toppark == NULL)
{
printf("停車場中沒有車輛:\n");
sleep(1);
break;
}
printf("停車場實時信息如下:\n");
parkcat();
input: printf("請輸入幾號車位的車離開:");
scanf("%d",&get);
if(get > (current-1))
{
if( get > 10)
{
printf("沒有該車位號\n");
}
else
{
printf("該車位空閒,請重新輸入\n");
}
goto input;
}
leave(get);
sleep(2);
break;
}
case 4:
{
exit(0);
}
default:
{
printf("輸入有誤,請重新輸入\n");
break;
}
}
}
}
int main()
{
disp();
return 0;
}