#include "car_stack.h"
#include <iostream>
using namespace std;
car_stack::car_stack()
{
count = 0;
}
bool car_stack::empty() const
{
if(count==0)
return true;
else
return false;
}
bool car_stack::full() const
{
if(count==max_stack)
return true;
else
return false;
}
Error_code car_stack::push(const stack_entry &item)
{
if(full())
{
return overflow; //栈满,溢出
}
else
{
parking_lot[count].car_information= item.car_information;
parking_lot[count].car_number = item.car_number;
parking_lot[count].car_time = item.car_time;
count++;
}
return success;
}
Error_code car_stack::pop()
{
if(empty())
return underflow;
else
{
--count;
}
return success;
}
Error_code car_stack::top(stack_entry &item) const
{
if(empty())
return underflow;
else
{
item = parking_lot[count-1];
}
return success;
}
Error_code car_stack::pop_and_top(stack_entry &item)
{
if(count==0)
return underflow;
else
{
item = parking_lot[--count];
}
return success;
}
int car_stack::size() const
{
return count;
}
void car_stack::clear()
{
count = 0;
}
int car_stack::find_car(stack_entry &item)
{
for(int i=0;i<count;i++) //遍历一遍,找出车牌号所对应的栈下标
{
if(item.car_number==parking_lot[i].car_number)
return i;
}
return -1; //没有此车,提示重新输入
}
float car_stack::calculate_time(stack_entry &item,int top) //只在汽车出去时会调用此函数
{
int minute = item.car_time-parking_lot[top].car_time;
return (float)(minute/60.0);
}
void car_stack::pop_car(int top)
{
if(size()==1)
count=0;
else
{
for(int i=top;i<size()-1;i++)
{
parking_lot[i] = parking_lot[i+1];
}
count--;
}
}
void car_stack::print_stack() const
{
if(count==0)
{
cout<<"停车场内的情况为:"<<endl;
cout<<" 停车场内没有汽车"<<endl;
}
else
{
cout<<"停车场内的情况为:"<<endl;
for(int i=0;i<count;i++)
{
cout<<" 第"<<i+1<<"个元素为:"<<parking_lot[i].car_information<<" "<<parking_lot[i].car_number<<" "<<parking_lot[i].car_time<<endl;
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
问题描述:设停车场是一个可停放n辆车的狭长通道,且只有一个大门可供汽车进出。在停车场内,汽车按到达的先后次序,由北向南依次排列(假设大门在最南端)。若停车场内已停满n辆车,则后来的汽车需在门外的便道上等候,当有车开走时,便道上的第一辆车即可开入。当停车场内某辆车要离开时,在它之后进入的车辆必须先退出停车场为它让路,待该辆车开出大门后,其他车辆再按原次序返回车场。每辆车离开停车场时,应按其停留时间的长短交费(在便道上停留的时间不收费)。 设计要求: 1.模拟上述管理过程。要求以顺序栈模拟停车场,以链队列模拟便道。 2.从终端读入汽车到达或离去的数据,每组数据包括三项: (1)是“到达”还是“离去”; (2)汽车牌照号码; (3)“到达”或“离去”的时刻。 3.与每组输入信息相应的输出信息为:如果是到达的车辆,则输出其在停车场中或便道上的位置;如果是离去的车辆,则输出其在停车场中停留的时间和应交的费用。 提示:需要另设一个栈,临时停放为让路而从停车场退出的车。
资源推荐
资源详情
资源评论

















收起资源包目录












共 10 条
- 1
资源评论


zhu_546629
- 粉丝: 0
- 资源: 5
上传资源 快速赚钱
我的内容管理 收起
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


会员权益专享
安全验证
文档复制为VIP权益,开通VIP直接复制
