从一开始讨厌写程序(虽然作为一个气象专业的孩子),到现在基本上自己编程遇到的问题都能处理,要么自己写,要么求助网站或亲朋好友。在这一个痛苦的转变中感谢有家园的许多同仁的经验无私分享和热心相助。今天也发上来一个自己写的ForTran小程序,分享给家园的们,希望可以帮到初入家园的亲,还希望各位大大们 大神们给予指导。下面上程序,一个关于给定日期计算星期几的子程序:
!!!! 给定日期确定周几
subroutine day_in_week(year,month,day,week_out)
implicit none
integer,parameter::num=7
integer::days_of_year,day_in_year !!!子程序声明
integer::year,month,day,ind,ispace
integer::Std_year_ref=2016 !!! month_ref=1,day_ref=1
integer::year_diff,day_diff,i=1
character(len=6)::week_out
character(len=6)::week_all(num),Std_week_ref="Fri."
data week_all/"Mon.","Tues.","Wed.","Thur.","Fri.","Sat.","Sun."/
year_diff = year - Std_year_ref
day_diff=0
if( year_diff == 0 ) then
day_diff = day_in_year(year,month,day) - 1
else if( year_diff > 0 ) then
do while( i <= year_diff )
day_diff = day_diff + days_of_year(Std_year_ref+i)
i = i + 1
end do
day_diff = day_diff - day_in_year(year,month,day) + 1
else
do while( i <= abs(year_diff) )
day_diff = day_diff + days_of_year(Std_year_ref-i)
i = i + 1
end do
day_diff = day_in_year(year,month,day) - day_diff - 1
end if
!!print*,day_diff
ispace = mod(day_diff,num)
select case(ispace) !!! based on Variable: Std_week_ref
case(-6,1)
ind = mod(5,num) + 1
case(-5,2)
ind = mod(5+1,num) + 1
case(-4,3)
ind = mod(5+2,num) + 1
case(-3,4)
ind = mod(5+3,num) + 1
case(-2,5)
ind = mod(5+4,num) + 1
case(-1,6)
ind = mod(5+5,num) + 1
case default
ind = 5
end select
week_out = week_all(ind)
write(*,*)"day_in_week : ",week_out
end subroutine day_in_week
!!!! 子函数days_of_year 判断给定的年份有多少天(365 or 366 )
function days_of_year(year)
integer::year,days_of_year
integer::leap !!! 子程序声明
if(leap(year)==1)then
days_of_year=366
else
days_of_year=365
end if
end function days_of_year
!!! 子函数day_in_year 对某一确定日期(year,month,day)计算它在当年中是具体多少天(1-366)
function day_in_year(year,month,day)
integer::days_of_month !!! 子程序声明
integer::year,month,day
integer::day_in_year
integer::imo
imo=1
day_in_year = day
do while(imo < month)
day_in_year = day_in_year + days_of_month(year,imo)
imo = imo + 1
end do
end function day_in_year
!!! 子函数days_of_month,根据给定的年、月计算当月共有多有天(1-31)
function days_of_month(year,month)
integer::year,month,days_of_month
integer::leap !!! 子程序声明
select case(month)
case(1,3,5,7,8,10,12)
days_of_month=31
case(4,6,9,11)
days_of_month=30
case default
if(leap(year)==1) then !!! leap year
days_of_month=29
else !!! not leap year
days_of_month=28
end if
end select
end function days_of_month
!!! 子函数leap,判断平(闰)年
function leap(year)
integer::leap,year
if((mod(year,4)==0 .and. mod(year,100)/=0)) then
leap=1
else if(mod(year,400)==0) then
leap=1
else
leap=0
end if
end function leap