📄 traffic.cpp
字号:
// FILE: traffic.cxx// Written by: --------(Zuwei LI, E-mail: zli@turing.une.edu.au)--------// A simple program to simulate two synchronized traffic lights. The// program prints a table indicating the average wait-time for cars coming// through the two lights.#include <iomanip.h> // Provides setw#include <iostream.h> // Provides cout#include "things.h" // Provides the Averager, BooleanSource & TrafficLight#include "queue2.h" // The Queue template class (with a peek function)// This is a function to simulate traffic flowing through two synchronized// traffic lights.// Parameters and return value:// Offset:// This is the number of seconds between the time that the// first light changes to green and the time that the second light changes to// green. For example, if offset is zero, then the two lights change to green// at the same time. If offset is 10 then the first light changes to green// 10 seconds ahead of the second.// Span:// This is the length of time that each light stays red or green. For// example, if span is 30, then each light stays red for 30 seconds then// switches to green for 30 seconds, and so on.// Arrival_prob:// This is the probability during any given second that a car arrives at// the first traffic light. For example, if arrival_prob is 0.25, then there// is a 25% chance that a car arrives at the first light during any given// second and a 75% chance that no car arrives. (There will never be more// than one car arriving during a single second.)// Travel_time:// This is the number of seconds that it takes a car to travel from the// first traffic light to the second.// Total_time:// This is the total number of seconds that the function will simulate// at the traffic lights. (At the start of this simulation, there are no// cars waiting at either light).// Computation and Return value:// The function simulates cars arriving at the first stop light, waiting// for a green light, moving to the second stoplight, waiting again, and// moving through the second light. During a green light, at most one car// can move through the the light per second. As the computation proceeds,// the function uses an Averager to keep track of the average waiting time// for cars that arrived and pass through the first light. A second// Averager keeps track of the average waiting time for cars that arrived// and passed through the second light. The function's return value is the// sum of these two averages.// Special Case:// If no cars make it through the second light, then the return value is -1.double traffic( unsigned int offset, unsigned int span, double arrival_prob, unsigned int travel_time, unsigned int total_time ); int main( ){ const double ARRIVAL_PROB = 0.1; // Prob. of car arrival in a second. const unsigned int TRAVEL_TIME = 40; // Seconds to travel between lights. const unsigned int TOTAL_TIME = 6000; // Total seconds in the simulation. const unsigned int SPAN = 30; // How long is a light red or green? unsigned int offset; for (offset = 0; offset < 2*SPAN; offset++) { cout << "Offset of " << setw(2) << offset << " seconds "; cout << "results in average wait of "; cout << traffic(offset, SPAN, ARRIVAL_PROB, TRAVEL_TIME, TOTAL_TIME); cout << " seconds." << endl; } return EXIT_SUCCESS;}double traffic( unsigned int offset, unsigned int span, double arrival_prob, unsigned int travel_time, unsigned int total_time ){ Queue <unsigned int> arrival1_times,arrival2_times; unsigned int next; BooleanSource arrival(arrival_prob); TrafficLight trlt1(span,span),trlt2(span,span); Averager wait1_times,wait2_times; unsigned int current_second; for (current_second=1;current_second<=total_time;++current_second) { if (arrival.query()) arrival1_times.push(currend_second); if ( trlt1.is_green() && (!arrival1_times.empty())) { next=arrival1_times.front(); arrival1_times.pop(); wait1_times.next_number(current_second - next); arrival2_times.push(currend_second+travel_time); } trlt1.simulate_time(1); if ( trlt2.is_green() && (!arrival2_times.empty())) { next=arrival2_times.front(); if ((current_second - next)>=travel_time) { arrival2_times.pop(); wait2_times.next_number(current_second - next); } } trlt2.simulate_time(1); } // cout << " TrafficLight1 served " <<wait1_times.how_many_numbers()<<endl; //cout << " TrafficLight2 served " <<wait2_times.how_many_numbers()<<endl; if (wait1_times.how_many_numbers()>0) cout << " Total TrafficLight wait time " <<wait1_times.average()+wait2_times.average(); /* -- Student must implement the body of this function. The function should -- use two Queues--one for the cars waiting at each stoplight. Each -- Queue contains the arrival times of cars that are waiting to go -- through the light. The function should have just one BooleanSource -- to determine when new cars arrive at the first light. There should -- also be two TrafficLight objects and two Averager objects.*/}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -