[Fortran] 纯文本查看 复制代码
MODULE FUNC
TYPE PERSON
CHARACTER ( LEN = 10 ) :: NAME
REAL :: HEIGHT , WEIGHT
END TYPE
TYPE PPERSON
TYPE(PERSON) , POINTER :: P
END TYPE
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(J)%P%HEIGHT < P(I)%P%HEIGHT ) THEN
TEMP = P(I)
P(I) = P(J)
P(J) = TEMP
END IF
END DO
END DO
RETURN
END SUBROUTINE
END MODULE
PROGRAM MAIN
USE FUNC
IMPLICIT NONE
TYPE(PERSON) , TARGET :: P(3) = (/PERSON("CHEN" , 180.0 , 56.0 ), &
PERSON("HUANG", 190.0 , 75.0 ),&
PERSON("LIU", 177.0 , 89.0 )/)
TYPE(PPERSON) :: PT(3)
INTEGER I
FORALL (I = 1:3)
PT(I)%P => P(I)
END FORALL
CALL SORT(PT)
WRITE (*,"(3(A8,F6.1,F5.1/))")( PT(I)%P , I = 1,3)
STOP
END PROGRAM
[Fortran] 纯文本查看 复制代码
TYPE(PPERSON) , target :: P (:)
TYPE(PERSON) , pointer :: TEMP
TEMP => P(I)%P
P(I)%P => P(J)%P
P(J)%P => TEMP