TP 3
Exercice corrigé du TP N.3 Fortran de Matmeca. Il traite des tableaux : initialisation, tableaux dynamiques, opérations... Il est aussi question de la création de type dérivé.
Exercice 11
program poly
implicit None
type polynome
integer :: degre = -1
real, dimension(0:100) :: coeff = 0
end type polynome
integer :: i, d
type(polynome) :: p, q, r, s, t
type(polynome) :: tmp
type(polynome)::s2
! Definition des polynomes
! P
p%degre = 2
p%coeff(0) = 3
p%coeff(2) = 1
! Polynomes q et r
q%degre = 3
do i=0,3
q%coeff(i) = 1
end do
r = q
r%coeff(3) = -1
! S
s%degre = 3
s%coeff(0) = 1.46
s%coeff(1) = 3.14
s%coeff(2) = 1./3.
s%coeff(3) = 1./11.
! T
t%degre = s%degre
t%coeff = -s%coeff
!Affichage des degres et coeff de chaque polynome
print *,
print *, "-------"
print *,'Le polynome p est'
print *,"degre",p%degre
print *,p%coeff(0:p%degre)
print *,
print *, "-------"
print *,'Le polynome q est'
print *,"degre",q%degre
print *,q%coeff(0:q%degre)
print *,
print *, "-------"
print *,'Le polynome r est'
print *,"degre",r%degre
print *,r%coeff(0:r%degre)
print *,
print *, "-------"
print *,'Le polynome s est'
print *,"degre",s%degre
print *,s%coeff(0:s%degre)
print *,
print *, "-------"
print *,'Le polynome t est'
print *,"degre",t%degre
print *,t%coeff(0:t%degre)
print *,
print *, "-------"
print *,'p + 10 X**5'
p%degre =5
p%coeff(5)=10
print *, "degre", p%degre
print *, p%coeff(0:p%degre)
! Q + R
print *,
print *, "-------"
print *,'Q + R'
tmp%coeff = q%coeff + r%coeff
! Calcul du degre de tmp
! cas 1 deg(Q) = deg(R)
if (q%degre==r%degre) then
d = q%degre
! Attention a ne pas créer une boucle infinie
do while (d>-1 .and. tmp%coeff(d)==0)
d = d-1
end do
tmp%degre = d
else
! cas 2 deg(Q) /= deg(R)
tmp%degre = max(q%degre, r%degre)
end if
if (tmp%degre==-1) Then
print*,'Q + R = 0'
else
print *, "degre", tmp%degre
print *, tmp%coeff(0:tmp%degre)
end if
! Q + R
print *,
print *, "-------"
print *,'S + T'
tmp%coeff = s%coeff + t%coeff
! Calcul du degre de tmp
! cas 1 deg(Q) = deg(R)
if (s%degre==t%degre) then
d = s%degre
! Attention a ne pas créer une boucle infinie
do while (d>-1 .and. tmp%coeff(d)==0)
d = d-1
end do
tmp%degre = d
else
! cas 2 deg(Q) /= deg(R)
tmp%degre = max(s%degre, t%degre)
end if
if (tmp%degre==-1) Then
print*,'S + T = 0'
else
print *, "degre", tmp%degre
print *, tmp%coeff(0:tmp%degre)
end if
end Program poly
Exercice 12
program exo12
implicit none
type bd
character(50) :: titre
character(30) :: editeur
integer :: nbvolumes
end type bd
integer, parameter :: nbBD = 6
type(bd), dimension(nbBD) :: biblio
integer :: i
biblio(1)%titre = "Quintett"
biblio(1)%editeur = "Dupuis"
biblio(1)%nbvolumes = 5
biblio(2)%titre = "Largo-Winch"
biblio(2)%editeur = "Dupuis"
biblio(2)%nbvolumes = 16
biblio(3)%titre = "XIII"
biblio(3)%editeur = "Dargaud"
biblio(3)%nbvolumes = 19
biblio(4)%titre = "Astérix"
biblio(4)%editeur = "Hachette"
biblio(4)%nbvolumes = 33
biblio(5)%titre = "TinTin"
biblio(5)%editeur = "Casterman"
biblio(5)%nbvolumes = 24
biblio(6)%titre = "De-Cape-et-de-Crocs"
biblio(6)%editeur = "Delcourt"
biblio(6)%nbvolumes = 8
print *, "Ces BDs ont plus de 20 volumes"
do i=1, nbBD
if (biblio(i)%nbvolumes>20) then
print *, "titre : ", biblio(i)%titre
print *, "editeur : ", biblio(i)%editeur
print *,
end if
end do
end program exo12