第一范文网 - 专业文章范例文档资料分享平台

Fortran语言基础 - 图文

来源:用户分享 时间:2025/6/1 3:42:06 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

山东建筑大学

write(*,*) \

read(*,*) height ! 读入身高 write(*,*) \

read(*,*) weight ! 读入体重 if ( weight > height-100 ) then

! 如果体重大于身高减去100, 会执行下面的程序 write(*,*) \ else

! 如果体重不大于身高减去100, 会执行下面的程序 write(*,*) \ end if stop end

3.1.2 逻辑运算

Fortran90的逻辑判断运算符号:(括号内为Fortran77的逻辑判断缩写符号)

== 相等 (.EQ.) /= 不相等 (.NE.) > 大于 (.GT.) >= 大于等于 (.GE.) < 小于 (.LT.) <= 小于等于 (.LE.)

逻辑关系运算浮号:

.AND. 逻辑与 .OR. 逻辑或 .NOT. 逻辑非

.EQV. 两边表达式运算结果相同时,结果为真 .NEQV. 两边表达式运算结果不同时,结果为真

注意:逻辑判断运算优先级高于逻辑关系运算。应此下面的两种逻辑关系式时等价的:

21

数值分析程序设计——Fortran基础

a>=80 .and. a<90 (a>=80) .and. (a<90)

不过为明显逻辑运算关系,建议使用后一种写法。

3.1.3 多重判断IF-ELSE IF

多重判断流程: if (条件1)then

程序代码 else if (条件2) then

程序代码 else if (条件3)then

程序代码 else if (条件4)then

程序代码 else

程序代码 end if

利用多重判断编写成绩等级查询程序:

program ex0505 implicit none integer score character grade write(*,*) \ read(*,*) score

if ( score>=90 .and. score<=100 ) then grade='A'

else if ( score>=80 .and. score<90 ) then grade='B'

else if ( score>=70 .and. score<80 ) then grade='C'

else if ( score>=60 .and. score<70 ) then

22

山东建筑大学

grade='D'

else if ( score>=0 .and. score<60 ) then grade='E' else

! score<0 或 score>100的不合理情形 grade='?' end if

write(*,\ grade stop end

3.1.4 嵌套IF语句

IF (……) THEN IF (……) THEN IF (……) THEN ELSE IF (……) THEN ELSE END IF END IF END IF

例 判断平面上一个点位于第几象限。

program ex0508 implicit none real x,y integer ans

write(*,*) \ read(*,*) x,y if ( x>0 ) then

if ( y>0 ) then ! x>0,y>0 ans=1

23

数值分析程序设计——Fortran基础

else if ( y<0 ) then ! x>0, y<0 ans=4 else ! x>0, y=0 ans=0 end if

else if ( x<0 ) then if ( y>0 ) then ! x<0, y>0 ans=2

else if ( y<0 ) then ! x<0, y<0 ans=3 else ! x<0, y=0 ans=0 end if

else ! x=0, y=任意数 ans=0 end if

if ( ans/=0 ) then ! ans不为0时, 代表有解 write(*,\第',I1,'象限')\ else

write(*,*) \落在轴上\ end if stop end

3.2 浮点数及字符的逻辑运算

3.2.1 浮点数的逻辑判断

使用浮点数作逻辑运算,要避免使用“等于”判断。因为使用浮点数作计算时,有效位数有限,难免出现计算上的误差,理想的等号不一定会成立。

program ex0509 implicit none

24

搜索更多关于: Fortran语言基础 - 图文 的文档
Fortran语言基础 - 图文.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c9kxkv2vxon207lq1bafn_6.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top