停车场模拟问题的分析思路,解决细节。加了关键注释。

问题描述:

停车场管理员的任务就是帮助车主把车停放在停车场中,或者是帮助车主将 车开出乘车场。然后停车场中能够停放的车辆数目很多,这就使得让莫辆车开出停车场变得复杂。比如:要开走一辆车,则管理员需要把他前面的车全部暂时清除,然后等这辆车开出后再将这些车重新放入停车场。当然了,这个时候腾出了一个空位置,此位置由后面的车占据。
任务:编程模拟这样的情况,这里假设停车场最多可停放5辆车。data.txt记录了某一时间段内,该停车场车辆的到来与离开记录,刚开始,停车场是空的。其中大写字母A–P是车辆的代号,arrives–到来,departs—离开。
程序需要从data.txt中读取这些信息,并且用这些数据来模拟停车场的车辆调度情况。

 

data.txt内容如下:

 

A arrives
A departs
B arrives
C arrives
D arrives
C departs
E arrives
F arrives
G arrives
B departs
H arrives
D departs
E departs
I arrives
I departs
J arrives
F departs
K arrives
L arrives
M arrives
H departs
N arrives
J departs
K departs
O arrives
P arrives
P departs
O departs
L departs

实现代码如下:

模拟停车场问题.cpp(没有再继续分.h文件,混为一体了,主要.h文件过于简单)

#ifndef CAR_H #define CAR_H #include<iostream> #include<string> using namespace std; class car { public: car(string,int); string getlicense(); int getmovedtimes(); ~car(); void move(); private: string license;//车的通行证 int movedtimes;//被移动的次数 }; #endif car::car(string license,int movedtimes):license(license),movedtimes(0) { } string car::getlicense() { return license; } int car::getmovedtimes() { return movedtimes; } void car::move() { movedtimes++; } car::~car() {} #include<fstream> #include<stack> int main() { string in_filename=”data.txt”;//数据文件了,包含了停车场内的车辆进出记录 ifstream inf(in_filename.c_str());//void open(const char* filename,int mode,int access);另外,fstream还有和open()一样的构造函数,对于上例,在定义的时侯就可以打开文件了: //fstream file1(“c://config.sys”); if(!inf) { cerr<<“文件打开失败!”<<in_filename<<endl; return EXIT_FAILURE; } stack<car*> parking_lot,tempstack;//定义两个栈,一个模拟停车场,另外一个用来暂时存放从停车场哪里暂时清除的车,当然最后还是得返回给停车场 car* pcar; string license_plate,action;//分别记录从数据文件中读取的通行证跟行为(到达?离开?) //按行读取数据文件 while(!inf.eof()) { inf>>license_plate>>action; if(action==”arrives”)//到达 { if(parking_lot.size()<5)//栈不满的话,继续入栈 { pcar=new car(license_plate,0);//这个就不用多罗嗦 parking_lot.push(pcar); } else cout<<“抱歉”<<license_plate<<“,停车场已满!”<<endl; } else if(action==”departs”)//如果是出发 { //首先得给出判断,此时栈是否为空?而且出发的这辆车的license_plate是否位于栈顶 while( (!parking_lot.empty()) && (parking_lot.top()->getlicense()!=license_plate))//while循环 { tempstack.push(parking_lot.top()); parking_lot.top()->move();//增加移动次数 parking_lot.pop(); //delete parking_lot.top();此处还不能销毁结点,只是一个短暂的转移罢了 } if(parking_lot.top()->getlicense()==license_plate)//如果要出发的这辆车的license_plate刚好就处在栈顶位置,则直接销毁相关结点,不需要增加移动次数 { cout<<parking_lot.top()->getlicense()<<“被移动了”<<parking_lot.top()->getmovedtimes()<<” 次在这里!”<<endl;//输出被移动的次数 delete parking_lot.top(); parking_lot.pop(); } else cout<<“神马情况(异常)!”<<endl; //接下来还得进行还原,既然有移动那就得还原 while(!tempstack.empty()) { parking_lot.push(tempstack.top()); tempstack.pop(); } } } cout<<“还在车库里面的!”<<endl;//最后把还在车库里面的车的license输出,同时关注移动次数 while(!parking_lot.empty())//用循环依次遍历栈中的元素,也就是对应的车辆了 { cout<<parking_lot.top()->getlicense()<<” 被移动了”<<parking_lot.top()->getmovedtimes()<<“次在这里”<<endl; delete parking_lot.top();//销毁栈顶 parking_lot.pop();//出栈 } inf.close(); return 0; }

    原文作者:停车场模拟问题
    原文地址: https://blog.csdn.net/yiruirui0507/article/details/5963595
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