Fortran Samples

prog1.f90

program prog1 
  print *, 'Hello, World (1)' 
  print '(A)', 'Hello, World (2)' 
  print '(A8)', 'Hello, World (3)' 
  print '(A)', 'Hello, World' 
  print '(A$)', 'Hello, ' 
  print '(A)', 'World' 
  stop 
end

ポイント

  • プログラムは
    program プログラムの名前
      プログラムの本体
      stop 
    end

    という形式になる。

  • 何でもいいから適当に出力したいときは
      print *, 出力したいものをコンマで区切って並べる
  • 文字列はシングルクォート(一重引用符)でくくる。文字列を出力する正式な方法は
      print '(A)', '出力したい文字列'

    となる。

prog2.f90

program prog2 
  implicit none 
  real(8):: xvalue 
  print '(A$)', 'Input X: ' 
  read (*, *) xvalue 
  print '("X = ", E15.7)', xvalue 
  stop 
end

prog3.f90

program prog3 
  implicit none 
  integer:: counter 
  real(8):: xvalue 
  do counter = 1, 5 
    xvalue = 1./dble(counter) 
    print *, counter, xvalue 
  end do 
  stop 
end

prog4.f90

program prog4 
  implicit none 
  integer, parameter:: IMAX = 10 
  integer:: i, isum 
  real(8):: dsum 
  isum = 0 
  dsum = 0.d0 
  do i = 1, IMAX 
    isum = isum + i 
    dsum = dsum + dble(i) 
    print *, i, isum, dsum 
  end do 
  print *, 'ISUM = ', isum 
  print *, 'DSUM = ', dsum 
  print *, '1/ISUM = ', 1/isum 
  print *, '1.D0/DSUM = ', 1.d0/dsum 
  stop 
end

prog5.f90

program prog5 
  implicit none 
  integer, parameter:: IMAX = 10 
  integer:: i 
  real(8):: r 
  do i = 1, IMAX 
    call random_number(r) 
    print *, i, r 
  end do 
  stop 
end

prog6.f90

program prog6 
  implicit none 
  integer:: N, M       !!!  M <- N - 1 
  integer:: i          !!!  Counter 
  real(8), dimension(:), allocatable:: r 
  print '(A$)', 'N = ' 
  read (*, *) N 
  print *, 'N = ', N 
  M = N - 1 
  allocate (r(0:M)) 
  call random_seed() 
  call random_number(r) 
  do i = 0, M 
    print *, i, r(i) 
  end do 
  deallocate (r) 
  stop 
end

prog7.f90

program prog7 
  implicit none 
  integer:: N, M 
  integer:: i, idummy 
  real(8):: dmini, trial 
  real(8), dimension(:), allocatable:: r 
  read (*, *) N 
  write (*, *) 'Number of Data = ', N 
  M = N - 1 
  allocate (r(0:M)) 
  do i = 0, M 
    read (*, *) idummy, r(i) 
  end do 
! 
  dmini = r(0) 
  do i = 1, M 
    trial = r(i) 
    if (trial <= dmini) then 
      dmini = trial 
      print '(A10$)', 'CHANGED: ' 
      print '(I8, 2E15.7)', i, trial, dmini 
    else 
      print '(A10, I8, 2E15.7)', '', i, trial, dmini 
    end if 
  end do 
  deallocate (r) 
  print *, 'Minimum Value = ', dmini 
  stop 
end

prog8.f90

program prog8 
  implicit none 
  integer, parameter:: N = 100, M = N - 1 
  real(8):: a, b, h 
  real(8), dimension(0:M):: x, y, z 
  real(8):: getvalue 
  integer:: i 
  a = 0.d0 
  b = 1.d0 
  h = (b - a)/dble(N) 
  do i = 0, M 
    x(i) = a + h*dble(i) 
    call setvalue(x(i), y(i)) 
    z(i) = getvalue(x(i)) 
  end do 
  do i = 0, M 
    print '(3E15.7)', x(i), y(i), z(i) 
  end do 
  stop 
end
 
subroutine setvalue(xval, yval) 
  implicit none 
  real(8):: xval, yval 
  yval = sin(xval) 
  return 
end
 
function getvalue(xval) result(yval) 
  implicit none 
  real(8):: xval, yval 
  yval = exp(-xval) 
  return 
end
fortransamples.txt · 最終更新: 2014/05/09 10:16 by kimi
www.chimeric.de Creative Commons License Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0