[Learning] C ++ implementation of the elevator algorithm

xiaoxiao2021-03-06  104

It is well known that object-oriented programming is more suitable for the description of real life, more embossed the spirit of software industrialization, so most software development work is carried around OOP's idea. But in the actual problem of real life, how to conduct object-oriented analysis and design for the system studied? This article introduces you how to analyze and design practical problems with an actual example.

One. Problem Description:

This example is an elevator passenger problem, the problem is described below:

A total of 10 floors in a trade center is included. In order to handle the problem, there is a limited conditions:

(1) The operating rules of the elevator are: each layer can be reached.

(2) The maximum multiply of each elevator is a k person (K value can be determined between 10 to 20 people according to the simulation).

(3) At the beginning of the simulation, the elevator is randomly located in any layer that meets the rules of operation, which is an air ladder.

(4) After the simulation begins, N-person (> 20 people) starts the ladder activity at the 1st floor of the International Trade Center.

(5) The floor to each person is random, starting waiting for the elevator at the bottom.

(6) After each person takes the elevator to reach the designated floor, then go to the other floor, according to such push, when each person is taken, the value can be determined between 3 and 10 times according to simulation) , The next to the bottom layer and end the ladder behavior. When all people end the ladder behavior, this simulation ends.

(7) The elevator running speed is S second / layer (the S value can be determined between 1 to 5 according to the simulation), and each person can be determined between 2 to 10 per person according to the simulation.

(8) The direction of the elevator operation is determined by first requiring the requester, and the requester will not allow the requester to change the current operation direction of the elevator, unless it is an unqualified empty ladder.

Finally developed software requirements:

(1) Design an easy-to-understand interface, dynamically display the passenger and operation of each ladder (upper, down or stop), dynamically display the staff of the staff and requirements of the staff; dynamic display begins to the current time.

(2) Variable parameters (k, n, m, l, s, t) should be input from the dialog when the program begins. System analysis and design:

The key to the OOP analysis is to classify the classification of the problem (class analysis and design, this point is not the same as the process-oriented flow chart design), which is included in the entire system, each class Contains those attribute features and behavioral features. For the above elevator problem, there are two classes: ie elevators and passengers (from the designed object description, there is a problem, even if this step is also analyzed). Elevators should reflect all information about elevator status and behavior, and the passenger class should also reflect this information. The expression of the above problem is now organized as follows: Passengers:

