Пример #1
0
int main(int argc, const char * argv[]) {

	Car *myCar = new Car(true);
	Ship *myShip = new Ship();

	// 1)
	cout << "Vehicles velocity:" << endl;
	cout << "Car: " << myCar->getMaximumVelocity() << endl;
	cout << "Ship: " << myShip->getMaximumVelocity() << endl;
	cout << endl;

	// 2)
	cout << "Vehicles length:" << endl;
	cout << "Car: " << myCar->getLength() << endl;
	cout << "Ship: " << myShip->getLength() << endl;
	cout << endl;

	// 3)
	AmphibicVehicle *myAmphibicVehicle = new AmphibicVehicle(true);
	cout << "Amphibic vehicle: " << endl;
	cout << "Current mode: " << myAmphibicVehicle->printUsageMode() << endl;
	cout << "Change mode: " << myAmphibicVehicle->changeUsageMode() << endl;
	cout << "Current mode: " << myAmphibicVehicle->printUsageMode() << endl;
	cout << "Change mode: " << myAmphibicVehicle->changeUsageMode() << endl;
	cout << "Current mode: " << myAmphibicVehicle->printUsageMode() << endl;
	cout << endl;

	// 4)
	cout << "Car wheels:" << endl;
	cout << "Has summer wheels: " << myCar->hasSummerWheels() << endl;
	cout << endl;

	// free the memory
	// Hint: see the reference to the keyword delete
	// [1] http://www.cplusplus.com/doc/tutorial/dynamic/
	delete myCar;
	delete myShip;
	delete myAmphibicVehicle;

	return 0;
}
Пример #2
0
vector<Event> Intersection::runEvent(Event ev, double time, double dt)
{
    vector<Event> eventsToReschedule(0);
    EventType type = ev.getType();
    switch(type)
    {
    case enteringRoad:
    {
        Road* road = ev.getRoad();
        bool hasMoved = road->moveCarsInJA();
        if(hasMoved) //if at least one car has moved
        {
            Intersection* previousIntersection = road->getIntersectionA();
            //Reschedule event running the TravelingArea and CommonQueue in current road
            eventsToReschedule.push_back(Event(outgoingRoad,road,previousIntersection));
        }
        break;
    }
    case outgoingRoad:
    {
        Road* road = ev.getRoad();
        bool hasMoved = road->moveCarsInCQandTA(dt);
        if(hasMoved) //if at least one car has moved
        {
            //Rescheduling the entrance of the car that enter the network
            eventsToReschedule.push_back(Event(enteringNetwork,NULL,this));
            //Rescheduling entry of cars from the road entering the intersection
            for(vector<Road*>::iterator it = enteringRoads.begin(); it != enteringRoads.end(); it++)
            {
                eventsToReschedule.push_back(Event(enteringRoad,*it,this));
            }
        }
        break;
    }
    case enteringNetwork:
    {
        EnterCarIt it = enteringCars.begin();
        while(it != enteringCars.end()) //We go through every entering car
        {
            EnterCarIt it2 = next(it);
            Car* currentCar = it->first;
            Road* targetRoad = it->second;
            if(currentCar->getEnteringTime() <= time) //If it is time for the current car to enter the network
            {
                if(targetRoad->getRoomLeftInTravelingArea() >= currentCar->getLength()) //If there is room for the car
                {
                    MoveResult result = currentCar->tryToEnterRoad(targetRoad); //Run the moving function in the car
                    targetRoad->getTA()->addCar(currentCar); //Add the car to the new traveling area
                    enteringCars.erase(it); //erasing the car
                    it = it2; //updating the iterator
                }
                else
                {
                    currentCar->postponeEnteringTime(dt); //We postpone the car's entering time
                    it++; //updating the iterator
                }
            }
            else
                it++;
        }
        break;
    }
    }
    return eventsToReschedule;
}