Fortran 77 从输入中获取字符串直到 space 就像 java 中的 input.next()
fortran 77 take string from input until space like input.next() in java
我是 Fortran 77 的新手。我需要像 Java 中的 input.next() 这样的代码。我想给出一个像 "hi how are you today" 这样的输入并一个一个地检查每个单词,所以要做到这一点,我需要一个一个地接受单词。最简单的方法是什么?我可以检查每个字符并将字符放在字符变量 space 之前,但它看起来很难。
虽然这可能是重复的,但这是我手头的东西(表明没有直接的内置例程,虽然乍一看可能很棘手,但并不是 "hard"以某种方式写它......)我认为还有基于 index() 函数的更有效的方法。如果有必要,写一个类似 "input_next()" 的例程来获取下一个单词 one-by-one [*].
可能会有用
program main
implicit none
character(100) line, words( 100 )
integer n, i
line = "hi how are you today" ! input string
words = "" ! words to be obtained
call split_str( line, words, n )
do i = 1, n
print *, "i= ", i, "word= ", trim(words( i ))
enddo
end
subroutine split_str( line, words, n )
implicit none
character(*), intent(in) :: line
character(*), intent(out) :: words(*)
integer, intent(out) :: n
integer :: ios
character(100) :: buf( 100 ) ! large buffer
n = 0
do
n = n + 1
read( line, *, iostat=ios ) buf( 1 : n ) ! use list-directed input
if ( ios == 0 ) then
words( 1 : n ) = buf( 1 : n ) ! if success, copy to the original array
else
n = n - 1
exit ! if all the words are obtained, finish
endif
enddo
end
结果:
i= 1 word= hi
i= 2 word= how
i= 3 word= are
i= 4 word= you
i= 5 word= today
[*] 这是这种 getnextword()
的一种可能方法,它通过 list-directed 输入从输入字符串 (line
) 中获取一个词,然后从字符串中删除该词下次通话。如果在 line
中找不到更多的单词,则 found
变为假。 (请在网络或 SO 页面中搜索 "list-directed input" 以获取更多详细信息。)
program main
implicit none
character(100) line, word
logical found
line = "hi how are you today"
do
call getnextword( line, word, found )
if ( .not. found ) exit
print "(a,a7,2a)", "word= ", trim( word ), " : line= ", trim( line )
enddo
end program
subroutine getnextword( line, word, found )
implicit none
character(*), intent(inout) :: line
character(*), intent(out) :: word
logical, intent(out) :: found
integer :: ios
character(100) :: buf
read( line, *, iostat=ios ) buf ! try to read one word into a buffer via list-directed input
if ( ios == 0 ) then ! if success
found = .true.
word = trim( buf ) ! save the word
line = adjustL( line )
line = line( len_trim( word ) + 1 : ) ! and remove the word from the input line
else
found = .false.
word = ""
endif
end
结果:
word= hi : line= how are you today
word= how : line= are you today
word= are : line= you today
word= you : line= today
word= today : line=
我是 Fortran 77 的新手。我需要像 Java 中的 input.next() 这样的代码。我想给出一个像 "hi how are you today" 这样的输入并一个一个地检查每个单词,所以要做到这一点,我需要一个一个地接受单词。最简单的方法是什么?我可以检查每个字符并将字符放在字符变量 space 之前,但它看起来很难。
虽然这可能是重复的,但这是我手头的东西(表明没有直接的内置例程,虽然乍一看可能很棘手,但并不是 "hard"以某种方式写它......)我认为还有基于 index() 函数的更有效的方法。如果有必要,写一个类似 "input_next()" 的例程来获取下一个单词 one-by-one [*].
可能会有用program main
implicit none
character(100) line, words( 100 )
integer n, i
line = "hi how are you today" ! input string
words = "" ! words to be obtained
call split_str( line, words, n )
do i = 1, n
print *, "i= ", i, "word= ", trim(words( i ))
enddo
end
subroutine split_str( line, words, n )
implicit none
character(*), intent(in) :: line
character(*), intent(out) :: words(*)
integer, intent(out) :: n
integer :: ios
character(100) :: buf( 100 ) ! large buffer
n = 0
do
n = n + 1
read( line, *, iostat=ios ) buf( 1 : n ) ! use list-directed input
if ( ios == 0 ) then
words( 1 : n ) = buf( 1 : n ) ! if success, copy to the original array
else
n = n - 1
exit ! if all the words are obtained, finish
endif
enddo
end
结果:
i= 1 word= hi
i= 2 word= how
i= 3 word= are
i= 4 word= you
i= 5 word= today
[*] 这是这种 getnextword()
的一种可能方法,它通过 list-directed 输入从输入字符串 (line
) 中获取一个词,然后从字符串中删除该词下次通话。如果在 line
中找不到更多的单词,则 found
变为假。 (请在网络或 SO 页面中搜索 "list-directed input" 以获取更多详细信息。)
program main
implicit none
character(100) line, word
logical found
line = "hi how are you today"
do
call getnextword( line, word, found )
if ( .not. found ) exit
print "(a,a7,2a)", "word= ", trim( word ), " : line= ", trim( line )
enddo
end program
subroutine getnextword( line, word, found )
implicit none
character(*), intent(inout) :: line
character(*), intent(out) :: word
logical, intent(out) :: found
integer :: ios
character(100) :: buf
read( line, *, iostat=ios ) buf ! try to read one word into a buffer via list-directed input
if ( ios == 0 ) then ! if success
found = .true.
word = trim( buf ) ! save the word
line = adjustL( line )
line = line( len_trim( word ) + 1 : ) ! and remove the word from the input line
else
found = .false.
word = ""
endif
end
结果:
word= hi : line= how are you today
word= how : line= are you today
word= are : line= you today
word= you : line= today
word= today : line=