# 1、Problem description and requirement

The elevator system of nine storey teaching building in a school is simulated. The building has an automatic elevator, which can stay in every layer, the first floor is the entrance layer of the building, that is the “home base” of the elevator. When the elevator is “idle”, it will come to this layer in the future.

The elevator has seven states, that is, Opening, Opened, Closing, Closed, Waiting, Moving, Decelerate..

Passengers can be randomly entered into any layer. For everyone, he has the longest waiting time that can be tolerated. Once he waits for too long a lift, he will give up.

The analog clock starts from 0, and the time unit is 0.1 seconds. All kinds of movements of people and elevators consume a certain amount of time (T), for example:

When people enter or leave, the elevator is tested once every 40t. If no one enters or goes, it closes.

Close the door and open the door for 20t.

Everyone needs 25t to get in and out of the elevator.

Elevator acceleration needs 15t;

When ascending, each layer needs 51T and deceleration needs 14T.

When falling, 61T is needed for each level and 23t for deceleration.

If the elevator stays at 300t above a certain level, it will return to the 1 tier waiting time.

Elevator scheduling rules:

1）Proximity principle: the main scheduling strategy of an elevator is to respond first to the nearest end request along the current direction until the most remote request is satisfied. If there is no request in the direction, the direction of movement will be changed.

2）In the case that the principle of proximity can not be satisfied, the higher level requests are first met.

3）The maximum carrying capacity of the elevator is 13, and the number of elevators reaches 13.

4）The passengers go up and down when the elevator is up and down. Passengers enter the elevator according to the order of the request, and each time they enter only one person, and each person spends 25t time.

5）The elevator is also allowed to enter the elevator during the closing period (before the elevator leaves).

Requirement：

The changing state of the system state is shown in time sequence, that is, the sequence of actions of all people and elevators.

Extension requirements:

The visual interface of elevator simulation is realized. Animation shows elevator lifts and people enter and exit elevators. The following objects are designed: elevators, people, elevator control panels and buttons, timers, etc.

## 2、Design

**2.****1**** design idea**

Data can be customized by user, or can be changed to generate relevant data by means of pseudo random number. This program uses user – defined input, which can be more free to determine the relevant data, but it has the same drawbacks, and it is not suitable to change the data at random. The functions of the operation are as follows: addPassengers, passenger requests, judgement of the direction of the elevator, the judgment design of the switch door, the design of the passengers entering and out, the judgment design of the elevator movement, the change of the elevator state, and the judgment of whether someone has given up to change the most remote request of the elevator. The main operations must be traversed every time, so time will be a bit too expensive and store.The design of the structure is not reasonable, so that every time we need to traverse all the data, we waste time.

The storage structure outside the elevator waiting queue is single linked list, and the elevator adopts array storage. The single chain table of the waiting queue is designed to store the node of the passenger class data, which makes it more convenient to access the passenger’s information, but gives up the sort inside the list, because the waiting queue needs to traverse every time.Sorting will waste more time, so discard this function. The array inside the elevator opens up 10 sizes, because the subscript of the array starts from 0, so in order to better store the floor of the passenger, choose to open up an array of 10 sizes. The number stored in each subscript represents the elevator under the floor.The number of people, the reason for the selection of array storage is that once passengers enter the elevator, the useful information is only the floor of the elevator, so the array is a better solution.

The most involved algorithm in this program is the exhaustive search method, which is designed to traverse all data every time, but the algorithm has defects and can not cope with the operation of many data, thus limiting the amount of data of the program. The algorithm uses for loop and while loop to traverse the data.And insert judgment sentences in the loop, so that a series of operations can be done after getting the data needed.

**2.****2**** Design representation**

**2.3 detailed design**

Chain table structure node

Members have: (1) passenger class data data: store relevant information of passengers for use in elevator modules.

（2）struct node*Type next, which is used to link the next node.

The function has: (1) append (parameter is passenger class data): because there are head and tail pointer and head node, as long as the parameter is linked to the tail.

（2）empty：As long as the pointer pointer to the next is empty, return true, otherwise it will return to false.

（3）Remove（The parameter is ID of the passenger): whether the current node of the if is the node to be deleted, if it is handled separately according to whether it is only one node or multiple nodes; the current node of the else is not the node to be deleted, then traverses the whole chain to find the node to be deleted.

Passenger class passenger

Members have: (1) ID: number for passengers.

（2）nowfloor：It is used to record the current floor of the passenger.

（3）gofloor：It is used to record floors where passengers need to go.

（4）whenwait：It is used to record how often passengers enter the waiting queue.

The functions are: (1) default constructor and interface function.

（2）setdata（The parameter is passenger ID): assign the passengers to the ID, and limit the passengers’ current floor and the floor to which they are going to 1-9. Once the range is beyond the range, it is suggested that the data need to be reentered.

Elevator class elevator

Members have: (1) State: used to record the status of elevators.

（2）floor：It is used to record the floor of the elevator.

（3）Wait：A pointer to the header node of the waiting queue.

（4）DiantiLI[10]：An array of personnel in the elevator. Subscript is used to record whether someone in the elevator is down.

（5）All：Record the number of people in the elevator.

（6）Dir：It is used to judge the state before the elevator is outside Up and Down, so as to judge the next state, -1 is the initial state, 0 is down, and 1 is rising.

The function has: (1) setnowState (parameter is the elevator state that needs to be set): used to change elevator condition.

（2）setAll（Parameter is the number of passengers to be modified, signed integer): add and subtract parameters and All parameters.

（3）setDir（Parameter is the value that you want to change Dir: change Dir to parameter value.

