文档详情

fortran逻辑运算及选择结构4.ppt

发布:2017-04-16约2.17千字共25页下载文档
文本预览下载声明
第八章 常用算法的程序设计举例;一、引言; 例:计算职工工资。正常工资rate (yuan/h),一周超过40 hours时,超过部分按1.5 rate (yuan/h) 。;c Payroll with overtime program payroll read(*,*) rate, hours if(hours.gt.40.0) then regpay=rate*40.0 ovtpay=1.5*rate*(hours-40.0) else regpay=rate*hours ovtpay=0.0 end if pay=regpay+ovtpay write(*,*) rate=, rate, hours=, hours write(*,*) regular pay=, regpay, overtime pay=, ovtpay write(*,*) total pay=, pay end ;二、关系表达式;; 几点注意:;三、逻辑表达式;3. 逻辑运算符;4. 逻辑表达式;例如,A=3.5, B=5.0, C=2.5, D=1.0;算术运算符的运算对象是数值量,运算结果为数值。 关系运算符的运算对象是数值量,运算结果为逻辑量。 逻辑运算符的运算对象是逻辑量,运算结果仍为逻辑量。;四、用块IF实现选择结构;3. 一个块IF可以不包含ELSE语句和else块,???可以不包含then块。; read(*,*) grade if(grade.ge.60.) then if(grade.ge.70.) then if(grade.ge.80.) then write(*,*) A else write(*,*) B end if else write(*,*) C end if else write(*,*) D end if end;给a, b, c三个数排序,要求从小到大输出; read(*,*) a,b,c if(a.le.b) then else t=a a=b b=t end if if(b.le.c) then else t=b b=c c=t if(a.le.b) then else t=a a=b b=t end if end if write(*,*) a,b,c end;五、ELSE IF 语句; read(*,*) grade if(grade.ge.60.) then if(grade.ge.70.) then if(grade.ge.80.) then write(*,*) A else write(*,*) B end if else write(*,*) C end if else write(*,*) D end if end;几点说明:; read(*,*) grade if(grade.ge.80.) then write(*,*) A else if(grade.ge.70.) then write(*,*) B else if(grade.ge.60.) then write(*,*) C else write(*,*) D end if end; read(*,*) x if(x.lt.-10.0) then y=0.0 else if(x.lt.0.0) then y=2.0*x+20.0 else if(x.lt.20.0) then y=20.0 else if(x.lt.40.0) then y=30.0-0.5*x else if(x.lt.50.0) then y=50.0-x else y=0.0 end if write(*,*) y=,y end;六、逻辑IF语句; read(*,*) grade if(grade.ge.80.) write(*,*) A if(grade.ge.70..and.grade.lt.80.) write(*,*) B if(grade.ge.60..and.grade.lt.70.) write(*,*) C if(grade.lt.60.) write(*,*) D end;C 单价850,1
显示全部
相似文档