[Fortran] 纯文本查看 复制代码
!================== module func ===========================
module func
type person
character(len = 10) :: name
real :: height, weight
end type person
type pperson
type(person), pointer :: p
end type pperson
contains
subroutine sort(p)
implicit none
type(pperson) :: p(:)
type(pperson) :: temp
integer :: i, j, s
s = size(p, 1)
do i = 1, s-1
do j = i+1, s
if (p(i)%p%height < p(j)%p%height) then
temp = p(i)
p(i) = p(j)
p(j) = temp
end if
end do
end do
! return
end subroutine sort
end module func
!================ main program ==============================
program examSort
use func
implicit none
type(person), target :: p(5) = [person('chen', 180.0, 75.0), &
person('huang', 170.0, 65.0), &
person('liu', 175.0, 80.0), &
person('cai', 182.0, 78.0), &
person('xu', 178.0, 70.0) ]
type(pperson) :: pt(5)
integer :: i
forall (i=1:5)
pt(i)%p => p(i)
end forall
call sort(pt)
write(*, '(5(a8, f6.1, f5.1, /))') (pt(i)%p, i=1,5)
end program examSort
[Fortran] 纯文本查看 复制代码
!================== module func ===========================
module func
type person
character(len = 10) :: name
real :: height, weight
end type person
! type pperson
type(person), pointer :: p
! end type pperson
contains
subroutine sort(p)
implicit none
! type(pperson) :: p(:)
! type(pperson) :: temp
type(person), pointer :: p(:)
type(person), pointer :: temp
integer :: i, j, s
s = size(p, 1)
print *, 's', s
do i = 1, s-1
do j = i+1, s
! if (p(i)%p%height < p(j)%p%height) then
if (p(i)%height < p(j)%height) then
temp = p(i)
p(i) = p(j)
p(j) = temp
end if
end do
end do
! return
end subroutine sort
end module func
!================ main program ==============================
program examSortTestArrayPointer
use func
implicit none
type(person), target :: a(5) = [person('chen', 180.0, 75.0), &
person('huang', 170.0, 65.0), &
person('liu', 175.0, 80.0), &
person('cai', 182.0, 78.0), &
person('xu', 178.0, 70.0) ]
! type(pperson) :: pt(5)
!Error: Array pointer 'pt' at (1) must have a deferred shape or assumed rank
! type(person), pointer :: pt(5)
type(person), pointer :: pt(:)
integer :: i
! forall (i=1:5)
! pt(i) => p(i)
! end forall
pt => a
call sort(pt)
! write(*, '(5(a8, f6.1, f5.1, /))') (pt(i)%p, i=1,5)
write(*, '(5(a8, f6.1, f5.1, /))') (pt(i), i=1,5)
end program examSortTestArrayPointer