INSTITUT FÜR MATHEMATIK UND
WISSENSCHAFTLICHES RECHNEN
Text Karl-Franzens-Universitaet logo uni graz
     Präsentation     Mitarbeiterinnen    Forschung    Lehre    Allgemeines    Bibliothek    Fakultät    Uni Graz    Home
 

Humor in der Mathematik - Löwenjagd

<Prev | Index | Next>


How to catch a lion in the desert

Method in C++

// 
// LionHunt.cp        v1.0
// 
// (c) 1998 Wolfgang Thaller
// All rights reseved. Do not distribute without permission from
//           wolfgang.thaller@gmx.net
// 
// 
// This program is intended to run on THE UNIVERSAL COMPUTER.
// Contact the system administrator (probably God or someone like that)
// in order to get an account.
// 
// This program demonstrates the CatchLion routine by hunting and catching
// a lion in the Sahara desert, taking a photo of it and then setting it
// free again. The CatchLion routine can be used for catching lions in any
// desert on any planet. It can also be easily adapted for hunting other
// animals, including, but not limited to, camels.
//
// One last thing:
// Don't blame me if the program has a bug and causes the Sahara to crash.
    // These #includes are interfaces to the Universe's Operating System
#include <universe.h>
#include <planet.h>
#include <desert.h>
#include <lion.h>
#include <cage.h>
    // <showimage.h> just contains a little utility routine
#include <showimage.h>
Lion * CatchLion(Desert * inDesert, Cage * inCage = NULL);
    // if inCage is not specified, a new cage will be automatically created
    

const double kMinimumLionWeight = 50;
    // Do not hunt for lions lighter than kMinimumLionWeight kilograms.
    // !!! Insert the weight of a newborn lion here! !!!

Lion * CatchLion(Desert * inDesert, Cage * inCage)
{
    if(inCage == NULL)            // if there is no cage
        inCage = new Cage();    // create a new one
        
    if(!inCage -> IsIn(inDesert))    // if the cage is not yet in the desert
        inCage->MoveTo(inDesert);    // move it there
        
    /* The Algorithm works like this:
     *    Starting with the heaviest sand grain, we look at every grain in
     *  the desert until we have found a grain with a lion sitting on it.
     *  The grains that were already examined are moved into a temporary desert.
     *  We cannot delete them because they might have camels or people on them.
     *  The search is aborted when we have found a lion or when the heaviest
     *  sand grain that is left in the desert is lighter that a newborn lion.
     *
     *  While we are doing all this, time has to be stopped. Otherwise, the lion
     *  could keep walking from one sand grain to another.
     *  Also, we don't want the camels to get frightened when they are moved
     *  to the temporary desert --- they just won't have the time to notice it.
     */
    
    inDesert->GetUniverse()->StopTime();    // stop the flow of time
    
    Desert *tempDesert = new Desert("LionHunt Temporary Desert");
        
    SandGrain *aGrain = inDesert->FindHeaviestGrain();

    Lion    *theLion = NULL;
    
    while(aGrain && aGrain->GetWeight() > kMinimumLionWeight)
    {
        Animal *theAnimal;
        
        theAnimal = aGrain->AnimalOnGrain();
        
        if(theAnimal != NULL)
        {
            theLion = dynamic_cast<Lion*> theAnimal;
            if(theLion)
                break;        // we have found a lion!
        }
        
        aGrain->MoveTo(tempDesert);
    }
    
        // If we have a pointer to a lion, we can move it to the cage
    if(theLion)
        theLion->MoveTo(inCage);
        
        // Now we have to move back all sand grains from the temporary desert
    aGrain = tempDesert->FindAnyGrain();
    while(aGrain)
    {
        aGrain->MoveTo(inDesert);
        
        aGrain = tempDesert->FindAnyGrain();
    }
    delete tempDesert;    // we do no longer need the temporary desert
    
    inDesert->GetUniverse()->ResumeTime();    // resume the flow of time

        // return the lion, which should be in the cage now
    return theLion;
}

int main()
{
        // First, get a pointer to the current universe
        // (we don't want to catch a lion in a parallel universe)
    Universe * theUniverse = Universe::GetCurrentUniverse();
    
        // Then, find planet Earth
    Planet * theEarth = theUniverse->FindPlanet("Earth");
    if(theEarth == NULL)
    {

        // We didn't find the right planet!
        // A full-featured version of this program should include a list of
        // the planet's name in as many languages as possible and try finding
        // the planet by another name.
    }
    
        // We'll build our own cage
    Cage *theCage = new Cage();
    
        // And now, catch the lion!
    Lion *theLion = CatchLion(theEarth->FindDesert("Sahara"),theCage);
    
        // As a proof that we have cought a lion, we'll show a photograph of
        // the cage with the lion in it
    ::ShowImage(theCage);
    
        // Now that we know that the CatchLion function works, we can set the
        // lion free again:
    theCage->Open();
    
        // We can't delete the cage because the lion might be still in it.
        // It will be deleted during the next cosmic reboot.
}


<Prev | Index | Next>

footer bild
  AKTUELL    SITEMAP   SUCHE   ENGLISCH   UNI GRAZ         Betreuer: Bernd Thaller / 13.11.04