[Fortran] 纯文本查看 复制代码
Module typedef
Type tdd
Integer :: stat = 0
Real :: tmax, tmin, total, average
End Type tdd
End Module typedef
Integer Function daysinyear(year, mon, day)
Integer :: year, mon, day
Integer :: daysinmonth(12) = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ]
If (((mod(year,4)==0) .And. (mod(year,100)/=0)) .Or. (mod(year,400)==0)) Then
daysinmonth(2) = 29
Else
daysinmonth(2) = 28
End If
daysinyear = sum(daysinmonth(:mon-1)) + day
End Function daysinyear
Program main
Use typedef
Implicit None
Real :: rain
Type (tdd), Allocatable :: t(:) ! 保存气温值
Type (tdd) :: total !日均温累加
Integer i, m, dy, start, end, year, mon, day, daysinyear, k
Character (Len=20) :: line, filename = 'CN174653 (12)'
Open (8, File='result.txt')
!初始日期转换为一年的第几天
Write (*, *) 'mon:'
Read (*, *) mon
Write (*, *) 'day:'
Read (*, *) day
Print *, daysinyear(year, mon, day)
start = daysinyear(year, mon, day)
!结束日期转化为一年的第几天
Write (*, *) 'mon:'
Read (*, *) mon
Write (*, *) 'day:'
Read (*, *) day
Print *, daysinyear(year, mon, day)
end = daysinyear(year, mon, day)
!年份与文件年份对应
year = 1979
!批量处理
Do k = 1, 30
year=year+1
!内部文件读写, 将 filename 第 8-11 字符换为相应年份
Write (filename(11:12), '(i2)') k
Open (7, File=filename)
!获取行数
i = 0
Do
Read (7, *, End=100) line
i = i + 1
End Do
100 m = i - 1 !//跳过第一行
Rewind (7)
Allocate (t(m))
Read (7, *)
!积温计算
Do i = 1, m
Read (7, *) dy, t(i)%tmax, t(i)%tmin, rain !读入数据
t(i)%average = (t(i)%tmax+t(i)%tmin)/2 !日均温
End Do
total%average = 0
!
Do i = start, end
total%average = total%average + t(i)%average !日均温累加=积温
End Do
Write (8, *) year,total%average ! 结果写入另一个文件
Deallocate (t)
Close (7)
End Do
Close (8)
End Program main