#include #include #include #include #include #include //using namespace std; enum result{AIRBORN,GROUNDED,TAKEOFF,LANDING,LANDED,CRASHED}; class aircraft { public : aircraft(int fuel,bool isairborn,int landingtime,int takeofftime,const int ctime ){ ftime = fuel; ltime = landingtime; ttime = takeofftime; airborn = isairborn; time = ctime; } result tick(){ //Increase the time ++time; if(airborn == true){//Is the aircraft in the air? //tick fuel --ftime; if(ftime < ltime) return CRASHED; else return AIRBORN; }else return GROUNDED; } result landPlane(){ if(ftime < ltime) //How did this slip through return CRASHED; else return LANDED; } int getDelay(){ return time; } private : bool airborn; int totalfuel,ftime,ltime,ttime,time; }; class airport{ public : airport(){ } void start(){ landed =0,toff=0; std::cout << "Airport simulator starting." << std::endl; std::cout << "Please enter data for the following feilds" << std::endl; getInput(lt,"Minutes required for a plane to land"); getInput(tt,"Minutes required for a plane to takeoff"); getInput(ap,"Probability of a plane ariving in one minute"); getInput(dp,"Probability of a plane departing in one minute"); getInput(ft,"Minutes before a planes fuel runs out"); getInput(st,"Simulation in minutes"); //std::cout << std::endl << "Loading airspace" << std::endl; std::queue tmp; srand(time(NULL)); rclear = true; aircraft *currentplane; bool done =false; int i; crash=0; int ttf;//Ticks till free ct = -1; pc=0; while(st != ct){ ct++; // std::cout << "***Simulation for min "<< ct <<"***" << std::endl; if(rand() < ap * RAND_MAX){ //Plane enters the airspace pc++; // std::cout << "Adding an aircraft into the airspace " << std::endl; addAircraftAir(); } if(rand() < dp * RAND_MAX){ //Plane enters the landing queue addAircraftGround(); // std::cout << "Adding an aircraft on the ground" << std::endl; } // std::cout << "Planes left in queue to land"<< landing.size() << std::endl; // std::cout << "Planes left in queue to take off"<< takeoff.size() << std::endl; // std::cout << "Ticking all planes" << std::endl; //Tick planes in the air std::cout << landing.size()<< std::endl; while(!landing.empty()){ if(landing.empty()) break; *currentplane = landing.front(); switch(currentplane->tick()){ case CRASHED: // std::cout << "A plane has crashed!" << std::endl; delete currentplane; crash++; break; case AIRBORN : tmp.push(landing.front()); break; } landing.pop(); } for(i=tmp.size();!tmp.empty();tmp.pop()){ landing.push(tmp.front()); } if(!tmp.empty()){ // std::cout<< "The tmp queue was not emptied out" << std::endl; } while(!takeoff.empty()){ *currentplane = takeoff.front(); switch(currentplane->tick()){ case GROUNDED: tmp.push(takeoff.front()); takeoff.pop(); //std::cout << "A plane is currently grounded" << std::endl; break; } } for(i=tmp.size();!tmp.empty();tmp.pop()){ takeoff.push(tmp.front()); } if(!tmp.empty()){ std::cout<< "The tmp queue was not emptied out" << std::endl; } if(!landing.empty() && rclear){ //Land the plane *currentplane = landing.front(); landing.pop(); ttf = lt; landed++; ldelay.push(ct-currentplane->getDelay()); rclear =false; std::cout << "Plane has landed " << std::endl; }else if(!takeoff.empty() && rclear){ //Plane takes off *currentplane = takeoff.front(); takeoff.pop(); ttf = tt; toff++; rclear =false; tdelay.push(ct-currentplane->getDelay()); std::cout << "Plane has taken off! " << std::endl; //system("PAUSE"); }else if(!rclear){ // std::cout << "Runway not clear! runway will clear in "<< ttf << std::endl; } if(ttf == 0){ rclear = true; } ttf--; } std::cout<< "Planes count: " << pc << std::endl; std::cout<< "Planes crashed: " << crash << std::endl; std::cout<< "Planes landed: " << landed << std::endl; std::cout<< "Planes tookoff: " << toff << std::endl; std::cout<< "Landing delay: " << getTDelay() << std::endl; std::cout<< "Takeoff delay: " << getLDelay() << std::endl; } double getTDelay(){ int i; int delay=0; if(tdelay.size() ==0) return 0; for(i=tdelay.size();tdelay.size() >0;tdelay.pop()){ delay += tdelay.top(); } return delay/i; } double getLDelay(){ int i; int delay=0; if(ldelay.size() ==0) return 0; for(i=ldelay.size();ldelay.size() >0;ldelay.pop()){ delay += ldelay.top(); } return delay/i; } void addAircraftAir(){ aircraft *plane; plane = new aircraft(ft,true,lt,tt,ct); landing.push(*plane); plane = NULL; // std::cout<< "Adding a plane into the air queue"<< std::endl; // std::cout<< "Landing queue size "<< landing.size() << std::endl; } void addAircraftGround(){ aircraft *plane; plane = new aircraft(ft,false,lt,tt,ct); takeoff.push(*plane); plane = NULL; // std::cout<< "Adding a plane on the ground queue"<< std::endl; } void getInput(int &input,std::string p){ std::cout << p << ':' << std::endl; std::cin >> input; } void getInput(double &input,std::string p){ std::cout << p << ':' << std::endl; std::cin >> input; } private : std::queue takeoff; std::queue landing; std::stack ldelay; std::stack tdelay; int ft,lt,tt,fs,st,ct,crash,pc,toff,landed; double ap,dp; bool rclear; }; int main(){ airport a; a.start(); return EXIT_SUCCESS; }