Analyze the problem of parking lot simulation and solve the details. Added key notes.

Source: Internet
Author: User

Problem description:

The parking lot administrator's task is to help the car owner park the car in the parking lot, or help the car owner to drive the car out of the bus. The number of vehicles that can be parked in the parking lot is large, which makes it complicated for a Moche to leave the parking lot. For example, if you want to drive a car, the administrator needs to temporarily clear all the cars in front of him, and then wait until the car leaves and re-place the cars into the parking lot. Of course, a blank position is made up at this time, which is occupied by the rear car.
Task: Simulate the scenario by programming. Here we assume that a maximum of five vehicles can be parked in the parking lot. Data.txt records the arrival and departure records of vehicles in the parking lot within a certain period of time. At the beginning, the parking lot was empty. The capital letter A--P is the vehicle code, arrives -- arrival, departs --- Leave.
The Program Scheduler reads the information from data.txt and uses the data to simulate vehicle scheduling in the parking lot.

 

The content of data.txt is as follows:

 

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

The implementation code is as follows:

Simulate the parking lot problem. cpp (no further splitting. h file, mixed as one, the main. h file is too simple)

# Ifndef car_h <br/> # define car_h <br/> # include <iostream> <br/> # include <string> <br/> using namespace STD; <br/> class car <br/>{< br/> Public: <br/> Car (string, INT); <br/> string getlicense (); <br/> int getmovedtimes (); <br/> ~ Car (); <br/> void move (); <br/> PRIVATE: <br/> string license; // vehicle pass <br/> int movedtimes; // number of moves <br/>}; <br/> # endif <br/> Car: Car (string license, int movedtimes): License (license ), movedtimes (0) <br/>{< br/>}</P> <p> string car: getlicense () <br/>{< br/> return license; <br/>}< br/> int car: getmovedtimes () <br/>{< br/> return movedtimes; <br/>}< br/> void car:: Move () <br/>{< br/> movedtimes ++; <br/>}< br/> C AR ::~ Car () <br/>{}</P> <p> # include <fstream> <br/> # include <stack> <br/> int main () <br/>{< br/> string in_filename = "data.txt"; // data file, contains the vehicle entry and exit records in the parking lot <br/> ifstream Inf (in_filename.c_str (); // void open (const char * filename, int mode, int access); in addition, fstream also has the same constructor as open (). For the above example, you can open the file when defining: <br/> // fstream file1 ("C: // config. sys "); </P> <p> If (! INF) <br/>{< br/> cerr <"An error occurred while opening the file! "<In_filename <Endl; <br/> return exit_failure; <br/>}< br/> stack <car *> parking_lot, tempstack; // defines two stacks, one simulated parking lot and the other used to temporarily store the vehicles that are temporarily cleared from the parking lot. Of course, you still have to return them to the parking lot <br/> Car * pcar; <br/> string license_plate, action; // records the pass and behavior read from the data file respectively (arrive? Leave ?) <Br/> // read the data file by row <br/> while (! INF. EOF () <br/>{< br/> INF> license_plate> action; <br/> If (Action = "arrives ") // arrive at <br/>{< br/> If (parking_lot.size () <5) // If the stack is not satisfied, continue to stack <br/>{< br/> pcar = new car (license_plate, 0); // you do not need to roll back <br/> parking_lot.push (pcar ); </P> <p >}< br/> else </P> <p> cout <"sorry" <license_plate <", the parking lot is full! "<Endl; </P> <p >}< br/> else if (Action =" departs ") // if it is a departure <br/>{< br/> // you must first determine whether the stack is empty at this time? And whether the license_plate of the departure car is located at the top of the stack <br/> while ((! Parking_lot.empty () & (parking_lot.top ()-> getlicense ()! = License_plate) // while loop <br/>{< br/> tempstack. push (parking_lot.top (); <br/> parking_lot.top ()-> move (); // increase the number of moves <br/> parking_lot.pop (); <br/> // Delete parking_lot.top (); the node cannot be destroyed, but it is only a short transfer. <br/>}< br/> If (parking_lot.top () -> getlicense () = license_plate) // If the license_plate of the car to be set off is at the top of the stack, the related node is directly destroyed, no need to increase the number of moves <br/> {<br/> cout <parking_lot.top ()-> getlicense () <"Moved" <parking_lot.top () -> getmovedtimes () <"Times here! "<Endl; // number of times the output is moved </P> <p> Delete parking_lot.top (); <br/> parking_lot.pop (); <br/>}< br/> else <br/> cout <"Shenma situation (exception )! "<Endl; <br/> // you have to restore it later. Since there is a movement, You have to restore it. <br/> while (! Tempstack. empty () <br/>{< br/> parking_lot.push (tempstack. top (); <br/> tempstack. pop (); <br/>}</P> <p >}< br/> cout <"still in the garage! "<Endl; // output the license of the car still in the garage, and pay attention to the number of moves <br/> while (! Parking_lot.empty () // cyclically traverse the elements in the stack, that is, the corresponding vehicle <br/>{< br/> cout <parking_lot.top ()-> getlicense () <"Moved" <parking_lot.top ()-> getmovedtimes () <"times here" <Endl; <br/> Delete parking_lot.top (); // destroy the top of the stack <br/> parking_lot.pop (); // out of the stack <br/>}< br/> INF. close (); <br/> return 0; </P> <p>}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.