chap06/kadai4.f90

サンプルコードのダウンロード

 1program answer
 2  implicit none
 3
 4  integer, parameter :: n = 32
 5  integer :: i, j, ios
 6  real(8) :: xyz(n, 3)
 7  character(20) :: files(3)
 8
 9  open(unit=10, iostat=ios, file='helix1.dat', action='read', &
10       & form='formatted', status='old')
11
12  if(ios /= 0) then
13    write(*, *) 'Failed to open a file'
14    stop
15  endif
16
17  do i = 1, n
18    read(10, *) xyz(i, 1), xyz(i, 2), xyz(i, 3)
19  enddo
20
21  close(10)
22
23  ! 別ファイルに出力
24  files(1) = 'x.dat'
25  files(2) = 'y.dat'
26  files(3) = 'z.dat'
27
28  do j = 1, 3
29    open(unit=20, iostat=ios, file=files(j), action='write', &
30         & form='formatted', status='replace')
31
32    if(ios /= 0) then
33      write(*, *) 'Failed to open a file'
34      cycle
35    endif
36
37    do i = 1, n
38      write(20, '(f5.2)') xyz(i, j)
39    enddo
40
41    close(20)
42  enddo
43
44  stop
45endprogram answer