（4）JudgeGiveUp（Parameter is the current time: used to determine whether someone is giving up at the moment.

If (the pointer is not empty)

If the waiting time of the currently visited node is + endurance time = = current time)

Delete the node and prompt the}}

（5）NoPassenger：It is mainly used for some operations of elevators when they are idle.

If the elevator is Waiting {

If holdtime (a global variable is used to detect whether 300t is reached) ==300 and floor is not equal to 1{

Change the elevator state to drop and reset the holdtime to 0, enter the MoveDown function, and return to true

}

If holdtim==300 and floor are 1{

Output “elevator idle idle” and set holdtime to 0 and return to true.

}

There’s no one in the elevator.

Output “elevator idle free”, and holdtime self add, return to true

}

In other cases, the JudgeDirction function is called and false is returned.

}

If the elevator is not in the Waiting state, call the JudgeDirction function and return to false.

（6）JudgeDirction：The farthest request under different circumstances is passed to MoveDirction for changing state.

If the state is Closed{

There’s no one in the elevator.

Change the state of the elevator and return to the air

}

If floor is 1{/ / / because at that time, there must be at least one condition inside the elevator.

Change the state of the elevator to Up and set Dir to 1

}

If floor is 9{

Change the state of the elevator to Down and set Dir to 0

}

If there’s someone in the elevator {

Traverse the array DiantiLi to get the subscript i and change the elevator state through I.

}

If the elevator is not empty {

//At this time, according to the Dir before the elevator closes, it determines whether to pick up passengers. This is divided into 4 cases, 2 uplink and 2 downlink.

If Dir is 1{

If the passenger floor is above the current floor, it is possible to pick up the passenger {

If the passenger is ascending, the farthest request will be compared with the passenger’s going to the floor, and the change will be made.

Otherwise, if the passenger is down, the farthest request will be compared with the current floor of the passenger and compared.

}

Contrast and change the farthest requests

}

If Dir is 0{

If the passenger floor is below the current floor, it is possible to pick up the passenger {

If the passenger is down, the farthest request will be compared with the floor of the passenger and the changes will be made.

Otherwise, if the passenger is ascending, the farthest request will be compared with the current floor of the passenger and compared.

}

Contrast and change the farthest requests

}

}

}

If the state is Waiting{

Loop the queue to find the first button to answer the request.

If the pTemp (pointing to the head node) is not empty {

If it is convenient to the earliest passengers entering the queue {

If the elevator needs to go up

Record the farthest request of the uplink

If the elevator needs to go down

Record the farthest request of the downlink

If both of them have

Then meet the principle of first and then down to change the state of elevator.

}

}

}

If the state of the elevator is Up{

If there’s someone in the elevator {

Traversing the DiantiLi array gets the farthest request of the passengers in the elevator.

}

If the waiting queue has someone

Traversing the entire waiting queue

If a passenger wants to go to the floor or the current floor is larger than the furthest request.

Change the farthest request

}

}

If the state of the elevator is Down{

If there’s someone in the elevator {

Traversing the DiantiLi array gets the farthest request of the passengers in the elevator.

}

If the waiting queue has someone

Traversing the entire waiting queue

If a passenger wants to go to the floor or the current floor is smaller than the farthest request.

Change the farthest request

}

}

Finally, call the MoveDirction function and import the farthest request.

（7）MoveDirction（The parameter is the farthest request: calling the different functions through the farthest request and the current state.

If the farthest request is less than the current floor

Call MoveDown and return to the empty

If the farthest request is larger than the current floor

Call MoveUp and return to the empty

If the state of the elevator is currently Opening

Call the Open function and return to the empty

If the state of the elevator is currently In

The IN function is called and the JudgeClose function is called to determine whether there are people entering and returning to space.

If the state of the elevator is currently Out

Call the OUT function, and call the JudgeClose function to determine whether there will be someone else to enter and return empty.

If the state of the elevator is currently Opened

JudgeCloseDecide whether to close the door or not

Call JudgeOut to determine if anyone wants to go out, and if someone goes out, call OUT; call the JudgeIn to determine if there is any entry, if anyone enters, calls IN, then returns to the empty.

If the state of the elevator is currently Closing

Call Close and return to empty

If the farthest request is equal to the current floor and there is no one inside and outside the elevator.

The elevator is set to Waiting and enters the NoPassenger. The elevator is idle.

If the farthest request is equal to the current floor, there is someone inside and outside the elevator.

At this point, lift the elevator to Opening and enter Open.

（8）MoveDown：Output the state of every T at the bottom of the elevator

If record (global variable is used to export the state of each elevator T), it is less than the enumerated value.

recordThe elevator is downstairs and returned to the air.

If it is equal to the value of the enumeration

The floor of the elevator is reduced by 1, and the record is 0

If JudgeOpen is false, continue to call MoveDown to show the state of downstairs.

If JudgeOpen is true, elevate the status of the elevator to Opening.

（9）MoveUp：Output the state of every T on the elevator

If record (global variable is used to export the state of each elevator T), it is less than the enumerated value.

recordThe elevator is going upstairs and returning to the air.

If it is equal to the value of the enumeration

The floor of the elevator is added 1, and the record is 0

If JudgeOpen is false, continue to call MoveUp to show the status of the upstairs.

If JudgeOpen is true, elevate the status of the elevator to Opening.

（10）Open：Show the state of each t of the elevator.

If the current state is not Opening

Set the current state to Opening, set record to 0, and return empty.

If the record is less than the enumerated value

recordSelf add, output “elevator door”, return to empty

