`
chemingliang
  • 浏览: 131113 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

FORTRAN 90 程序典型示例

 
阅读更多

 

1 FORTRAN 90 程序示例代码

 

!------------------------------------------------------------------------------------!

! Code NAME: people.F90                                                                                     !

! Code DESC: This code contains module, structure(type),pointer                                !

! array,subroutine et al. usage.                                                                             ! 

!------------------------------------------------------------------------------------!

! IGSNRR of CAS  shuchangs@126.com                                                                   !

!------------------------------------------------------------------------------------!

 

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

 
2 运行结果
 
$ ifort people.F90
$ ./a.out 
 The result is below when the subroutine is called:
 The People Information----------------------------------------
 Basic Information:
 ID-CARD:           1 NAME:SHU Chang SEX:GirlAGE:          20
 Archive Information:
 Archive Year:        2001 Archive History:      WELL                                              
 Archive Year:        2002 Archive History:      WELL                                                  
 Archive Year:        2003 Archive History:      WELL                                                  
 Archive Year:        2004 Archive History:      WELL                                              
 Archive Year:        2005 Archive History:      WELL                                             
 Archive Year:        2006 Archive History:      WELL                                             
 Archive Year:        2007 Archive History:      WELL                                             
 Archive Year:        2008 Archive History:      WELL                                              
 Archive Year:        2009 Archive History:      WELL                                             
 Archive Year:        2010 Archive History:      WELL                                             
 Archive Year:        2011 Archive History:      WELL                                             
 Archive Year:        2012 Archive History:      WELL                                           
 Archive Year:        2013 Archive History:      WELL                                              
 Archive Year:        2014 Archive History:      WELL                                            
 Archive Year:        2015 Archive History:      WELL                                             
 Archive Year:        2016 Archive History:      WELL                                             
 Archive Year:        2017 Archive History:      WELL                                             
 Archive Year:        2018 Archive History:      WELL                                             
 Archive Year:        2019 Archive History:      WELL                                             
 Archive Year:        2020 Archive History:      WELL                                             
 ---------------------------------------------------------------
 The result is here after the values being assigned:
 The People Information----------------------------------------
 Basic Information:
 ID-CARD:           2 NAME:LIU Fei   SEX:Boy AGE:          22
 Archive Information:
 Archive Year:        2001 Archive History:      WELL                                              
 Archive Year:        2002 Archive History:      WELL                                             
 Archive Year:        2003 Archive History:      WELL                                              
 Archive Year:        2004 Archive History:      WELL                                              
 Archive Year:        2005 Archive History:      WELL                                              
 Archive Year:        2006 Archive History:      WELL                                              
 Archive Year:        2007 Archive History:      WELL                                             
 Archive Year:        2008 Archive History:      WELL                                             
 Archive Year:        2009 Archive History:      WELL                                            
 Archive Year:        2010 Archive History:      WELL                                             
 Archive Year:        2011 Archive History:      WELL 
 Archive Year:        2012 Archive History:      WELL                                            
 Archive Year:        2013 Archive History:      WELL                                              
 Archive Year:        2014 Archive History:      WELL                                             
 Archive Year:        2015 Archive History:      WELL                                            
 Archive Year:        2016 Archive History:      WELL                                             
 Archive Year:        2017 Archive History:      WELL                                             
 Archive Year:        2018 Archive History:      WELL                                             
 Archive Year:        2019 Archive History:      WELL                                             
 Archive Year:        2020 Archive History:      WELL                                        
 Archive Year:           0 Archive History:      1?
 Archive Year:           0 Archive History:
 
 ---------------------------------------------------------------
 The student Information is below:
 The People Information----------------------------------------
 Basic Information:
 ID-CARD:           3 NAME:CHEN Qing SEX:GirlAGE:          18
 Archive Information:
 Archive Year:        2001 Archive History:      WELL                                              
 Archive Year:        2002 Archive History:      WELL                                              
 Archive Year:        2003 Archive History:      WELL                                              
 Archive Year:        2004 Archive History:      WELL                                             
 Archive Year:        2005 Archive History:      WELL                                              
 Archive Year:        2006 Archive History:      WELL                                             
 Archive Year:        2007 Archive History:      WELL                                             
 Archive Year:        2008 Archive History:      WELL                                             
 Archive Year:        2009 Archive History:      WELL                                   
 Archive Year:        2010 Archive History:      WELL                                              
 Archive Year:        2011 Archive History:      WELL                                              
 Archive Year:        2012 Archive History:      WELL                                              
 Archive Year:        2013 Archive History:      WELL                                             
 Archive Year:        2014 Archive History:      WELL                                              
 Archive Year:        2015 Archive History:      WELL                                              
 Archive Year:        2016 Archive History:      WELL                                              
 Archive Year:        2017 Archive History:      WELL                                              
 Archive Year:        2018 Archive History:      WELL                                              
 ---------------------------------------------------------------
 The Student Information----------------------------------------
 The family-members number is:           3
 They are respectively:
 Father    
 Mother    
 Self      
 ---------------------------------------------------------------
 
3  使用Fortrain小结
(1)Fortran中使用指针(pointer)必须进行初始化(即指向Target 对象),否则会造成内存错误(segmentation fault)。
(2)Fortran中的指针数组相当于动态数组。
(3)结构体类型中调用成员函数可以使用‘%’或‘.’符。
(4)Fortran中变量定义永远在过程体(执行体)之前,比如
        integer :: r=5
         print *,'hellow world'
        不能为:
        print *,'hellow world'
        integer :: r=5
(5)Fortran不区分大小写。
(6)Fortran变量限定符大致有:变量类型符(integer,real,character...),public(private),target,pointer,save,parameter,dimiension等等。

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics