Just uploaded a completely simulate our school library elevator program, there are readers can download to see (http://download.csdn.net/user/xiadasong007 ).
(Due to a post from Baidu, I was forced to be busy with this program all afternoon. Fortunately, it is quite simple)
Let's take a break today. Let's take a look at "Hero-Season One" and talk about the code implementation process tomorrow to see what is difficult?
2009.3.12
Simple Analysis of elevator control simulation code:
To simulate the operation of an elevator, you need to draw a picture. There should be no problem in drawing a line or a square. In this way, you can first draw the basic structure of the elevator and then add a button, add and click messages for each button.
Elevator simulation is important in two places. The first is how to let the elevator run where we need it, and the second is how to run the elevator after we press multiple buttons at will.
Note that the elevator has a characteristic: the last time, the next time, the middle process will not change the direction.
Set floor height h;
Each time the timer sends a message, the elevator goes one step.
First point: Let the elevator run to the floor we need
1: at the starting time, the elevator is on the first floor. For example, if we want to go to the fifth floor, we need to move the elevator up to a height of 4 * H, however, it is possible that when the elevator is on the fifth floor (the stairs are not on the second floor, 2.5 * H higher than the second floor), someone wants to go to the fourth floor. At this time, the elevator must be able to change its target.
2: it is very important to select the variable of the elevator running status. Poor selection may cause the elevator to fail to meet our requirements. I have selected two variables in this program, one is the number of elevator steps m_icount, and the other is the current floor m_icurrentfloor of the elevator. M_icount is used to count, the elevator takes an H height in 60 steps, m_icount is initialized to 0, the elevator takes a step, m_icount + = 1, so once m_icount = 60, m_icurrentfloor + = 1 (up) or m_icurrentfloor-= 1 (down), reset m_icount to 0, and continue counting.
In this way, no matter where the elevator runs, we can know its specific location. And only when m_icount = 60, the elevator has the opportunity to stop, so that the elevator will not be parked in some inexplicable places.
3: when the elevator stops, the corresponding button on the floor should be invalid.
4: when the elevator reaches the desired floor, you can stop the timer and enable it after a while. (A new timer can be used, but two timers cannot coexist)
The second key point: how to save the button:
1: for elevator users, the elevator must meet the needs of all people, so the elevator must be able to save the button clicked by each customer.
2: it is a good choice to use the list to save it, but what is the most suitable? Think about several containers in STL. You don't have to think about them. The first choice is set containers. The containers can be automatically sorted. This function is really good.
3: But now the elevator is running up to the fifth floor, and there are still people going to the 1st, 2nd, 4th, 7th, and 9th floors. As mentioned above, there is no influence on the distance of the elevator once, therefore, the elevator will go to the 7th and 9th floors first, and then go to the 4th, 2nd and 1st floors.
In view of this, we need to use two set objects to save the required information. One is saved upstairs, and the other is saved downstairs.
4: in this way, the message sent by the button is very simple, that is, to save its name to the set object.
5: The timer will check set every time it responds. When going upstairs, check set: Begin (). When going downstairs, check set: end ()-1 to obtain the latest information, adjust the target in time.
6: if the elevator reaches the required floor, delete the hierarchy number in the Set set. If it is deleted, it indicates that the operation has to be switched or stopped. In the program, m_bdown controls the direction of the elevator (one at a time.
Other details are omitted.
Note: This program only writes the content of the core part, and all other additional functions are deleted from
If you have any questions, please try again.
2009.3.13