If it’s the other case.

recordSet to 0, output “open the door to complete”, and set the current state to Opened

If JudgeOut is true, call OUT

If JudgeIn is true, call IN

JudgeCloseWhether the judgment is closed or not

}

（11）IN：Show which passengers enter the elevator and determine the sentence

If the elevator is less than 13 people in the elevator.

If the record is less than the enumerated value {

If the current state is Opened{

recordSelf adding, traversing waiting queue, and adding judgement that the current floor of passengers must be equal to floor before entering elevator.

}

If the current state is In{

recordSelf added, the output “passengers are entering the elevator”, return empty.

}

}

If it is equal to the enumerated value {

recordSet to 0, and put the current state to Opened

}

}

If the number of people in the elevator is greater than 13{

Output “the number of elevators has reached the maximum”, and the current state is set to Closing.

}

（12）JudgeClose：Judge whether the current elevator can be closed or not

If the current state is Opened{

If the record is less than the enumerated value {

recordSelf plus, the output is “closing detection” and then returned empty.

}

If equal to the enumerated value {

Output “closing door detection end”

If the current state is Opened{

recordSet to 0, call the Close function

}

Otherwise, the record is set to 0 and returns to the empty

}

}

Other cases return to the empty

（13）Close：Show the state of each T when the elevator is closed

If the record is less than the enumerated value {

recordSelf add, set the current state to Closing, and output “the elevator is closing.”

}

Other cases

recordSet to 0, output “elevator has been closed”, set the current state to Closed

If the maximum request is floor{

If there are people in the elevator {

Traversing the entire waiting queue

If the waiting queue at this time someone sends a request

If you stop, the elevator is going up

To determine whether it is on the current floor or on the current floor, if it is satisfied, the current state is set to Opening.

If you stop, the elevator is going down

It is judged whether it is under the current floor or the current floor. If it is satisfied, the current state is set to Opening.

}

If there’s no one in the elevator

Set the current state to be Waiting, and set Dir to -1

}

If the maximum request is not the current floor

The state of the elevator remains unchanged before the stop is stopped

}

}

Call NoPassenger

（14）OUT：Record the state of each t of the passengers’ elevator.

If the record is less than the enumerated value {

If the current state is Opened{

recordSelf addition

If someone in the elevator wants to get off the elevator.

According to the array DiantiLi, determine the number of passengers to go down and output each T state, and set the current state to Out and return to empty.

}

}

If the current state is Out{

recordSelf add, output “passengers are down the elevator”, return empty.

}

}

In other cases, the “passengers need to go down” are exported, record is set to 0, and the current status is set to Opened.

}

（15）JudgeIn：Judge whether you can enter a passenger

If All is not 13{

If the waiting queue is not empty {

If the passenger is going up, and the current floor is on the floor, and the direction ahead of the elevator is upward.

Allow entry, return to true

If the passenger is going down, and the current floor is on the floor and the direction ahead of the elevator is downward.

Allow entry, return to true

}

}

Return to false at this time if the traversal does not return to true

（16）JudgeOut：Judge whether the passengers can go out

If there’s someone in the elevator {

The data in the index of the floor of the array DiantiLi is not 0, then return to true.

}

Otherwise, return to false

## 3、Source program list

passenger.h //Passenger headers, storage of passengers’ relevant information, and interface functions for changing values.

elevator.h //Elevator class header files, enumerated information and elevator attributes and related judgment function.

Node.h //The node class header file is used to create linked lists and linked list related operation functions.

main.cpp //Main function, mainly call the set attribute function of the passenger class, the JudgeGiveUp function of the elevator class, and the addpassenger function of the elevator class, and show how much T is now.

## 4、source code

passenger.h //Passenger headers, storage of passengers’ relevant information, and interface functions for changing values.

1 #ifndef PASSENGER_H 2 #define PASSENGER_H 3 #include<iostream> 4 using namespace std; 5 6 class passenger { 7 private: 8 int ID; 9 int nowfloor; 10 int gofloor; 11 int whenwait; 12 public: 13 passenger(); 14 void setdata(int ID1); 15 void setnowfloor(int nowfloor1); 16 void setgofloor(int gofloor1); 17 void setwhenwait(int whenwait1); 18 int getnowfloor()const; 19 int getgofloor()const; 20 int getID()const; 21 int getwhenwait()const; 22 }; 23 24 passenger::passenger() { 25 ID = 0; 26 nowfloor = 0; 27 gofloor = 0; 28 whenwait = 0; 29 } 30 31 void passenger::setdata(int ID1) { 32 ID = ID1; int i = 1; 33 while (i) { 34 cout << "Please enter "< < ID < <"; "passenger information" < < endl;35 cout < < "which floor is the passenger currently in:"; CIN > &gT; nowfloor;36 cout < < "which layer should the passenger go to:"; CIN > > gofloor;37 cout < < "this multiplier"When the guest on the elevator: "; CIN > > whenwait;38 If (nowfloor > 9 nowfloor nowfloor; <) {Thirty-nineCout < < "the passenger's current floor is wrong, please re-enter! "< < endl;40}41 If (gofloor > 9) goflooR < 0) {42 cout < < "the floor of the passenger is wrong, please re-enter! "< < endl;43}44 elSe I = 0;45}46}Forty-seven48 void passenger:: setnowfloor (int nowfloor1) {49 nowfloor = nowfloOr1;50}Fifty-one52 void passenger:: setgofloor (int gofloor1) {53 gofloor = gofloor1;54}Fifty-five56 VOid passenger:: setwhenwait (int whenwait1) {57 whenwait = whenwait1;58}Fifty-nine60 int passenger::Getnowfloor () const {61 return nowfloor;62}Sixty-three64 int passenger:: getgofloor () const {65 REturn gofloor;66}Sixty-seven68 int passenger:: getID () const {69 return ID;70}Seventy-one72 int passenger:: getwhenwait () const {73 return whenwait;74}Seventy-five76 #endif / /! PASSENGER_H77 #pragma once

