module typedef
type tdd
integer :: stat=0
real :: tmax , tmin , total , average
end type
end module
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='CN174654 (12)'
open(8,file='result.txt')
!批量处理
do k=1,30
!内部文件读写, 将 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,*)
!初始日期转换为一年的第几天
write(*,*)"year:"
read(*,*)year
write(*,*)"mon:"
read(*,*)mon
write(*,*)"day:"
read(*,*)day
print *,DaysInYear( year , mon , day )
start=DaysInYear( year , mon , day )
!结束日期转化为一年的第几天
write(*,*)"year:"
read(*,*)year
write(*,*)"mon:"
read(*,*)mon
write(*,*)"day:"
read(*,*)day
print *,DaysInYear( year , mon , day )
end=DaysInYear( year , mon , day )
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 !日均温累加=积温
enddo
write(8,*) total%average !
close(8)
end do
end PROGRAM
QQ截图20170201220710.png (9.12 KB, 下载次数: 325)
QQ截图20170201221715.png (21.58 KB, 下载次数: 318)
fcode 发表于 2017-2-2 12:46
你忘了 Close 7 和 deallocate(t)
module typedef
type tdd
integer :: stat=0
real :: tmax , tmin , total , average
end type
end module
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='CN174654 (12)'
open(8,file='result.txt')
!批量处理
do k=1,30
!内部文件读写, 将 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
!初始日期转换为一年的第几天
write(*,*)"year:"
read(*,*)year
write(*,*)"mon:"
read(*,*)mon
write(*,*)"day:"
read(*,*)day
print *,DaysInYear( year , mon , day )
start=DaysInYear( year , mon , day )
!结束日期转化为一年的第几天
write(*,*)"year:"
read(*,*)year
write(*,*)"mon:"
read(*,*)mon
write(*,*)"day:"
read(*,*)day
print *,DaysInYear( year , mon , day )
end=DaysInYear( year , mon , day )
do i=start,end
total%average=total%average+t(i)%average !日均温累加=积温
enddo
write(8,*) total%average ! 结果写入另一个文件
close(8)
deallocate(t)
Close(7)
end do
end PROGRAM
fcode 发表于 2017-2-2 16:07
write(*,*)"year:"
read(*,*)year
write(*,*)"mon:"
fcode 发表于 2017-2-2 16:07
write(*,*)"year:"
read(*,*)year
write(*,*)"mon:"
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 = 'CN174654 (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)
!批量处理
Do k = 1, 30
!内部文件读写, 将 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
Write (*, *) 'year:'
Read (*, *) year
Print *, daysinyear(year, mon, day)
start = daysinyear(year, mon, day)
end=start
!积温计算
Do i = start, end
total%average = total%average + t(i)%average !日均温累加=积温
End Do
Write (8, *) total%average ! 结果写入另一个文件
Deallocate (t)
Close (7)
End Do
Close (8)
End Program main
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 = 'CN174654 (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
QQ截图20170203153352.png (6.26 KB, 下载次数: 284)
QQ截图20170203153343.png (1.82 KB, 下载次数: 300)
fcode 发表于 2017-2-3 12:33
假设从 2000 年循环到 2030 年
year = 2000
do k = 1 , 30
欢迎光临 Fortran Coder (http://bbs.fcode.cn/) | Powered by Discuz! X3.2 |