1 FORTRAN 90 程序示例代码
!------------------------------------------------------------------------------------!
! Code NAME: people.F90 !
! Code DESC: This code contains module, structure(type),pointer !
! array,subroutine et al. usage. !
!------------------------------------------------------------------------------------!
! IGSNRR of CAS [email protected] !
!------------------------------------------------------------------------------------!
MODULE peopleType
IMPLICIT NONE
!PRIVATE
TYPE,PUBLIC :: peopleT
INTEGER,POINTER :: idCard
CHARACTER *10,POINTER :: name
CHARACTER *4,POINTER :: sex
INTEGER,POINTER :: age
INTEGER,POINTER :: archiveYear(:)
CHARACTER *50,POINTER :: archiveHistory(:)
END TYPE peopleT
TYPE,PUBLIC :: studentT
TYPE(peopleT) :: p
INTEGER,POINTER :: familyMembersNum
CHARACTER *10,POINTER :: familyMembers(:)
END TYPE studentT
!----------------------------------------------------
! Declare single instance of peopleType
!----------------------------------------------------
TYPE(peopleT) , PUBLIC, TARGET, SAVE :: people
TYPE(studentT), PUBLIC, TARGET, SAVE :: student
PUBLIC :: peopleInit
PUBLIC :: peoplePrint
PUBLIC :: studentInit
PUBLIC :: studentPrint
CONTAINS
SUBROUTINE peopleInit()
INTEGER,TARGET :: NUMBER=001
CHARACTER *10,TARGET :: NAME='SHU Chang'
INTEGER,TARGET :: AGE=20
CHARACTER *4,TARGET :: SEX='Girl'
INTEGER :: I
people.idCard => NUMBER
people%name => NAME
people.sex => SEX
people.age => AGE
ALLOCATE(people.archiveYear(1:AGE))
ALLOCATE(people.archiveHistory(1:AGE))
!Assign values to pointer arrays:
DO I=1,AGE
people.archiveYear(I)=2000+I
people.archiveHistory(I)='WELL'
END DO
END SUBROUTINE peopleInit
SUBROUTINE peoplePrint()
INTEGER :: i
print *,'The People Information----------------------------------------'
print *,'Basic Information:'
print *,'ID-CARD:',people.idCard,'NAME:',people.name,'SEX:',people.sex,&
'AGE:',people.age
print *,'Archive Information:'
DO i=1,people.age
print *,'Archive Year:',people.archiveYear(i),'Archive History:',&
people.archiveHistory(i)
END DO
print *,'---------------------------------------------------------------'
END SUBROUTINE peoplePrint
SUBROUTINE studentInit()
INTEGER,TARGET :: n=3
student.familyMembersNum => n
ALLOCATE(student.familyMembers(1:n))
student.familyMembers=(/'Father','Mother','Self'/)
call peopleInit
END SUBROUTINE studentInit
SUBROUTINE studentPrint()
INTEGER :: i
call peoplePrint
print *,'The Student Information----------------------------------------'
print *,'The family-members number is:',student.familyMembersNum
print *,'They are respectively:'
DO i=1,student.familyMembersNum
print *,student.familyMembers(i)
END DO
print *,'---------------------------------------------------------------'
END SUBROUTINE studentPrint
END MODULE peopleType
!The main program begins here:
program main
!Use the module of peopleType above.
use peopleType
!Local pointer variables:
integer,pointer :: numberL
character(len=10),pointer :: nameL
character(len=4),pointer :: sexL
integer,pointer :: ageL
!Call the subroutine: peopleInit() to initialize the peopleT structure.
print *,'The result is below when the subroutine is called:'
call peopleInit
call peoplePrint
!Relate the free pointer and the target pointer
numberL => people%idCard
nameL => people.name
sexL => people.sex
ageL => people.age
!Revalue them
numberL = 002
nameL = 'LIU Fei'
sexL = 'Boy'
ageL = 22
print *,'The result is here after the values being assigned:'
call peoplePrint
call studentInit
!Revalue them
numberL = 003
nameL = 'CHEN Qing'
sexL = 'Girl'
ageL = 18
print *,'The student Information is below:'
call studentPrint
end program