Fortran快速入門

最近需要使用fortran,通過網上的資料,快速入門該語言

基本程序結構

program main !程序開始,main可以自定義
implicit none !告訴編譯器,所有變量需要聲明後才能使用,否則編譯失敗
!主體內容
stop !終止程序,相當與C exit(0)
end program main

數據類型,變量聲明

與老式C語言一樣,所有變量聲明在開頭,之後就不能聲明瞭
parameter聲明的變量,不可被修改,相當於C中的const

program fortran_learn
    implicit none !不允許隱式變量聲明

    integer, external :: myfunc !聲明函數,必須有返回類型
    integer :: a !int類型(4字節)
    integer(kind=8) :: b !int 8字節
    integer :: i !4字節
    integer sum
    real :: p,q !浮點數
    character :: ch !charl類型
    character(len=20) :: name !字符數組
    character(len=10) :: str1, str2, str !字符串
    logical :: true_or_false !邏輯類型
    complex :: cx !複數
    integer, dimension (5,5) :: matrix !聲明5x5矩陣,二維數組
    real, dimension (5) :: array !聲明數組
    integer, parameter :: constval = 123 !不能修改
    
	 !變量賦值
    a = 12345678
    print *, a, b
    name = "abcdefg"
    print *, name
    true_or_false = .true. !.false.
    print *, true_or_false, b !一次輸出多個
    cx = cmplx(23.1, -7) !23.1+7i
    print *, cx
    a = 2**3 !2^3
    print *, a
	array(2) = 23.4
    array = (/12.0, 234.4, 234.2, 45.2 , 123.45/) !數組初始化
    print *, array !打印數組
end program

數組操作補充,類似matlab

a(3:5)=(/3,4,5/) !a(3)=3,a(4)=4,a(5)=5 
a(1:5:2)=3 !a(1)=3,a(3)=3,a(5)=3 
a(3:)=5 !a(3)以及之後的所有元素賦值爲5 
a(1:3)=b(4:6) !類似於這種的要求左右數組元素個數相同 
a(:)=b(:,2) !a(1)=b(1,2),a(2)=b(2,2),以此類推

if select do-endo do-while

if有if-endif , if-else-endif , if-elseif-endif 這幾種模式,與C一眼
select就是C中的switch
do-endo就是列表循環
do-while 與C一致

常用運算符(if條件判斷)
(1)邏輯運算符
== /= > >= < <= ! Fortran 90用法
.EQ. .NE. .GT. .GE. .LT. .LE. ! Fortran 77用法
(2)涉及相互關係的集合運算符
.AND. .OR. .NOT. .EQV. .NEQV. ! 僅.NOT.連接一個表達式,其餘左右兩邊都要有表達式(可以是logical類型的變量) !.EQV.:當兩邊邏輯運算值相同時爲真, .NEQV.:當兩邊邏輯運算值不同時爲真

if (a >= 8) then !if-then-endif
     print *, "a>=8"
 end if

 if (true_or_false .and. .true.) then !兩個都是true
     print *, "true_or_false and true is true"
 else
     print *, "true_or_false and true is not all true"
 end if

 ch = 'A'
 ! select結構
 select case (ch)
     case ('A')
         print *, "A"
     case ('B')
         print *, "B"
 end select

 ! do-while 循環結構
 a = 1
 b = 100
 sum = 0
 do i = a, b
     sum = sum + i
 end do
 print *, "1 to 100  ", sum

 sum = 0
 do i = a, b, 2 ! i+=2 
     sum = sum + i
 end do
 print *, "odd sum  ", sum

函數與子程序

子程序使用call來調用,且子程序沒有返回值,參數是地址的形式傳遞,返回值是作爲參數
函數需要聲明後才能使用,跟變量的聲明類似,返回變量就是自身的函數名
(函數需要聲明的原因,數組使用()來索引,如果不聲明無法區分是數組還是函數)

integer, external :: myfunc !在外部使用該語句聲明函數
integer :: a,b,c
c = myfuc(a,bc) !調用函數

function myfunc(a, b)
    implicit none
    integer :: myfunc, a, b !聲明返回值類型和函數參數
    myfunc = a * b
end function myfunc

call mysub(a,b)  !調用子程序
subroutine mysub(a, b)
    !intent: 意圖屬性允許指定與參數的過程中使用的意向
    implicit none
    integer, intent(in) :: a !a不會被改變
    integer, intent(out) :: b !b可能會被改變
    integer :: c
    print *, "call mysub"
    !a = 123 ! a是輸入的變量,不可以被修改
    b = a + 1
end subroutine mysub

遞歸函數

recursive function myfactorial (n) result (fac) !遞歸函數 result指定返回值,默認返回值爲函數名
    implicit none

    integer :: fac
    integer, intent (in) :: n !輸入的變量、
    select case (n)
    case (0:1)
        fac = 1
    case default
        fac = n * myfactorial (n-1)
    end select
end function myfactorial

模塊module

聲明模塊

module constants
    ! 模塊中一般是聲明函數或者過程
    implicit none

    real, parameter :: pi = 3.1415926536 !parameter聲明,相當於const
    real, parameter :: e = 2.7182818285

contains !內部過程或函數,變量不可在這聲明
    subroutine show_consts()
        print*, "Pi = ", pi
        print*,  "e = ", e
    end subroutine show_consts

end module constants

使用模塊

program fortran_learn
    use constants !聲明使用的模塊,該句在第一句
    implicit none !不允許隱式變量聲明
	call show_consts() !調用模塊過程
	print *, pi !模塊公有變量
end program	

使用private修飾後的變量,外部不可以訪問,例如

module constants
    ! 模塊中一般是聲明函數或者過程
    implicit none
    real, private :: val !外部不可訪問

contains !內部過程,變量不可在這聲明
    subroutine set_val(val2)
        integer, intent(in) :: val2
        val = val2
    end subroutine set_val

    subroutine print_val()
        print *, "val is ", val
    end subroutine print_val

end module constants

program fortran_learn
    use constants !聲明使用的模塊
    implicit none !不允許隱式變量聲明
    
    call set_val(2) !設置val
    call print_val() !打印val
    !print *, val !失敗
end program
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章