Reference no: EM13163987
Write a program that emulates the behavior of a bus. The bus must have a schedule ( a finite number of times for it to stop) and a certain capacity (number of passengers). In this program the bus has two states: stopped and going. Over time, events take place, such as stopping the bus ( for which the bus must already be going ), loading passengers, unloading passengers, or starting the bus. For each event, the current state of the bus must change accordingly. Each time the bus stops, it must load and unload passengers. The number of passengers that will be unloaded at every bus stop must be generated randomly. The number of passengers waiting for the bus at each stop is also generated randomly. The bus should load as many waiting passengers as possible without exceeding the bus capacity. Write a program that implements this model, with the following operation:
void stop_bus(); /* Stop the bus */
void start_bus(); /* Start the bus */
void load_pass(); /* Load passengers */
void unload_pass(); /* Unload passengers */
Note: These function prototypes lack parameters; add whatever parameters you feel are necessary so that the program works without global variables. The program's output should reflect the bus's actions by reporting each change in state, along with the number of passengers loaded and unloaded.
Hints:
Bus status transition diagram: ( Diagram in the link: https://www.dropbox.com/s/qwe455521mm9hex/BUS%20C%2B%2B.jpg )
Bus travel from the first station to the last station. In each station, bus needs to stop, unload passengers, load passengers, and start. However, based on the station, bus's status , and the number of passengers, those actions may do different things.
In the computation (or simulation), bus will repeat actions in each station. In such a iteration, siulation codes call functions stop_bus, unload_pass, load_pass, and start_bus. In a specific function, we need to handle all cases.
For example,
Load_passengers
Case 1: If bus status is "running", do noting (direct return)
Case 2: If bus is full, do nothing
Case 3: If bus is in the last station, do nothing
Case 4: If the number of passengers plus the number of wating people is more than the max capacity, only a part of waiting customers can get loaded (to change the number of passengers)
Case 5 : Or else, all waiting people get loaded (to change the number of passengers)
Inputs: station_id, max_station_id, bus_status, &passengers
Output: the number of people who get loaded
Unload_passengers
Case 1: If bus is "running", do nothing
Case 2: If bus is empty, do noting
Case 3: If bus is in the last station, all passengers get unloaded (to change the number of passengers to be 0)
Case 4: Or else, our program generates a random number which represents the unloaded passengers (to change the number of passengers)
Inputs: station_id, max_station_id, bus_status, &passengers,
Outputs: the number of people who get unloaded
Stop_bus
Case 1: if bus is in the first station, display "Welcome!"
Case 2: Or else display "We are in the station X."
Inputs: station id, &bus_status
Start_bus
Case1: If bus is in the last station, do nothing
Case 2: Or else display "We leave for our next station X"
Inputs: station_id, max_station_id, &bus_status