c3_car.cpp
来自「这个是严蔚敏版的数据结构上机教程中的部分源代码」· C++ 代码 · 共 66 行
CPP
66 行
/*
停车场模拟程序
BY:wangyucao
*/
#include<iostream>
#include<fstream>
#include<stack>
#include<queue>
using namespace std;
struct Car{
int num; //车辆编号
int time; //进入停车场的时间
};
int main()
{
stack<Car> car; //停车场
stack<Car> car_out; //车辆出栈时的存储栈
queue<Car> outside; //便道
Car now;
ifstream fin("car.in");
ofstream fout("car.out");
char sit;
int i,j,k,n,price,number,time;
fin>>n>>price; //输入停车场容量和单位时间停车单价
while(fin>>sit>>now.num>>now.time && sit!='E') //输入当前车辆的状态,汽车编号和时间
{
switch(sit){
case 'A': //如果是来停车场的车辆,停车场有空位时输出车辆在停车场中的位置
if(car.size()<n) {car.push(now); fout<<"A "<<"停车场 "<<car.size()<<endl;}
//如果停车场没有空位,则停在便道中,输出便道中的位置。
else {outside.push(now); fout<<"A 便道 "<<outside.size()<<endl;}
break;
case 'D':
Car temp;
temp=car.top();
while(temp.num!= now.num)
{
car.pop();
car_out.push(temp);
temp=car.top();
}
car.pop();
//如果车辆离开,则输出离开的车辆编号和离开时间及收费情况
fout<<"D "<<temp.num<<' '<<now.time-temp.time<<' '<<(now.time-temp.time)*price<<endl;
while(!car_out.empty())
{
temp=car_out.top();
car_out.pop();
car.push(temp);
}
if(!outside.empty())
{
temp=outside.front();
temp.time=now.time;
outside.pop();
car.push(temp);
}
break;
case 'E':
break;
}
}
return 0;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?