Fortran:运算符

声明:本博文翻译自:https://www.tutorialspoint.com/fortran/fortran_operators.htm

一个运算符对编译器来说是一个符号:告诉编译器执行特定的数学操作或是逻辑运算。
fortran语言提供了下面几种运算符:
Arithmetic Operators  算术运算符
Relational Operators  关系运算符
Logical Operations  逻辑运算符

1. Arithmetic Operators
+ : 表示加法运算
- : 表示减法运算
* : 表示乘法运算
/ : 表示除法运算
**: 表示乘方运算
示例程序如下:
Program arithmeticOp
  implicit none  
  integer :: a, b, c

  a = 5   
  b = 3  

  c = a ** b  !.. 乘方
  write(*,'(1x,a,g0)') "c = ", c

  c = a * b  !.. 乘法
  write(*,'(1x,a,g0)') "c = ", c

  c = a / b  !.. 除法
  write(*,'(1x,a,g0)') "c = ", c

  c = a + b  !.. 加法
  write(*,'(1x,a,g0)') "c = ", c

  c = a - b  !.. 减法
  write(*,'(1x,a,g0)') "c = ", c

End program arithmeticOp
用ivf编译器,执行结果如下:
c = 125
c = 15
c = 1
c = 8
c = 2

2. 关系运算符
== : 相等运算符
/= : 不等运算符
 > :  大于运算符
 < :  小于运算符
>= :  大于等于运算符
<= :  小于等于运算符
示例程序如下:
Program RelationalOp
  implicit none
  integer :: a, b

  a = 10; b = 20
  if ( a == b ) write(*,'(1x,a)') "a == b"
  if ( a  > b ) write(*,'(1x,a)') "a > b"
  if ( a  < b ) write(*,'(1x,a)') "a < b"
  if ( a >= b ) write(*,'(1x,a)') "a >= b"
  if ( a <= b ) write(*,'(1x,a)') "a <= b"
  if ( a /= b ) write(*,'(1x,a)') "a /= b"
End program RelationalOp
用ivf,执行结果如下:
a < b
a <= b
a /= b

3. 逻辑运算符
设A = .true. B = .false.
则(A.and.B) is false
(A.or.B) is true
!(A.and.B) is true
(A.eqv.B) is false
(A.neqv.B) is true
这里:.eqv.用来检查两个逻辑变量是否相等
     .neqv.用来检查两个逻辑变量是否不等
Program logicalOp
  implicit none  

  logical :: a, b

  a = .true.   
  b = .false. 

  if (a .and. b) then
     write(*,'(1x,a)') "Line 1 - Condition is true"
  else
     write(*,'(1x,a)') "Line 1 - Condition is false"
  end if

  if (a .or. b) then
     write(*,'(1x,a)') "Line 2 - Condition is true"
  else
     write(*,'(1x,a)') "Line 2 - Condition is false"
  end if

  ! changing values 
  a = .false.   
  b = .true. 

  if (.not.(a .and. b)) then
     write(*,'(1x,a)') "Line 3 - Condition is true"
  else
     write(*,'(1x,a)') "Line 3 - Condition is false"
  end if

  if (b .neqv. a) then
     write(*,'(1x,a)') "Line 4 - Condition is true"
  else
     write(*,'(1x,a)') "Line 4 - Condition is false"
  end if

  if (b .eqv. a) then
     write(*,'(1x,a)') "Line 5 - Condition is true"
  else
     write(*,'(1x,a)') "Line 5 - Condition is false"
  end if

End program logicalOp
用ivf,执行结果如下:
Line 1 - Condition is false
Line 2 - Condition is true
Line 3 - Condition is true
Line 4 - Condition is true
Line 5 - Condition is false

4. fortran中运算符的优先权
          Category                      Operator                         Associativity
Logical NOT and negative sign          .not. (-)                         Left to right
       Exponentiation                      **                            Left to right
       Multiplicative                      */                            Left to right
         Additive                          +-                            Left to right
         Relational                     < <= > >=                        Left to right
         Equality                        == /=                           Left to right 
        Logical AND                      .and.                           Left to right   
        Logical OR                       .or.                            Left to right
        Assignment                        =                              Right to left
示例程序如下:
Program precedenceOp
  implicit none  
  integer :: a, b, c, d, e
  a = 20   
  b = 10
  c = 15
  d = 5

  e = (a + b) * c / d      !.. ( 30 * 15 ) / 5
  write(*,'(1x,a,g0)') "Value of (a + b) * c / d = ",  e 

  e = ((a + b) * c) / d    !.. (30 * 15 ) / 5
  write(*,'(1x,a,g0)') "Value of ((a + b) * c) / d = ",  e 

  e = (a + b) * (c / d);   !.. (30) * (15/5)
  write(*,'(1x,a,g0)') "Value of (a + b) * (c / d) = ",  e 

  e = a + (b * c) / d;     !..  20 + (150/5)
  write(*,'(1x,a,g0)') "Value of a + (b * c) / d = " ,  e 

End program precedenceOp
用ivf,执行结果如下:
Value of (a + b) * c / d = 90
Value of ((a + b) * c) / d = 90
Value of (a + b) * (c / d) = 90
Value of a + (b * c) / d = 50
  • 5
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值