Program. Sams Publishing Object-Oriented Programming in C++
Reklaam
Reklaam
14 3087 CH13 11/29/01 2:24 PM Page 675
Multifile Programs
675
4. Is there is a down floor request on this floor, and we are going down, load the passengers.
5. If there are no destinations or requests above or below, stop.
6. If there are destinations above us, go up.
7. If there are destinations below us, go down.
8. If we’re stopped or going up, and there is a floor request above us, and there are no other cars going up between us and the request, or above it and going down and closer than we are, go up.
9. If we’re stopped or going down, and there is a floor request below us, and there are no other cars going down between us and the request, or below it and going up and closer than we are, go down.
10. If no other rules apply, stop.
Rules 8 and 9 are rather complicated. They attempt to keep two or more cars from rushing to answer the same floor request. However, the results are not perfect. In some situations cars are slow to answer requests because they are afraid another car is on its way, when in fact the other car is answering a different floor request. The program’s strategy could be improved by allowing the decide() function to distinguish between up and down requests when it checks whether there are requests above or below the current car. However, this would further complicate d ecide()
, which is already long enough. We’ll leave such refinements to you.
State Diagram for the
ELEV
Program
We introduced UML state diagrams in Chapter 10, “Pointers.” Now let’s look at a state diagram for an elevator object. To simplify things a little, we’ll assume that there is only one person in the building and only one elevator in use. Thus there can be only one floor request at a time, and only one destination selected by the rider. The elevator car doesn’t need to worry about what the other cars are doing. Figure 13.6 shows how this looks.
In the diagram, “cd” stands for car destination, the button pushed inside the car, roughly corresponding to a value in the destination array in the program. Also, “fr” stands for floor request, the button pushed outside the car, corresponding to the floor_req variable.
The states are derived from the values of the current_dir variable plus the status of the car’s loading_timer and unloading_timer
. Because all the transitions are the result of time ticks, only the guard conditions are shown. The guards represent what the car finds out about floor requests and car destinations.
13
Laadi alla
Reklaam