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
  • 55
    点赞
  • 252
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值