corrections TD FORTRAN : sous-programmes
(jeu du plus ou moins)

Licence Techno Méca



c jeu du plus ou moins, version structurée en sous-programmes        
c Cours d'informatique LTM -IPST novembre 2004 - P. TRAU        
       
c choisit un nombre aléatoire entre 1 et la borne donnée en argument (compris)
        integer function tirernb(borne)
        implicit none
        integer n,borne
        call srand (time())
        n=mod (irand(),borne)+1
        tirernb=n
        end function tirernb

c demande un nombre, nécessairement dans les bornes données en argument
        integer function demander(binf,bsup)
        implicit none
        integer n,binf,bsup
10      print *,'entrez votre proposition'
        read *,n
        if ((n.lt.binf).or.(n.gt.bsup)) then
           print *, 'veuillez donner une proposition plus censée svp !!!'
           goto 10
        endif
        demander=n
        end function demander

c analyse la réponse proposée, et remet à jour les bornes
        subroutine analyser (nbat,nbp,binf,bsup)
        implicit none
        integer nbat,nbp,binf,bsup
        if (nbp.lt.nbat) then
           print *,'trop petit'
           binf=nbp+1
        elseif (nbp.gt.nbat) then
           print *,'trop grand'
           bsup=nbp-1
        else
           print *,'gagné'
        endif
        end subroutine analyser

c donne son avis sur le score obtenu
        subroutine score(s)
        implicit none
        integer s
        print *,'il vous a fallu',s,' tentatives'
        if (s.le.2) then
          print *,'score phénoménal'
        elseif (s.le.5) then
          print *,'c''est franchement bon'
        elseif (s.le.8) then
          print *,'c''est pas mal'
        elseif (s.le.12) then
          print *,'ce n''est pas génial'
        else
          print *,'c''est franchement nul'
        endif
        end subroutine score
       
c enfin le programme principal       
        program jeu
        implicit none
        integer tirernb,demander !il faut déclarer toutes les fonctions
        integer nbatrouver,prop
        integer nbessais,binf,bsup
        binf=1
        bsup=100
        nbatrouver=tirernb(bsup)
        do while (nbatrouver.ne.prop)
          prop=demander(binf,bsup)
          nbessais=nbessais+1
          call analyser (nbatrouver,prop,binf,bsup)
        enddo
        call score(nbessais)
        end program jeu

les TP/TD cours Fortran P. Trau