elevator.h //Elevator class header files, enumerated information and elevator attributes and related judgment function.

1 #ifndef ELEVATOR_H 2 #define ELEVATOR_H 3 #include"Node.h" 4 #include"passenger.h" 5 6 enum state { 7 Opening, 8 Opened, 9 Closing, 10 Closed, 11 Waiting, 12 Up, 13 Down, 14 In, 15 Out, 16 Decelerate 17 }; 18 19 int holdtime = 0,record=0,near=0; 20 enum timeX 21 { 22 test = 40, 23 open = 20, 24 close = 20, 25 in = 25, 26 out = 25, 27 quick = 15, 28 up = 51, 29 updecelerate = 14, 30 down = 61, 31 downdecelerate = 23, 32 peoplewait = 500, 33 wait = 300 34 }; 35 36 class elevator { 37 private: 38 state State=Waiting; 39 int floor = 1; 40 PNODE Wait=p_head; 41 int DiantiLi[10] = {0}; 42 int All = 0; 43 int Dir=-1;//Judging the situation up and down44 public:45 State getnowState () const;46 void setnowState (state T);47 intGetfloor () const;48 void setfloor (int Floor1);49 int getAll () const;50 void setAll (Int Num); //num is the number of outside elevators51 int getDir () const;52 void setDir (int x);53 void addpasseNger (const passenger & x); / / / adding passengers54 bool NoPassenger (); / / to determine whether there is a passenger request.55 void JudgeDirctioN (); / / judge the direction of elevator walking56 bool JudgeOpen (); / / judge whether to open the door57 void Open (); / / elevator door open58 bool JudgeOut ()/ / / judge the passengers to go out59 void OUT (); / / / passengers out60 bool JudgeIn (); / / judge passengers entering61 void IN (); / / passengers enteringSixty-twoVoid Close (); / / close the door63 void MoveUp (); / / upward moving64 void MoveDown (); / / downwards65 void JudgeCloSe (); //40t time to judge whether the door is closed66 void MoveDirction (const int Floor1); / / used to change the state of the elevator.67 void JudgeGiveUp(int waittime); / / to determine if someone gave up, at the very beginning of the function.68};Sixty-nine70 state elevator:: getnowState () const {71 returN State;72}73 void elevator:: setnowState (state T) {74 State = t;75}76 int elevator::Getfloor () const {77 return floor;78}79 void elevator:: setfloor (int Floor1) {80 flooR = Floor1;81}82 int elevator:: getAll () const {83 return All;84}85 void elevator:: SetAll (int Num) {86 All + = num;87}88 int elevator:: getDir () const {89 return Dir;Nine0}91 void elevator:: setDir (int Num) {92 Dir = num;93}94 void elevator:: addpassenger (coNST passenger & x) {95 append (x);96 cout < < "<"; < x.getID () < < "name".Passengers enter the waiting queue.97}98 bool elevator:: NoPassenger () {99 / / used to determine whether the elevator receives passengers' requests.100 if (getnowStAte () = = Waiting) {101 if (holdtime = = 300 & & floor! =1) {102 / / if the wait is 300t and not on the 1 floor, start down103 setnowState (Down);104 setDir (0);105 holdtiMe = 0;106 MoveDown ();107 return true;108}109 else if (Holdtime== 300 & & floor = = = 1) {110 / / if the elevator itself is on the first floor, there is no need to operate.111 cout<< "the elevator is idle and is waiting";112 holdtime = 0;113 return true;114}One hundred and fifteenElse if (All = = 0 & & empty () ==true) {116 cout < < "elevator idle no one is waiting";One hundred and seventeenHoldtime++;118 return true;119}120 else {One hundred and twenty-oneJudgeDirction ();122 return false;123}124}125 else {One hundred and twenty-sixJudgeDirction ();127 return false;128}129}One hundred and thirty131 void elevator:: JudgeDirction () {132 / / use this function to determine the direction of the elevator.133 near = floor; / / initialize the value of near, and MoveDirction function can still be used to prevent situations that do not meet the requirements.One hundred and thirty-fourInt upoutfar = 0, downoutfar = 10; / / 2 variables that define the most distant requests to go up and down.135 int upinfar = 0, downinfar = 10; / / / definitionThe most distant request variable in the 2 records136 if (State = = Closed) {137 if (getAll () = = 0 & & empty () ==truE) {138 setnowState (Waiting);139 return;140}141 if (floOr = = 1) {142 setnowState (Up); setDir (1);143}144 if (floor = = 9) {One45 setnowState (Down); setDir (0);146}147 if (getAll ()) = 0) / / there is still someone in the elevator.148 / / at this point, we need to distinguish the direction of the elevator from two different situations.149 for (int i = 1; I < 10; i++)) {150 if (DiantiLi[i]! = 0) {151 near = I;One hundred and fifty-twoIf (I > floor) {153 upinfar = I;One hundred and fifty-fourSetnowState (Up);155 setDir (1);One hundred and fifty-sixBreak;157}158 else if (I < floor) {One hundred and fifty-nineDowninfar = I;160 setnowState (Down);One hundred and sixty-oneSetDir (0);162 break;One hundred and sixty-three}164}165}166}167 if (empty () = = = =False) / / / / the person waiting outside the elevator is not empty168 PNODE ptemp=p_head-> next;169 while (PTEMP! =NULL) {170 if (getDir (= = 1)) / / only the upstream passengers.171 if (PTEMp-> data.getnowfloor () > floor) / / the passenger's floor is on the elevator floor, so that it is possible for the elevator to pick up.172 // start retrieving the passengers in the linked list and make sure that they are uplink.173 If (ptemp-> data.getgofloor () > ptemP-> data.getnowfloor ()) {174 / / on behalf of the person is uplink, and is in the direction of the elevator upstairs, need to pick up people.One hundred and seventy-fiveIf (ptemp-> data.getgofloor () > upoutfar) upoutfar = ptemp-> DaTa.getgofloor ();176}177 If (ptemp-> DAT)A.getgofloor () < ptemp-> data.getnowfloor ()) {178 If (ptemp-&gT; data.getnowfloor () > upoutfar) upoutfar = ptemp-> data.getnowfloor ();One hundred and seventy-nine}180}181 If (upinfar = = 0 & & UPOU)Tfar = = = 0) {setnowState (Down); setDir (0);}182 If (upinfar < = upoutfar)Near = upoutfar;183 else near = upinfar;184}One hundred and eighty-fiveOne hundred and eighty-sixElse if (getDir () = = = 0) / / / / only downlink passengers187 if (ptemp-> DAT)A.getnowfloor () < floor) / / the floor of the passenger is under the elevator floor, so that it is possible for the elevator to pick up.188 / / openFirst retrieve the passengers in the linked list and determine whether there is any downlink.189 if (ptemp-> data.getgofloor () < ptemp-> data.getnowfloor ()) {190 / / this person is on the downside and downstairs in the direction of the elevator operation.One hundred and ninety-oneIf (ptemp-> data.getgofloor () < downoutfar) downoutfar = ptemp->Data.getgofloor ();192}193 if (ptemp-> d)Ata.getgofloor () > ptemp-> data.getnowfloor ()) {194 if (ptemp-> data.getnowfloor () < downoutfar) downoutfar = ptemp-> data.getnowfloor ();One hundred and ninety-five}196}197 if (downinfar = = 10 & &)Amp; downoutfar = = 10) {setnowState (Up); setDir (1);}198 if (downinfar <); = downoutfar) near = downinfar;199 else near = downoutfar;Two hundred}201 pTemp = ptemp-> next;202}203}TwentyFour205}206 if (State = = Waiting) {207 PNODE pTemp = p_head-> next;Two hundred and eightInt time_now = 0;209 while (pTemp! =NULL) {210 int Time_now = ptemp-> data.getwhenwait (); / / / / / / to record the first person to press211 time_now = Time_now;212 if (ptemp-> DA)Ta.getwhenwait () < time_now) / / select the first button, then change the direction of the elevator.213 time_now = ptemp-> data.Getwhenwait ();214}215 pTemp = ptemp-> next;216}217 pTemp = p_head-> next;218 while (pTemp! = NULL) {219 int up (floor), down (flooR);220 if (ptemp-> data.getwhenwait () = = time_now) {221 int x = ptemP-> data.getgofloor () - ptemp-> data.getnowfloor ();222 / / there will be 4 cases at this time, and only 2 will be elevators.The 2 is the downlink of the elevator223 if ((x > 0& & (ptemp-> data.getnowfloor () > floor)) x< (x<); 0& & (ptemp-> data.getnowfloor () > floor)) {224 setnowState (Up);Two25 setDir (1);226 upinfar = ptemp-> data.getnowfloor ();Twenty-two7 if (up < upinfar) {228 up = upinfar;Two hundred and twenty-nine}230}231 else if ((x > 0 & & (ptemp-> data).getnowfloor () < floor)) (x<); 0; & & (ptemp-> data.getnowfloor () < floor)) {Two hundred and thirty-twoSetnowState (Down);233 setDir (0);234 downINfar = ptemp-> data.getnowfloor ();235 if (down > downinfar) {Two hundred and thirty-sixDown = downinfar;237}238}Two hundred and thirty-nineIf (down! = floor& & up = = floor) / / when the upper and lower request is made at the same time, the principle of first priority is satisfied.240 setnowState (Up); near = upinfar; setDir (1);241}242 else if (up! = floor) (/ / only)An uplink request243 setnowState (Up); near = upinfar; setDir (1);244}Two hundred and forty-fiveElse if (down! = floor) / / / / only downlink request246 setnowState (Down); near= downinfar; setDir (0);247}248 if (floor = = 1) {setnowState (Up));SetDir (1);}249}250 pTemp = ptemp-> next;251}252 if(near = = floor) Open ();253}254 if (State = = Up) {255 If (getAll)! =0) / / / someone in the elevator256 for (int i = 1; I < 10; i++) {257 if (DiantiLi[i]! = 0) {Two hundred and fifty-eightIf (I > = near) {259 upinfar = I;Two hundred and sixty}261}262}263}264 if (empty () = = = false) {Two hundred and sixty-fivePNODE pTemp = p_head-> next;266 while (pTemp! = NULL) {Two hundred and sixty-sevenIf (ptemp-> data.getnowfloor () > = near) {268 if (ptemp-> data.getgofl)Oor () > = upoutfar) {269 upoutfar = ptemp-> data.getgofloor ();Two hundred and seventy}271 if (ptemp-> data.getnowfloor () > = upoutfar) {Two hundred and seventy-twoUpoutfar = ptemp-> data.getnowfloor ();273}Two hundred and seventy-four}275 pTemp = ptemp-> next;276}277}Twenty-seven8 if (upinfar > = upoutfar) near = upinfar;279 else near = upoutfar;280}Two hundred and eighty-oneIf (State = = Down) {282 //downinfar = 10, downoutfar = 10;283 if (getAll ()! =! =0) / / / someone in the elevator284 for (int i = 1; I < 10; i++) {285 if (DiantiLi[i]!= 0) {286 if (I < = near) {287 downinfar = I;Two hundred and eighty-eightBreak;289}290}291}Two hundred and ninety-two}293 if (empty () = = = false) {294 PNODE pTemp = p_head-> next;Two hundred and ninety-fiveWhile (pTemp! = NULL) {296 if (ptemp-> data.getnowfloor () < = near) {Two97 if (ptemp-> data.getnowfloor () < = downoutfar) {Two hundred and ninety-eightDownoutfar = ptemp-> data.getnowfloor ();299}300 if(ptemp-> data.getgofloor () < = downoutfar) {301 downoutfar = ptemp->Data.getgofloor ();302}303}304 pTemp = pTemp-> next;305}306}307 if (getAll () = = 0 & & empty () = = true)Near = 1;308 else if (downinfar > = downoutfar) near = downoutfar;309 else near =Downinfar;310}311 MoveDirction (near);312}Three hundred and thirteen314 bool elevator:: JudgeOpen () / / / / / judge this floorDo anyone need to go in and out315 if (JudgeIn) () {JudgeOut ()))316 Open ();317 return true;Three hundred and eighteen}319 else return false;320}Three hundred and twenty-one322 void elevator:: Open () {323 if (getnowState ()! = OpEning) {324 setnowState (Opening);325 record = 0;326 return;327}Thirty-two8 if (record < open) {329 record++;330 cout < < "elevator door";331 REturn;332}333 else / / / / / /334 record = 0;335 cout < < "open the door";Three hundred and thirty-sixSetnowState (Opened);337 if (JudgeOut ()) OUT ();338 if (JudgeIn ()) IN ();Three hundred and thirty-nineJudgeClose ();340}341}Three hundred and forty-two343 bool elevator:: JudgeOut () {344 if (getAll ()! = 0) {345 int j = floor;346 if (DiantiLi[j]! = 0) return true;347}348 returN false;349}Three hundred and fifty351 void elevator:: OUT () {352 if (record < out) {353 if (getnowState () = = Opened) {354 record++;355 cout < < "passengers are getting off the elevator".Three hundred and fifty-sixIf (DiantiLi[floor]! = 0) {357 cout < < "go down on the same level" < < DiantiLi[floor]< < "individual";358 setAll (-DiantiLi[floor]);359 DiantiLi[floor] =0;360}361 setnowState (Out);362 return;363}Three hundred and sixty-fourIf (getnowState () = = = Out) {365 record++;366 cout< < "passengers are getting off the elevator".Three67 return;368}369}370 else {371 cout < < "the passengers in the elevator are already there.Go down.372 record = 0;373 setnowState (Opened);374}375}Three hundred and seventy-six377 bool elevator: JudgeIn ()] / / if the elevator is not full, return to true and continue to enter.378 / / at present, we need to improve, because we first delete the human input into the array, so we need to have a judgement.379 if (getAll ())! = 13) {380 if ((empty ())] / / not empty, execute if statement.381 PNODE pTemp = p_head-> next; intUpnum1=0, downnum1=0;382 while (pTemp! = NULL) {383 if (ptemp-> data.)Getnowfloor () = = floor) {384 if ((ptemp-> data.getnowfloor () < ptemp-&)Gt; data.getgofloor ()) & & (getDir () > 0) {385 / / passengers are going up.Three hundred and eighty-sixReturn true;387}388 IF ((ptemp-> data.getnowfloor () > ptemp-> data.getgofloor ()) & & (getDir (= = = 0)) {Three hundred and eighty-nine/ / passenger downlink390 return true;Three hundred and ninety-one}392 if (near = = ptemp-> data.getnowfloor ()) {Three hundred and ninety-three/ / reached the maximum request floor. If it is in the same direction as the elevator, it will not change direction and allow access.394 / / if the elevator is not the same direction, then change direction if all two conditions areYes, give priority to the same direction395 if (getDir () = = 1) {Three hundred and ninety-sixIf (ptemp-> data.getnowfloor () < ptemp-> data.getgofloor ()) {Three hundred and ninety-sevenSetDir (1); upnum1++;398}Three hundred and ninety-nineIf (ptemp-> data.getnowfloor () > ptemp-> data.getgofloor ()) {Four hundredSetDir (0); downnum1++;401}Four hundred and twoIf (upnum1 = 0 & & downnum1 = 0) setDir (1);403}Four hundred and fourElse if (getDir () ==0) {405 if (ptemp-≫ data.getnowfloor () < ptemp-> data.getgofloor ()) {406 seTDir (1); upnum1++;407}408 if (PTEMp-> data.getnowfloor () > ptemp-> data.getgofloor ()) {Four hundred and nineSetDir (0); downnum1++;410}Four hundred and elevenIf (upnum1 = 0 & & downnum1 = 0) setDir (0);412}Four hundred and thirteenReturn true;414}415}Four hundred and sixteenPTemp = ptemp-> next;417}418}419}420 return false;421}Four hundred and twenty-two423 void elevator:: IN () {424 if (getAll () < 13) {425 if (record < in)Four hundred and twenty-six{427 if (getnowState () = = = Opened) {428 record++;Four hundred and twenty-ninePNODE pTemp = p_head-> next;430 while (pTemp! = NULL) {Four hundred and thirty-oneIf (ptemp-> data.getnowfloor () ==floor) n. / / first, people must be judged on the floor of the elevator.432 if (ptemp-> data.getnowfloor () < ptemp-> data.getgofloor (); & & (getDir () > 0) {Four hundred and thirty-three/ / the passenger is going up434 cout < < "<"; < ptemp-> data.getID () < < "a passenger is entering the elevator";435 DiantiLi[ptemp-> data.getgofloor ()] + = 1;Four hundred and thirty-sixSetAll (1);437 Remove (ptemp-> data.getID ());Four hundred and thirty-eightSetnowState (In);439 return;Four hundred and forty}441 if ((ptemp-> data.getnowfloor () > ptemp-> data.getgofloor ()) & &Amp; (getDir () = = 0)) {442 / / passenger downlink443 cout < <"<"; < ptemp-> data.getID () < < "a passenger is entering the elevator";444 DiantiLi[ptemP-> data.getgofloor ()] + = 1;445 setAll (1);446 ReMove (ptemp-> data.getID ());447 setnowState (In);Four hundred and forty-eightReturn;449}450}451 pTemp = pTemp-> next;452}453}454 if (getnowState () = = = In) {Four hundred and fifty-fiveRecord++;456 cout < < "passengers are entering the elevator";457 return;Forty-five8}459}460 else {461 cout < < "passengers have entered the elevator";Four hundred and sixty-twoRecord = 0;463 setnowState (Opened);464}465}466 else {Four hundred and sixty-sevenCout < < "the number of elevators has reached the maximum".468 setnowState (Closing);469}470}Four hundred and seventy-one472 voidElevator:: Close () {473 if (record < close) {474 record++;475 setnowState (Closing);476 cout < < "the elevator is closing the door";477}478 else {479 record = 0;Four hundred and eightyCout < < "the elevator is closed";481 setnowState (Closed);482 if (near = = floor) / / / / / / electricityThe ladder has reached the maximum request floor. There may be 4 cases.483 if (empty () = = false||getAll () (=0).Four hundred and eighty-fourPNODE pTemp = p_head-> next; int OutPeople = 0;485 while (pTemp! = NULL) {Four hundred and eighty-sixIf (ptemp-> data.getnowfloor () = = floor) OutPeople + = 1;487 if (GETAll () = = 0 & & OutPeople! =0; / / there is someone in the elevator, someone outside determines where to go.488 if (getDir ())= = 1 & & (ptemp-> data.getgofloor () > floor)) / / Elevator upward, passengers ascending, open doorFour hundred and eighty-nineSetnowState (Opening);490}491 else if (getDir () = = = 0 &aMp; & (ptemp-> data.getgofloor () < floor)) / / Elevator downward, passengers descend, open the door.492 setnoWState (Opening);493}494}495 if (getAll () = = = =0 & & OutPeople! = 0) / / Elevator no one, there's somebody outside.496 if (getDir (= = 1)) / / elevator up.Four hundred and ninety-sevenIf (ptemp-> data.getgofloor () > floor)498 setnoWState (Opening);499 else if (ptemp-> data.getgofloor () < floor) {Five hundredSetDir (0); setnowState (Opening);501}Five hundred and two}503 if (getDir (= = 0)) / / Elevator downward.504 if (Ptemp-> data.getgofloor () < floor)505 setnowState (Opening);Five hundred and sixElse if (ptemp-> data.getgofloor () > floor) {Five hundred and sevenSetDir (1); setnowState (Opening);508}509}Five hundred and ten}511 if (getAll () = = 0 & & OutPeople==0) {Five hundred and twelve/ / there's no one in the elevator. There's no one outside. At this point, we should continue to send passengers up or down. First, go through the array to get the passengers to go to several floors.513 for (int i = 1; I < 1)0; i++) {514 if (DiantiLi[i] = 0) {515 if (I< floor) {setnowState (Down); setDir (0);}516 if (I > floor) {seTnowState (Up); setDir (1);}517}518}Five hundred and nineteenFive hundred and twenty}521 pTemp = ptemp-> next;522}523}Five hundred and twenty-fourElse if (empty () = = true & & getAll () = = 0) / / waiting queue no one, elevator no one.525 setnowStAte (Waiting); setDir (-1);526}527}528 else if (near! = floor) {Five hundred and twenty-nineIf (getDir () = = 1) {530 setDir (1); setnowState (Up);531}Five32 If (getDir () = = = 0) {533 setDir (0); setnowState (Down);Five hundred and thirty-four}535}536 NoPassenger ();537}538}Five hundred and thirty-nine540 void elevator:: MoveUp () {Five hundred and forty-oneIf (record< up) {542 record++;543 cout < < "the elevator is on the stairs";544 return;545}546 else {547 floor++;548 record = 0;549 if (JudgeOpen ())= = = = false)550 MoveUp ();551 else {552 cout < < "elevator is upstairs";Five hundred and fifty-threeSetnowState (Opening);554}555}Five hundred and fifty-six557}Five hundred and fifty-eight559 void elevator:: MoveDown() {560 if (record< down) {561 record++;562 cout < < "the elevator is going downstairs";Five hundred and sixty-threeReturn;564}565 else {566 floor--;567 record = 0;568 if (JUdgeOpen () = = = false) MoveDown ();569 else {570 cout < < "elevator is downstairs";Five hundred and seventy-oneSetnowState (Opening);572}573}574}Five hundred and seventy-five576 void elevator:: JudgeClose () {Fifty-seven7 if (getnowState () = = = Opened) {578 if (record < test) {579 record++;Fifty-eight0 cout < < "closing door detection";581 return;582}583 else {Five hundred and eighty-fourCout < < "closing the door detection end";585 if (getnowState () = = = Opened) {Five hundred and eighty-sixRecord = 0;587 Close ();588 return;589}Five hundred and ninetyElse / / / / suspect can be deleted591 record = 0;592 return;Five hundred and ninety-three}594}595}596 else return;597}Five hundred and ninety-eight599 void elevator:: MoveDirction (const int)Near) {600 //near is the farthest floor for passengers601 if (near< floor) {602 setnowState (Down);Six hundred and threeSetDir (0);604 MoveDown ();605 return;606}607 if (near> floor) {608 setnowState (Up);609 setDir (1);610 MoveUp ();611 return;Six hundred and twelve}613 if (getnowState () ==Opening) {614 Open ();615 return;616}Six hundred and seventeenIf (getnowState () = = = In) {618 IN ();619 JudgeClose ();620 return;Six hundred and twenty-one}622 if (getnowState () = = = Out) {623 OUT ();624 JudgeClose ();625 returN;626}627 if (getnowState () = = = Opened) {628 JudgeClose ();629 if (JudgeOuT ()) OUT ();630 if (JudgeIn ()) IN ();631 return;632}633 if (getnowState () = == Closing) {634 Close ();635 return;636}637 if (near = = floor& & E)Mpty () ==true& & getAll () ==0) {638 setnowState (Waiting);639 NoPassenger ();Sixty-four0 return;641}642 if (near = = floor) {643 setnowState (Opening);Six hundred and forty-fourOpen ();645 return;646}647}Six hundred and forty-eight649 void elevator:: JudgeGiveUp (int waittime) {Six50 / / when entering the system, it is first determined whether anyone will give up at that time.651 //waittime for the current time, for passengers and waiting time +wait to determine whether the passengers give up.652 PNODE ptemp=P_head-> next;653 while (pTemp! = NULL) {654 if ((ptemp-> data.getwhenwait () + PEO)Plewait) < = waittime) {655 cout < < "<"; < ptemp-> data.getID () < <"The famous passenger has given up waiting";656 Remove (ptemp-> data.getID ());657}658 pTemp = PtEmp-> next;659}660 NoPassenger ();661}Six hundred and sixty-two663 #endif / / ELEVATOR_H664 #pragma once

