PP#9~Fortran Tips#4
前回宿題としていたサブルーチンstrtoimdを作成しましょう。コアのサブルーチンなので二回に分けてお届けします。
初回は読み込んだ文字列の文字数をカウントするコードを考えます。
下記コードでは一度に読み込む文字列が一個以上の場合を想定しています。
subroutine strtoimd(Line, Nx, Words, NumValue, IntValue, DblValue)
!********************************************************************************
!* *
!* Convert the String to Integer and Double *
!* *
!********************************************************************************
implicit none
integer,intent(in):: Nx
integer,intent(out):: NumValue, IntValue(Nx)
real*8,intent(out):: DblValue(Nx)
character,intent(inout)::Line*(*)
integer:: NumWord,NumChar(Nx),length, cbegin, cend, count, star, &
endPtr, i, j, k
character:: Words(Nx)*(*)
logical:: digit
!================================================================================
! Function : Convert string to float including the expression like
! : "15*100.0", to read double (100.0) by int times (15)
! Line : Each line in the input data
! NumWord : Number of words in the Line
! NumChar : Number of characters in each word
! Words : Array of the words comprised of the line
! NumValue : Number of values extracted from the Line
! IntValue : Integer value extracted from the Line
! DblValue : Double value extracted from the Line
! endPtr : Pointer of the remaining word after converting the word
! length : Length of Line
! cbegin : Location of the first character of each word
! cend : Location of the last character of each word
! count : Number of count
!================================================================================
!Extract words from Line given
!================================================================================
length = len_trim(Line) !Calculate length of the string without space
!Global loop to find the "void" character
cbegin = 1
cend = 1
count = 0
do while (cbegin <=length)
if ( Line(cbegin:cbegin) == '0' .or. Line(cbegin:cbegin) == '1' .or. &
Line(cbegin:cbegin) == '2' .or. Line(cbegin:cbegin) == '3' .or. &
Line(cbegin:cbegin) == '4' .or. Line(cbegin:cbegin) == '5' .or. &
Line(cbegin:cbegin) == '6' .or. Line(cbegin:cbegin) == '7' .or. &
Line(cbegin:cbegin) == '8' .or. Line(cbegin:cbegin) == '9' .or. &
Line(cbegin:cbegin) == '-' .or. Line(cbegin:cbegin) == '.' ) then
count = count + 1
do j=cbegin+1,length+1 !Local loop to find the next "void" character
if (Line(j:j) == ' ' .or. Line(j:j) == '\0' .or. Line(j:j) == '\n') then
cend = j - 1
exit
endif
cend = j
enddo
NumChar(count) = cend - cbegin + 1
do k=1,NumChar(count)
Words(count)(k:k) = Line(cbegin+k-1:cbegin+k-1)
enddo
cbegin = cend
endif
cbegin = cbegin + 1
enddo
NumWord = count
!print *, "Numword= ",NumWord, ", Word= ",Words(NumWord)
コードを追っていけばロジックは自明だと思いますが、如何でしょうか?
つづく