选择结构
例1 从键盘输入一个以摄氏度为单位的气温数字,若不小于35.0,则在屏幕上显示高温热浪这一要求
程序源码:
program ex01 implicit none real T write(*,*) "输入气温" read(*,*)T !从键盘输入一个数值,其后的T表示将输入的数值赋予变量 if(T>=35.0)then write(*,*)'T=',T,'高温热浪' end if end
逻辑IF语句
program ex01 implicit none real T write(*,*) "输入气温" read(*,*)T !从键盘输入一个数值,其后的T表示将输入的数值赋予变量 if(T>=35.0) write(*,*)'T=',T,'高温热浪' end
例题1:
选择语句形式多样,可以利用一种或多种选择 语句编写不同的程序代码实现同一个目的。选择语 句示例第八题:已知 U,V 风速,判断风向的问题, 课件中给出了具体的程序代码,本次作业请改写该 程序。 备注:风速一般保留至小数点后 1 位。
课件中具体程序代码: program ex0308 real u,v read *, u,v if (u>0.0) then if (v>0.0) then !u>0,v>0 print *, '西南风' else if (v<0.0) then ! u>0, v<0 print *, '西北风' else ! u>0,v=0 print *, '西风' end if else if (u<0.0) then if (v>0.0) then !u<0,v>0 print *, '东南风' else if (v<0.0) then ! u<0, v<0 print *, '东北风' else ! u<0,v=0 print *, '东风' end if else if (v>0.0) then !u=0,v>0 print *, '南风' else if (v<0.0) then ! u=0, v<0 print *, '北风' else ! u=0,v=0 print *, '无风' end if end if end
我的程序源码:
PROGRAM ex3 real u,v read*,u,v if(u>0.0)then if(v>0.0)print*,'西南风' if(v<0.0)print*,'西北风' if(v==0.0)print*,'西风' else if(u<0.0)then if(v>0.0)print*,'东南风' if(v<0.0)print*,'东北风' if(v==0.0)print*,'东风' else if(v>0.0)print*,'南风' if(v<0.0)print*,'北风' if(v==0.0)print*,'无风' end if END
运行截图:
标准答案:
改写后的程序1: program ex0308 real u,v read *, u,v if (u>0.0.and.v>0.0) print *, '西南风' if (u>0.0.and.v<0.0) print *, '西北风' if (u>0.0.and.v==0) print *, '西风' if (u<0.0.and.v>0.0) print *, '东南风' if (u<0.0.and.v<0.0) print *, '东北风' if (u<0.0.and.v==0.0) print *, '东风' if (u==0.0.and.v>0.0) print *, '南风' if (u==0.0.and.v<0.0) print *, '北风' if (u==0.0.and.v==0.0) print *, '无风' End 改写后的程序2: program ex0308 real u,v read *, u,v if (u>0.0) then SELECT CASE(INT(v*10)) CASE(1:) print *, '西南风' CASE(:-1) print *, '西北风' CASE(0) print *, '西风' END SELECT else if (u<0.0) then SELECT CASE(INT(v*10)) CASE(1:) print *, '东南风' CASE(:-1) print *, '东北风' CASE(0) print *, '东风' END SELECT else SELECT CASE(INT(v*10)) CASE(1:) print *, '南风' CASE(:-1) print *, '北风' CASE(0) print *, '无风' END SELECT end if end
例题2:
人体对气候条件感觉的舒适与否,首先取决于气温,其次湿度和风速等因素也起着重要作用。3.1节已经根据人体舒适度计算公式,编程计算2月23日武汉,深圳,温州,北京,哈尔滨重点疫情城市的人体舒适度指数。
本次作业要求在3.1节所编程序基础上,增加以下内容:
(1)利用选择结构语句,根据人体舒适度指数数值,判断人体舒适度指数等级。
(2)要求每个城市的结果单独一行输出,每一行中输出顺序为城市名、指数等级、体感。
注:体感只输出 ,前的文字即可,例如:寒冷。
1、人体舒适度计算公式:
其中:温度T设定为日平均气温,RH为相对湿度*100,V日平均风速
2、2月23日五个城市日平均气温、RH为相对湿度,V日平均风速:
|
武汉 |
深圳 |
温州 |
北京 |
哈尔滨 |
日平均气温 |
14.5 |
21.5 |
12.0 |
5.5 |
-7.0 |
相对湿度 |
47.% |
45.% |
42.% |
29.% |
58.% |
日平均风速(m/s) |
2.45 |
2.45 |
0.9 |
2.45 |
4.4 |
我的程序源码:
PROGRAM main REAL::I1=53.1 REAL::I2=61.8 REAL::I3=52.0 REAL::I4=43.2 REAL::I5=21.6 WRITE(*,"(8X,'体感 指数等级')") if(0<I1.and.I1<=25)then write(*,"(2x,'武汉 寒冷 1')") else if (25<I1.and.I1<=38)then write(*,"(2x,'武汉 冷 2')") else if (38<I1.and.I1<=50)then write(*,"(2x,'武汉 凉 3')") else if (50<I1.and.I1<=55)then write(*,"(2x,'武汉 凉爽 4')") else if (55<I1.and.I1<=70)then write(*,"(2x,'武汉 舒服 5')") else if (70<I1.and.I1<=75)then write(*,"(2x,'武汉 暖和 6')") else if (75<I1.and.I1<=80)then write(*,"(2x,'武汉 热 7')") else if (85<I1.and.I1<=90)then write(*,"(2x,'武汉 炎热 8')") else if (95<I1.and.I1<=100)then write(*,"(2x,'武汉 酷热 9')") END IF if(0<I2.and.I2<=25)then write(*,"(2x,'深圳 寒冷 1')") else if (25<I2.and.I2<=38)then write(*,"(2x,'深圳 冷 2')") else if (38<I2.and.I2<=50)then write(*,"(2x,'深圳 凉 3')") else if (50<I2.and.I2<=55)then write(*,"(2x,'深圳 凉爽 4')") else if (55<I2.and.I2<=70)then write(*,"(2x,'深圳 舒服 5')") else if (70<I2.and.I2<=75)then write(*,"(2x,'深圳 暖和 6')") else if (75<I2.and.I2<=80)then write(*,"(2x,'深圳 热 7')") else if (85<I2.and.I2<=90)then write(*,"(2x,'深圳 炎热 8')") else if (95<I2.and.I2<=100)then write(*,"(2x,'深圳 酷热 9')") END IF if(0<I3.and.I3<=25)then write(*,"(2x,'温州 寒冷 1')") else if (25<I3.and.I3<=38)then write(*,"(2x,'温州 冷 2')") else if (38<I3.and.I3<=50)then write(*,"(2x,'温州 凉 3')") else if (50<I3.and.I3<=55)then write(*,"(2x,'温州 凉爽 4')") else if (55<I3.and.I3<=70)then write(*,"(2x,'温州 舒服 5')") else if (70<I3.and.I3<=75)then write(*,"(2x,'温州 暖和 6')") else if (75<I3.and.I3<=80)then write(*,"(2x,'温州 热 7')") else if (85<I3.and.I3<=90)then write(*,"(2x,'温州 炎热 8')") else if (95<I3.and.I3<=100)then write(*,"(2x,'温州圳 酷热 9')") END IF if(0<I4.and.I4<=25)then write(*,"(2x,'北京 寒冷 1')") else if (25<I4.and.I4<=38)then write(*,"(2x,'北京 冷 2')") else if (38<I4.and.I4<=50)then write(*,"(2x,'北京 凉 3')") else if (50<I4.and.I4<=55)then write(*,"(2x,'北京 凉爽 4')") else if (55<I4.and.I4<=70)then write(*,"(2x,'北京 舒服 5')") else if (70<I4.and.I4<=75)then write(*,"(2x,'北京 暖和 6')") else if (75<I4.and.I4<=80)then write(*,"(2x,'北京 热 7')") else if (85<I4.and.I4<=90)then write(*,"(2x,'北京 炎热 8')") else if (95<I4.and.I4<=100)then write(*,"(2x,'北京 酷热 9')") END IF if(0<I5.and.I5<=25)then write(*,"(2x,'哈尔滨 寒冷 1')") else if (25<I5.and.I5<=38)then write(*,"(2x,'哈尔滨 冷 2')") else if (38<I5.and.I5<=50)then write(*,"(2x,'哈尔滨 凉 3')") else if (50<I5.and.I5<=55)then write(*,"(2x,'哈尔滨 凉爽 4')") else if (55<I5.and.I5<=70)then write(*,"(2x,'哈尔滨 舒服 5')") else if (70<I5.and.I5<=75)then write(*,"(2x,'哈尔滨 暖和 6')") else if (75<I5.and.I5<=80)then write(*,"(2x,'哈尔滨 热 7')") else if (85<I5.and.I5<=90)then write(*,"(2x,'哈尔滨 炎热 8')") else if (95<I5.and.I5<=100)then write(*,"(2x,'哈尔滨 酷热 9')") END IF END
运行截图:
标准答案:
program main implicit none real:: t1,rh1,v1,ichb1 character*6 name write(*,*)'城市名 指数 指数等级 体感' t1=14.5 rh1=47. v1=2.45 name= ' 武汉 ' ichb1=(1.8*t1+32)-0.55*(1-rh1/100.)*(1.8*t1-26.)-3.2*sqrt(v1) if(ichb1<=25.0) write(*,100) name,ichb1,' 1级 ','寒冷' if(ichb1>25.0.and.ichb1<=38.0) write(*,100) name,ichb1,' 2级 ','冷' if(ichb1>38.0.and.ichb1<=50.0) write(*,100) name,ichb1,' 3级 ','凉' if(ichb1>50.0.and.ichb1<=55.0) write(*,100) name,ichb1,' 4级 ','凉爽' if(ichb1>55.0.and.ichb1<=70.0) write(*,100) name,ichb1,' 5级 ','舒服' if(ichb1>70.0.and.ichb1<=75.0) write(*,100) name,ichb1,' 6级 ','暖和' if(ichb1>75.0.and.ichb1<=80.0) write(*,100) name,ichb1,' 7级 ','热' if(ichb1>80.0.and.ichb1<=85.0) write(*,100) name,ichb1,' 8级 ','炎热' if(ichb1>85.0) write(*,100) name,ichb1,' 9级 ','酷热' name= ' 深圳 ' t1=21.5 rh1=45. v1=2.45 ichb1=(1.8*t1+32)-0.55*(1-rh1/100.)*(1.8*t1-26.)-3.2*sqrt(v1) if(ichb1<=25.0) write(*,100) name,ichb1,' 1级 ','寒冷' if(ichb1>25.0.and.ichb1<=38.0) write(*,100) name,ichb1,' 2级 ','冷' if(ichb1>38.0.and.ichb1<=50.0) write(*,100) name,ichb1,' 3级 ','凉' if(ichb1>50.0.and.ichb1<=55.0) write(*,100) name,ichb1,' 4级 ','凉爽' if(ichb1>55.0.and.ichb1<=70.0) write(*,100) name,ichb1,' 5级 ','舒服' if(ichb1>70.0.and.ichb1<=75.0) write(*,100) name,ichb1,' 6级 ','暖和' if(ichb1>75.0.and.ichb1<=80.0) write(*,100) name,ichb1,' 7级 ','热' if(ichb1>80.0.and.ichb1<=85.0) write(*,100) name,ichb1,' 8级 ','炎热' if(ichb1>85.0) write(*,100) name,ichb1,' 9级 ','酷热' name= ' 温州 ' t1=12.0 rh1=42. v1=0.9 ichb1=(1.8*t1+32)-0.55*(1-rh1/100.)*(1.8*t1-26.)-3.2*sqrt(v1) if(ichb1<=25.0) write(*,100) name,ichb1,' 1级 ','寒冷' if(ichb1>25.0.and.ichb1<=38.0) write(*,100) name,ichb1,' 2级 ','冷' if(ichb1>38.0.and.ichb1<=50.0) write(*,100) name,ichb1,' 3级 ','凉' if(ichb1>50.0.and.ichb1<=55.0) write(*,100) name,ichb1,' 4级 ','凉爽' if(ichb1>55.0.and.ichb1<=70.0) write(*,100) name,ichb1,' 5级 ','舒服' if(ichb1>70.0.and.ichb1<=75.0) write(*,100) name,ichb1,' 6级 ','暖和' if(ichb1>75.0.and.ichb1<=80.0) write(*,100) name,ichb1,' 7级 ','热' if(ichb1>80.0.and.ichb1<=85.0) write(*,100) name,ichb1,' 8级 ','炎热' if(ichb1>85.0) write(*,100) name,ichb1,' 9级 ','酷热' name= ' 北京 ' t1=5.5 rh1=29. v1=2.45 ichb1=(1.8*t1+32)-0.55*(1-rh1/100.)*(1.8*t1-26.)-3.2*sqrt(v1) if(ichb1<=25.0) write(*,100) name,ichb1,' 1级 ','寒冷' if(ichb1>25.0.and.ichb1<=38.0) write(*,100) name,ichb1,' 2级 ','冷' if(ichb1>38.0.and.ichb1<=50.0) write(*,100) name,ichb1,' 3级 ','凉' if(ichb1>50.0.and.ichb1<=55.0) write(*,100) name,ichb1,' 4级 ','凉爽' if(ichb1>55.0.and.ichb1<=70.0) write(*,100) name,ichb1,' 5级 ','舒服' if(ichb1>70.0.and.ichb1<=75.0) write(*,100) name,ichb1,' 6级 ','暖和' if(ichb1>75.0.and.ichb1<=80.0) write(*,100) name,ichb1,' 7级 ','热' if(ichb1>80.0.and.ichb1<=85.0) write(*,100) name,ichb1,' 8级 ','炎热' if(ichb1>85.0) write(*,100) name,ichb1,' 9级 ','酷热' name= '哈尔滨' t1=-7.0 rh1=58. v1=4.4 ichb1=(1.8*t1+32)-0.55*(1-rh1/100.)*(1.8*t1-26.)-3.2*sqrt(v1) if(ichb1<=25.0) write(*,100) name,ichb1,' 1级 ','寒冷' if(ichb1>25.0.and.ichb1<=38.0) write(*,100) name,ichb1,' 2级 ','冷' if(ichb1>38.0.and.ichb1<=50.0) write(*,100) name,ichb1,' 3级 ','凉' if(ichb1>50.0.and.ichb1<=55.0) write(*,100) name,ichb1,' 4级 ','凉爽' if(ichb1>55.0.and.ichb1<=70.0) write(*,100) name,ichb1,' 5级 ','舒服' if(ichb1>70.0.and.ichb1<=75.0) write(*,100) name,ichb1,' 6级 ','暖和' if(ichb1>75.0.and.ichb1<=80.0) write(*,100) name,ichb1,' 7级 ','热' if(ichb1>80.0.and.ichb1<=85.0) write(*,100) name,ichb1,' 8级 ','炎热' if(ichb1>85.0) write(*,100) name,ichb1,' 9级 ','酷热' 100 format(1x,a6,3x,f5.1,4x,a8,4x,a4) end program main
标准答案运行截图: