1234567
#include <iostream> #include <cstdlib> #include <math.h> #include <time.h> #include <iomanip> using namespace std;
123456789101112131415
int Random::poissonRandom(double expected) { int n=0; double limit; double x; limit = exp(-expected); x = rand()/((double)RAND_MAX+1); while (x>limit) { n++; x *= rand()/((double)RAND_MAX+1); } return n; }
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
/***************************************************************** * does everything pretty much *******************************************************************/ int main(int argc, char* argv[]) { } srand(2500); // FIX FIX FIX FIX use poisson distribution from above instead Random random; int endtime = 0; int lrefused = 0; int trefused = 0; int idle = 0; int planesLand = 0; int planesTakeOff = 0; int time = 0; int planeNum = 0; double arrivalsPerTime = 0; double takeOffsPerTime = 0; cout << "Units of time the simulation will run: "; cin >> endtime; cout << "Expected number of arrivals per time unit: "; cin >> arrivalsPerTime; cout << "Expected number of take offs per time unit: "; cin >> takeOffsPerTime; Queue takeOffQueue(5); Queue landQueue(5); int randnumber1, randnumber2; randnumber1 = random.poissonRandom(arrivalsPerTime); randnumber2 = random.poissonRandom(takeOffsPerTime); cout << randnumber1 << " " << randnumber2 << endl; for (int curtime = 1; curtime < endtime; curtime++) { for (int i = 0; i < randnumber1; i++) // plane to land { planeNum = i; if (landQueue.full())// landing queue is full { // refuse plane to land lrefused++; cerr << "refused"; } else { landQueue.insert(planeNum); // add plane to landing queue planesLand++; cerr << "plane landed: "; } } for (int i = 0; i < randnumber2; i++) { planeNum = i;// create new plane if(takeOffQueue.full()) //takeoff queue is full { trefused++; // refuse plane to takeoff } else { takeOffQueue.insert(planeNum); // add plane to takeoff queue } } if (!landQueue.empty()) // landing queue is not empty { landQueue.remove(); // land next plane in landing queue } else if (!takeOffQueue.empty()) // takeoff queue is not empty { takeOffQueue.remove(); // allow next plane in takeoff queue to takeoff planesTakeOff++; } else { idle++; // runway idle } } int numOfPlanes = randnumber1 + randnumber2; int readyLand = (landQueue.getBack() - landQueue.getFront()); int readyTakeOff = (takeOffQueue.getBack() - takeOffQueue.getFront()); cout << endl << endl; cout << "Total number of planes processed: 90 " << endtime - (lr\efused + trefused) << endl; cout << "Number of planes landed: 50 " << planesLand << endl; cout << "Number of planes taken off: 36 " << planesTakeOff << endl; cout << "Number left ready to land: 0 " << readyLand << endl; cout << "Number left ready to take off: 0 " << readyTakeOff << endl; cout << "Number of planes refused use: 4 " << lrefused + trefused << endl; cout << "Percentage of time runway idle: 14.00% " << idle / endtime << endl; cout << "Average wait time to land: 0.56 " << idle * randnumber1 << endl; cout << "Average wait time to take off: 4.06 " << idle * randnumber2 << endl; return 0; }