CPassenger {bool binlift; // Whether or not in the elevator BOOL BSIGNAL; / / Send request flag BOOL BSTART; / / The flyzed start flag (FALSE flag End) cstring flag; // identifies the number of each person and the number of demand layers IATFLOOR; // The floor INT ITOFLOOR; // To get the floor int ilifts; // has been taken by the elevator, the number of Int number; // passengers 得}

Elevator: Celevator {BOOL BSTART; / / Simulation Start Sign IT IATFLOOR; // Currently Layer number 5 Starting point int itofloor; // is about to go to the layered INT ipassengers; // Elevator number BOOL BOOL BSTOP; // Elevator stop sign BOOL BISEMPTYOPERATION; / / ELT whether or not the carray m_passengers; // The store is in the elevator. The first parameter represents the category stored in the collection. The second parameter represents the way to access the elements inside the set, which is the referenced access method, which is accessed by passing a 32-bit pointer. It also has a double advantage of address access (high efficiency) and value delivery, and now the access to large objects is generally advocated using this approach.

In addition, this also references the View class, defined as follows:

Class CliftsimulationView: Public CFormview {Uint M_NTIMER; INT K; INT N; INT L; INT S; INT T; Int FLOOR; CPassenger M_Passenger [100]; // Maximum 100 passengers CELEVATOR M_ELEVATOR; // A elevator Int Isrcfloor, Idesfloor; // represents the startup floor and the termination floor representing the passenger, using IEMPTYSRCFLOOR in ONTIMER, IemptyDesfloor; // Depending on the air-load floor and the termination floor, use in Ontimer int itimes [41] DWORD ELAPSetime; Void deleteColor (int src); Void DrawColor (INT SRC, INT INCREMENT);}; here is generally set to the members of each object, in order to facilitate storage, all variables are defined as public types, so It can improve the efficiency of storage, of course, it also destroys the idea of ​​OOP packaging, reducing the isolation between objects and objects. Here is the problem is not very complicated, so we use the front method. At this point, the design is basically over, of course, many times, it can be designed very well, it is often designed to be designed, then the problem is dealing with the original design. Add and delete work. It is important to pay special attention to the ultimate purpose of class design to reduce the coupling of the system, reaching the separation between program logic and data, more conducive to code preparation and maintenance. Three. Code composition:

The previous design has been completed, then the remaining is the implementation of the system logic part. For the overall system, there should be an event detection system to detect the status of the system for each passenger and elevator. In order to send or modify the necessary information, the cycle of the detection system is set for 1 second (can be implemented with a timer), and then simulate parallel with a serial manner. If you summarize your ideas, it should be the same as the principle of cartoon: Time to make multiple fixed and stationary pictures, as sequentially, turn it into a picture. Therefore, you can define a primary loop in the program, and the necessary initialization is performed outside of the loop. After entering, execute once per second, to override the currently generated object one by one, by them according to their current state and related Status variation rules determine if it is necessary to change, change to what is the state, and the design of the necessary object states according to the above behavior characteristics.

Based on the thoughts described above, the following list is listed mainly to list the "Event Detection System": (should trigger the second function in another function, that is, call Settimer (1, 1000, NULL))

Void CliftSimulationView :: ONTIMER (uint nidevent) // Mainly processed all logic {INT i = 1; int J = 1; int A; int flag1 = 0; CSTRING STR1, STR2; CSTRING SHOWTIME, SEN, M, H DWORD DWTIME; if (nidevent == 1) // The whole system is driven, this is the minimum time unit {if (this-> m_elevator.bstart = true) // Elevator in simulation {// Valve all passenger cyclic queries, Similar to the message loop, this is the elevator core program to be inlet for (i = 1; i <= this-> n; i ) {if (this-> m_passenger [i] .binlift == false && this-> m_passenger [i ] .bstart == true && this-> m_elevator.bstop == true && this-> m_elevator.bisemptyoperty == true) {// Polling each passenger request // Only when the elevator stops and passengers get up on the floor, Only the signal IF (this-> m_passenger [i] .bsignal == true) {// initializes passenger SRAND (:: gettickcount ()); a = rand ()% 10 1; // Generate 1 to 10 Get random tris-> m_passenger [i] .itofloor = a; // The passenger should go to the layer IF (a == this-> m_passenger [i] .iatfloor) // The floor is given the signal. IF (a == 10) this-> m_passenger [i] .iatfloor = a-1; else this-> m_passenger [i] .iatfloor = a 1; str1.format ("% D", this-> m_passenger [ I] .itofloor; str2.format ("% d", this-> m_passenger [i] .number); this-> m_passenger [i] .flag = STR2 "-" str1; this-> m_passenger [i] .bsignal = false; // Close her signal logo, give a passenger a chance this-> m_passenger [i] .binlift = true; // into Elevator (virtual) // Initialize the elevator, prepare this-> m_elevator.itofloor = this-> m_passenger [i ]-> m_passenger [i ]-> m_Elevator; this-> m_elevator.ipassengers = 1; this-> m_elevator.m_passengers.add (this-> m_passenger [i]); this-> m_elevator.bstop = false; // Indicates the elevator Run this-> iemptysrcfloor = this-> m_elevator.iatfllor;

this-> iemptydesfloor = this-> m_elevator.itofloor; this-> setdlgitemtext (IDC_STATIC18, (LPCTSTSTSTSTSTSTS (STR2 "-" STR1)); // Complete the no-load task this-> itimes [4] ; IF (this-> itimes [4] == 1) Settimer (4, 2000, 0); // Elevator no-load pick passenger BREAK; / / Each time you pick one}} // Decision End Simulation for (i = 1; i <= this-> n; i ) {if (this-> m_passenger [i] .bstart == true) FLAG1 = 1;} IF (Flag1 == 0) // End Simulation {KillTimer (1); This-> m_elevator.bstart = false; return;} / / Do not signal without passengers, the system enters the dead cycle for (i = 1; i <= this-> n; i ) {if (this-> m_paassenger [i] .binlift == false && this-> m_elevator.bstop == true && this-> m_elevator.bstart == true) {this-> m_passenger [i] .bsignal = true; // should be random Break;}}} Else {KillTimer (1); // If the elevator running flag is false, stop simulation} dwtime = :: gettickcount () -tis-> elapsetime; dwtime = dwtime / 1000; Sen.Format ("% D", DWTIME% 60 H.Format ("% D", DWTIME / 3600); M.Format ("% D", (DWTIME / 60)% 60); showtime = h ":" m ":" Sen This-> setdlgitemtext (IDC_STATIC16, ShowTime);} else if (nidevent == 4) // Elevator no-load operation (pick passengers) {if (this-> iemptysrcfloor == this-> iemptydededededededeDesfloor) // arrive at destination {IF (this-> Iemptysrcfloor! = 1 && this-> IemPtysrcfloor! = 10) {this-> DrawColor (IemPtysrcfloor, 1); this-> DrawColor (IEMPTYSRCFLOR, -1);} // Because it is. So set the elevator to receive signal status this-> m_elevator.bstop = true;

// Elevator does not run this-> m_elevator.bisemptyOperation = false; // For full load, prepare this-> m_elevator.iatFloor = this-> m_elevator.itofloor; this-> m_elevator.itofloor = this-> m_elevator.m_passengers [0 ] .itofloor; killtimer (4); this-> itimes [4] = 0; this-> isrcfloor = this-> m_elevator.iatfloor; this-> iDesfloor = this-> m_elevator.itofloor; this-> m_elevator.bstop = False; // Elevator Start this-> itimes [2] ; if (this-> itimes [2] == 1) Settimer (2, 2000, 0); // Start Passengers on Elevator} IF (Iemptysrcfloor DrawColor (Iemptysrcfloor, 1); IemPtysrcfloor ;} else {this-> DrawColor (Iemptysrcfloor, -1); Iemptysrcfloor ---;}} else if (nidevent == 2) // Elevator every 3 seconds, The elevator is a passenger, and there is a case where there is no passenger line {if (isrcfloor DrawColor (ISRCFLOOR, 1); isrcfloor ;} else if (isrcfloor> iDesfloor) {this-> DrawColor (isrcfloor, -1); isrcfloor ---;} if (isrcfloor == idesfloor) // arrived at the destination {this-> DrawColor (isrcfloor, 1); this-> DrawColor (ISR Cfloor, -1); // Because it is stationed, it is necessary to set the elevator to receive signal status this-> m_elevator.bstop = true; this-> m_elevator.iatfloor = this-> m_elevator.itofloor; // Uninstall passengers SRAND (:: gettickcount ()); a = rand ()% 10 1; // Generate a 1 to 10 random number this-> m_elevator.m_passengers [0] .iatfloor = this-> m_elevator.m_passengers [ 0] .itofloor; this-> m_elevator.m_passengers [0] .itofloor = a; // This passenger wants to get the number of layers THIS-> M_Elevator.m_Passengers [0] .ilifts ; if (this-> m_elevator.m_passengers [0 ] .ilifts == this-> l) this-> m_elevator.m_passengers [0] .bstart =

False; this-> m_elevator.m_passengers [0] .binlift = false; // Elevator outside this-> m_elevator.m_passengers.removeall (); this-> m_elevator.ipassengers -; // For the next no-load This-> m_elevator.bstop = true; this-> m_elevator.bisemptyopertyOperation = true; killtimer (2); this-> itimes [2] = 0;}} cformview :: Ontimer (nidevent); 4. in conclusion:

At this point, all of the design processes have been completed, and all of the code has been debugged under the VC6.0 Win2000 platform. From the above analysis, it can be seen that when the idea of ​​OOP is actually designed, the key is the design, class design must be done by dividing the system object domain, and the specific members of the class must be made by the system. Part of the function is determined. In short, class design is not a day two days, and long-term practices may meet the extent of proficiency.

转载请注明原文地址:https://www.9cbs.com/read-100678.html

New Post(0)