// This is the answer to test1 // written by Peter Clarke // #include #include #include #include #include //---------------------------------- // Define an Aircraft class as requested class Aircraft { private: std::string m_name ; // Aircraft name int m_height ; // Current flying height float m_velocity; // Current velocity // These two go together float m_position; // Position at specific time float m_time; // time at which position known public: //------------------------- // initialisation method void initialise( std::string name, int height, float velocity, float position, float time ) { m_name = name ; m_height = height ; m_velocity = velocity; m_position = position; m_time = time ; } // ------------------------ // Method to change height by a specified amount void climb( int heightChange ) { // This method must change the state of the object // to reflect the new height i.e. we must change // the member variable : m_height // Calculate requested new height float requestedNewHeight = m_height + heightChange ; // Do some checks on the requested change // Maximum flying height is 40000 feet if( requestedNewHeight > 40000 ) { m_height = 40000 ; } else if( requestedNewHeight < 0 ) { m_height = 0 ; } else { m_height = requestedNewHeight ; } return ; } // ------------------------- // Method to update the position accorging to new time void updatePosition( float currentTime ) { // Calculate the time difference from when last known position was measured float timeDifference = currentTime - m_time ; if( timeDifference < 0 ) { std::cout << " Cant specify earlier times " << std::endl ; } else { // Change position according to velocity and time diff m_position += timeDifference * m_velocity ; // Dont foget to change recorded time as well m_time = currentTime ; } return ; } // ----------------------------------------------- // Method to print out name height and position to screen void dump( ) { std::cout << "The " << m_name << " is flying at " << m_height << " feet " << " and was last spotted at x = " << m_position << std::endl ; } // --------------------------------- // Extra method for part 3 // To detect a collision between object Aircraft and another Aircraft bool collisionDetect( Aircraft& other ) { // If not at same height then no problem if( m_height != other.m_height ) return false ; // If not in opposite directions then no problem if( m_velocity * other.m_velocity > 0. ) return false ; // Finally if difference in position is large enough noproblem if( fabs( m_position - other.m_position ) > 10. ) return false ; // If we get here we are in trouble return true ; } }; // End of Aircraft class //======================================================================= // Function to demonstrate use of Aircaft to answer part 1 void part1() { // Create two aircraft Aircraft easyJet ; Aircraft swissAir ; // Initialise them easyJet.initialise ( "FluffyTheCat" , 1000, 500.0, 150.0, 4.0 ) ; swissAir.initialise( "GenferDienst" , 2000, -520.0, 350.0, 3.0 ) ; // Tell them to print their status easyJet.dump() ; swissAir.dump() ; // Tell them to change height by 2000 feet easyJet.climb( 2000 ) ; swissAir.climb( 2000 ) ; easyJet.dump() ; swissAir.dump() ; // Update their positions float timeNow = 10.2 ; easyJet.updatePosition( timeNow ) ; swissAir.updatePosition( timeNow ) ; easyJet.dump() ; swissAir.dump() ; } //======================================================================== // Function to answer part 2 and part 3 void part2() { // ======================= Part 2 ============================= // create two vector to hold the air traffic std::vector eastBound ; std::vector westBound ; // Open airlist file std::ifstream trafficList( "airlist.dat" ) ; if( ! trafficList ) { std::cout << " Unable to open file " << std::endl ; return; } // Read file in a loop until end of file std::string n; int h ; float v ; float p ; float t ; while( trafficList ) { // Read in a lin from the file trafficList >> n >> h >> v >> p >> t ; // Fix up if( ! trafficList ) break ; // Create a plane and initialise it Aircraft newPlane ; newPlane.initialise( n, h, v, p, t ) ; // Put it into appropriate vector if( v > 0. ) { eastBound.push_back( newPlane ) ; } else { westBound.push_back( newPlane ) ; } } // End of read in loop // Now print out both vectors for( int inde =0; inde < eastBound.size() ; ++inde ) eastBound[inde].dump() ; for( int indw =0; indw < westBound.size() ; ++indw ) westBound[indw].dump() ; // ======================= Part 3 ============================= // Perform a double loop through the two opposing traffic vectors bool collisionImminent = false ; // Loop over eastbound planes for( inde=0; inde < eastBound.size() ; ++inde ) { //For each eastbound plane in turn, loop over westbound planes for( indw =0; indw < westBound.size() ; ++indw ) { // Ask if they are about to collide collisionImminent = eastBound[inde].collisionDetect(westBound[indw]) ; // If so tell westbound plane to climb if( collisionImminent ) westBound[indw].climb( 1000 ) ; } // End westbound loop } //end eastbound loop } //=============================== // Main program to run it all int main() { part1() ; part2(); return 1 ; }