题目要求
设计程序,输入降水量
t
t
t,判断该降水量是
12
12
12 小时或
24
24
24 小时时间段内的,再依据图示降雨量,分类得出降水等级
题设分析
首先观察题目,发现涉及到“判断”类的字眼,于是初步确定使用选择结构。
选择结构可使用的程序结构如下:
-
if - endif 结构:
if (condition 01) then statement block end if
-
if - else - endif 结构:
if (condition 01) then statement block 01 else statement block 02 end if
-
if - else if - endif 结构:
if (condition 01) then statement block 01 else if (condition 02) then statement block 02 else if (condition 03) then statement block 03 ...... else statement block n end if
-
if 结构:
if (condition 01) statement block
-
select case - case - end select结构:
select case (variable) case (condition 01) statement block 01 case (condition 02) statement block 02 case (condition 03) statement block 03 ...... case defult statement block n end select
观察题设,发现该问题需要进行两次大的判断:
① 判断输入数据为12小时或24小时;
② 判断输入数据落在何种降雨量区间内。
第一个判断,可以通过显示“Is the input data rainfall within 12 hours?”后交由用户选择的方式进行判断。
即用户输入Y/N来进行判断输入数据的种类,若输入Y,则进入第一类雨量判断;若输入N,则进行第二类雨量判断。
可以将该段代码进行如下的书写:
character selc
print *,'Is the input data rainfall within 12 hours?(Y/N)'
read *,selc
if (selc.eq.Y.OR.selc.eq.y) then
call judgement_1
else if (selc.eq.N.OR.selc.eq.n) then
call judgement_2
else
print *,'Input error.'
end if
其中,judgement_1是接下来为了程序模块化而使用的第一类雨量判别的子程序,judgement_2是第二类雨量判别的子程序。
在这里,我们使用call()
语句进行子程序的调用。
接下来,书写判断雨量的两类子程序:
首先书写两类子程序的公需部分,即变量定义和用户输入雨量部分:
real pre
print *,'Please enter the precipitation rain fall.'
read *,pre
do while (pre.LT.0)
print *,'Input error.'
read *,pre
end do
然后书写第一类雨量判别的子程序:
Subroutine judgement_1(p)
inplicit none
real p
print *,'该次降雨的类型为'
if (p.LT.5) then
print *,'小雨'
else if (p.LT.15)
print *,'中雨'
else if (p.LT.30)
print *,'大雨'
else if (p.LT.70)
print *,'暴雨'
else if (p.LT.140)
print *,'大暴雨'
else
print *,'特大暴雨'
end if
end subroutine judgement_1
类似我们可以书写出第二类雨量判别的子程序(此处略过不谈)
经过模块化编写之后,我们可以初步完成我们的程序如下:
program As02
implicit none
character selc
real pre
print *,'Is the input data rainfall within 12 hours?(Y/N)'
read *,selc
do while (selc.ne."Y".OR.selc.ne."y".OR.selc.ne."N".OR.selc.ne."n")
print *,'Input error.'
read *,selc
end do
if (selc.eq.“Y”.OR.selc.eq.“y") then
print *,'Please enter 12 hours of precipitation rain fall.'
read *,pre
call judgement_1 (pre)
else
print *,'Please enter 24 hours of precipitation rain fall.'
read *,pre
call judgement_2 (pre)
end if
end program As02
Subroutine judgement_1 (p)
implicit none
real p
print *,'该次降雨的类型为:'
if (p.LT.5) then
print *,'小雨'
else if (p.LT.15)
print *,'中雨'
else if (p.LT.30)
print *,'大雨'
else if (p.LT.70)
print *,'暴雨'
else if (p.LT.140)
print *,'大暴雨'
else
print *,'特大暴雨'
end if
end subroutine judgement_1
Subroutine judgement_2 (p)
implicit none
real p
print *,'该次降雨的类型为:'
if (p.LT.10) then
print *,'小雨'
else if (p.LT.25)
print *,'中雨'
else if (p.LT.50)
print *,'大雨'
else if (p.LT.100)
print *,'暴雨'
else if (p.LT.250)
print *,'大暴雨'
else
print *,'特大暴雨'
end if
end subroutine judgement_2
上述程序即为题目要求的输入数据后判断降水类型的程序代码。
下面呈现程序设计的运行后的结果:
参考链接:
[1] Fortran学习笔记6(函数、子程序)
[2] 气象程序设计及绘图 6.1节相关内容