Node.h //The node class header file is used to create linked lists and linked list related operation functions.

1 #ifndef NODE_H 2 #define NODE_H 3 #include<iostream> 4 #include"passenger.h" 5 using namespace std; 6 7 typedef struct node { 8 passenger data; 9 struct node* next; 10 }NODE, *PNODE; 11 12 NODE head;//Head nodeThirteen14 PNODE p_head = & head; / / head pointer15 PNODE p_tail = & head; / / tail pointerSixteen17 void append (passeNger data) {18 PNODE pnew = (PNODE) malloc (sizeof (NODE));19 pnew-> data = data; pnew->Next = NULL;20 if (p_head = = NULL) {21 p_head = pnew;22 p_tail = pnew;Twenty-three}24 else {25 p_tail-> next = pnew;26 p_tail = pnew;27}28}Twenty-nine30 BOol empty () {31 if (p_head-> next = = NULL) return true;32 else return false;33}Thirty-fourThirty-fiveVoid Remove (int ID) {//data is the data stored by the node, and it is deleted after checking.36 int pnew = ID;37 PNODE pTemp = p_head-> nexT;38 If (ptemp-> data.getID () = = = pnew) {39 If (pTemp = = p_tail).FortyP_head-> next = ptemp-> next;41 p_tail = p_head;42 pTemp = NULL;43}44 else {/ / / / if multiple nodes45 p_head-> next = ptemp-> next;Forty-sixPTemp = NULL;47}48}49 while (pTemp! =NULL) {50 if (ptemp-> nex)T-> data.getID () = = pnew) {51 If (ptemp-> next = = p_tail) {52 ptemP-> next = NULL;53 p_tail = pTemp;54}55 else ptemp->Next = ptemp-> next-> next;56}57 pTemp = ptemp-> next;58}Fifty-nine}Sixty61 #endif / /! NODE_H62 #pragma once

main.cpp //Main function, mainly call the set attribute function of the passenger class, the JudgeGiveUp function of the elevator class, and the addpassenger function of the elevator class, and show how much T is now.

1 #include"elevator.h" 2 3 int main() { 4 long int time = 0; 5 int a = 0,j=1; 6 cout << "Please enter a number of passengers to use the elevator: "7 CIN > > a;8 passenger *people = new passenger[a+1];9 for (INT I = 1; I < a+1; i++) {10 people[i].setdata (I);11 cout < < endl;Twelve}13 elevator X;14 long int Time = 0;15 for (int b = 1; B < a + 1; b++) {SixteenIf (people[b].getwhenwait () > Time) Time = people[b].getwhenwait ();17}18 Time = a*1000; / / the setting here is not the optimal solution, and no suitable value has been found yet.19 for (; time < Time; time++) {20 cout < < tIME < < "t moment";21 for (int i = 1; I < a + 1; i++) {22 If (people[i].gEtwhenwait () = = = time) x.addpassenger (people[i]);23}24 cout < < "elevator in" < <X.getfloor () < < "layer";25 x.JudgeGiveUp (time);26 cout < < endl;Twenty-seven}28 delete[]people;29 return 0;30}