#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;
}