Article From:https://www.cnblogs.com/cugwangwei/p/9121676.html

# 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

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

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

}

}

（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{

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.

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

}

}

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.

}

## 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;
41     int DiantiLi[10] = {0};
42     int All = 0;

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

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}```