max533 发表于 2014-12-7 16:10:33

關於算式不成立程序無法運行

本帖最后由 max533 于 2014-12-7 21:48 编辑

小弟百思不得其解。
為什麼在讀取完第一個檔案後,跑到第39行時,j會等於0呢???
然後因為這個算式不成立整個程序無法運行下去Q~Q

他出現的錯誤訊息是floating point divide by zero
floating point co-processor fault at adress 004015c8 in file pwv_mon_avg.f95 at line39

想請問各位高手有沒有遇過下面這種狀況,該如何改寫呢?!

程序如下:
program pwv_mon_avg
implicit none

integer :: stat,i,j,stat2,year,mon,mon_def(12)
character :: filename*12,filename_new*16
real*8 :: pwv_mm,sum,pwv_mon_mean

open (11,file='list.txt')
close (11,status='delete')
call system ('for %f in (*_mon.pwv) do echo %f >> list.txt')
open (11,file='list.txt')

mon_def=(/ 01,02,03,04,05,06,07,08,09,10,11,12 /)

stat=0
do while (stat==0)
read (11,*,iostat=stat) filename
if (stat/=0) exit
open (22,file=filename)
filename_new=filename(1:4)//'_mon_avg.pwv'
open (33,file=filename_new)
do i=1,12,1
    j=0
    stat2=0
    do while (stat2==0)
      read (22,*,iostat=stat2) year,mon,pwv_mm
      if (stat2/=0) exit
      if (mon==mon_def(i))then
      j=j+1
      if (j==1) then
          sum=pwv_mm
      else if (j>1) then
          sum=sum+pwv_mm
      end if
      else
      cycle
      end if
    end do
    pwv_mon_mean= sum / (j*1D0)
    write (33,'(i2.2,3x,f12.7)') mon,pwv_mon_mean
end do
end do
end program以下為測試用檔案:

fcode 发表于 2014-12-7 18:33:06

数据文件是空白的

max533 发表于 2014-12-7 21:49:46

fcode 发表于 2014-12-7 18:33
数据文件是空白的

不好意思,傳到錯誤的測試檔案,已經更新數據,感謝fcode提醒。

li913 发表于 2014-12-9 18:55:23

program pwv_mon_avg
implicit none
integer :: stat, stat2, i, year, mon, mon_num(12)
character :: filename*12, filename_new*16
real(8) :: pwv_mm, pwv_mon_mean(12)

open (11,file='list.txt')
close (11,status='delete')
call system ('for %f in (*_mon.pwv) do echo %f >> list.txt')
open (11,file='list.txt')
do
    read (11,*,iostat=stat) filename
    if (stat/=0) exit
    filename_new=filename(1:4)//'_mon_avg.pwv'
    open (33,file=filename_new)
    open (22,file=filename)
    mon_num(:) = 0
    pwv_mon_mean(:) = 0.0d0
    do
      read (22,*,iostat=stat2) year, mon, pwv_mm
      if (stat2/=0) exit
      pwv_mon_mean(mon) = pwv_mon_mean(mon) + pwv_mm
      mon_num(mon) = mon_num(mon) + 1
    end do
    do i = 1, 12
      if( mon_num(i)/=0 ) pwv_mon_mean(i) = pwv_mon_mean(i) / mon_num(i)
      write (33,'(i2.2,3x,f12.7)') i, pwv_mon_mean(i)
    end do
    close(22)
    close(33)
end do
close (11,status='delete')
write(*,*) "completed..."
read(*,*)
end program
页: [1]
查看完整版本: 關於算式不成立程序無法運行