声明:本博文翻译自: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
Fortran:运算符
最新推荐文章于 2024-06-22 17:27:40 发布