这还是一个数据结构作业
不知道为什么插入不了太多代码。。。
那就只放关键部分,其他的都是些啥栈和队列的实现
//park
static int pos_walk = 0;//静态全局变量,用于记录可以获得的 便道的停车位置
int arrive(stack *park, stack *time, LinkQueue *walk, int license, int arrival)//若有车到达
{
if (isfull(*park) == notfull)//若停车场还有位置
{
push(park, license);//将车牌号和到达时间压入栈
push(time, arrival);
printf("got NO.%d position in park\n", park->top);//输出在停车场中的位置
}
else if (isfull(*park) == full)//若停车场已满,新来的车停入便道
{
pos_walk++;//便道进入一辆车
EnQueue(walk, license);//将车牌号压入便道栈
printf("got NO.%d position in sidewalk\n", pos_walk);//输出在便道中的位置
}
return ok;
}
int depart(stack *park, stack *time, LinkQueue *walk, int license, int arrival)//若有车要离开
{
stack temp_license; //用来存暂时退开的车
Init(&temp_license);
stack temp_time;
Init(&temp_time);
while (1)//1号车离开,先将2号车存入另外一个栈,pop(1),push(2)
{
int med_license = pop(park); //取出的车牌号
int med_time = pop(time);//被取出车的进入时间
if (med_license != license)
{ //不是要离开的车,就存入另一个栈
push(&temp_license, med_license);
push(&temp_time, med_time);
continue;
}
else if (med_license == license)//是要离开的车,输出停车时间和停车费,并将另外一个栈的车还回来
{
int park_time = arrival - med_time; //停车时间
int moneyEvery = 10; //每单位时间停车费
int money = park_time * moneyEvery; //停车费
printf("the car of %d have parked for %d time\n", license,park_time);//输出停车时间
printf("fees is %d\n", money);//输出停车费
while (isempty(temp_license) == notempty)//若temp未空,就一直出栈
{ //从temp还回暂时退开的车
push(park, pop(&temp_license));
push(time, pop(&temp_time));
}
break; //还完车就算这辆车成功离开
}
}
//离开一辆车后,若便道上有车等待,则车进入停车场
if (queue_isempty(*walk) == notempty)//若便道上有车等待
{
QElemType e;
DeQueue(walk, &e);//从便道驶出一辆车
pos_walk--;//便道停车位置减一
push(park, e); //走一辆车就进一辆车
push(time, arrival); //其进入停车场时间就是上一辆车离开的时间
}
return ok;
}
int read(stack *park, stack *time, LinkQueue *walk)//读入数据,并处理
{
while (1)
{
char order;//A或D或E
int license;//车牌号
int arrival;//到达时间
scanf("%c,%d,%d", &order, &license, &arrival);
if (order == 'A')//若有车到达
{
arrive(park, time, walk, license, arrival);
}
else if (order == 'D')//若有车离开
{
depart(park, time, walk, license, arrival);
}
else if (order == 'E')//模拟结束
{
break;
}
}
return ok;
}
//主函数
int main()
{
stack park;//停车场
Init(&park);
stack time;//进入停车场的时间
Init(&time);
LinkQueue walk;//便道
InitQueue(&walk);
read(&park, &time, &walk);//读入处理数据
return 0;
}