1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| program testlexico
implicit none
integer, parameter :: nbl=5, nbc=3
integer :: i, j
logical :: ok
integer, dimension(:), allocatable :: Vec
integer, dimension(:,:), allocatable :: T
integer :: pballocation
T = reshape( (/1,3,5,6,2,11,2,3,5,8,9,13,7,15,1/), (/nbl,nbc/) )
if(.not. allocated(Vec)) then
allocate(Vec(nbc),stat=pballocation )
if(pballocation .GT. 0) then
stop " Erreur: probleme memoire "
end if
end if
if(.not. allocated(T)) then
allocate(T(nbl,nbc),stat=pballocation )
if(pballocation .GT. 0) then
stop " Erreur: probleme memoire "
end if
end if
do i = 1, size(T,1)
write(*,"(99i4)") T(i,:)
end do
do
ok = .true.
do i = 1, size(T,1)
do j = i+1, size(T,1)
if (.not. CMP( T(i,:), T(j,:) ) ) then
Vec = T(i,:) ; T(i,:) = T(j,:) ; T(j,:) = Vec
ok = .false.
end if
end do
end do
if (ok) exit
enddo
write(*,"('Voici le tableau T dont les lignes sont triees dans l''ordre lexicographique')")
do i = 1, size(T,1)
write(*,"(99i4)") T(i,:)
end do
contains
function CMP(a,b)
integer, dimension(:), intent(in) :: a, b
integer :: k
logical :: CMP
! Compare 2 tableaux de dimensions 1 selon l'ordre lexicographique
! renvoie .TRUE. si a <= b
! renvoie .FALSE. si a > b
do k = 1, size(a)
if ( a(k) > b(k) ) then
CMP = .FALSE.
return
elseif ( a(k) < b(k) ) then
CMP = .TRUE.
return
endif
enddo
CMP = .TRUE. ! identiques
end function cmp
end program testlexico |
Partager