我正在尝试在Fortran中执行以下操作:
我的循环似乎无限期地运行,并且没有单词写入输出文档中
我的测试输入文档只有2行:
第一行正在正确读取和编码
PROGRAM zeichen
USE PufferMod
IMPLICIT NONE
CHARACTER(LEN=132) :: z
CHARACTER(maxlen):: x
INTEGER:: a,e,i,l
OPEN(UNIT=39,FILE="intext.txt",ACTION="READ", STATUS="OLD")
OPEN(UNIT=40, FILE="outext.txt", ACTION="WRITE")
DO
2 READ(39,"(A)", end=10) z
a=1;
DO WHILE(a<133)
WRITE(*,*) z
WRITE(*,*) a
CALL Suche_Wort(z,a,e)
l=e-a+1
WRITE(*,*) a,e,l
DO i=a,e
IF(z(i:i)/="") THEN
CALL Codiere(z(i:i),l)
END IF
END DO
a=e+1
END DO
WRITE(40,*) z
END DO
10 CLOSE(UNIT=39)
CLOSE(UNIT=40)
END PROGRAM zeichen
MODULE PufferMod
IMPLICIT NONE
PRIVATE
PUBLIC :: maxlen, Codiere, Suche_Wort
INTEGER, PARAMETER :: maxlen = 132
CONTAINS
FUNCTION Kleinbuchstabe(z)
CHARACTER(LEN=1) :: z
LOGICAL :: Kleinbuchstabe
Kleinbuchstabe= "a" <= z .AND. z <= "z"
END FUNCTION Kleinbuchstabe
FUNCTION Grossbuchstabe(z)
CHARACTER(LEN=1) :: z
LOGICAL :: Grossbuchstabe
Grossbuchstabe= "A" <= z .AND. z <= "Z"
END FUNCTION Grossbuchstabe
FUNCTION Buchstabe(z)
CHARACTER(LEN=1) :: z
LOGICAL :: Buchstabe
Buchstabe= (("a" <= z .AND. z <= "z") .OR. ("A" <= z .AND. z <= "Z"))
END FUNCTION Buchstabe
SUBROUTINE Codiere(z, Verschiebung)
CHARACTER(LEN=1) :: z
INTEGER, INTENT(IN) :: Verschiebung
INTEGER :: CodevonA
IF ( Kleinbuchstabe(z) ) THEN
CodevonA= ICHAR("a")
ELSE IF ( Grossbuchstabe(z) ) THEN
CodevonA= ICHAR("A")
END IF
z= CHAR( CodevonA + MOD( ICHAR(z) - CodevonA + Verschiebung, 26 ) )
END SUBROUTINE Codiere
SUBROUTINE Suche_Wort(z,a,e)
CHARACTER(LEN=maxlen), INTENT(in):: z
INTEGER, INTENT(inout):: a
INTEGER, INTENT(out):: e
CHARACTER:: x, temp*1
INTEGER:: i
i=1
temp=z(a:a)
!WRITE(*,*) "a before loop",a, "temp", temp
DO WHILE(temp == " ") !Find start of next word
temp=z(a+i:a+i)
i=i+1
a=a+1
END DO
!WRITE(*,*) "a after loop", a
e=a+1
i=1
temp=z(e:e)
DO WHILE(temp/= "") !find where the word ends by finding a space
temp=z(e+i:e+i)
i=i+1
e=e+1
IF(temp == " ") EXIT
END DO
END SUBROUTINE Suche_Wort
END MODULE PufferMod
答案 0 :(得分:2)
您的Suche_Wort
子例程至少有三个问题:
它从不检查z
变量的索引是否在范围内。
它假定结束索引严格大于起始索引,因此不允许使用1个字符的单词。
在每个循环中,它会同时i
和另一个整数变量(在第一个循环中为a
,在第二个循环中为e
)中递增一个在每次迭代中将z的索引增加2。
我建议对zeichen
程序进行两次更改,以通过使用固有函数trim
(删除尾随空格)和len_trim
(计算)来消除尾随空格。修剪变量的长度):
2 READ(39,"(A)", end=10) z
a=1;
DO WHILE(a<=len_trim(z))
WRITE(*,*) z
WRITE(*,*) a
CALL Suche_Wort(trim(z),a,e)
!...
然后,可以将检查和更正添加到Suche_Wort
子例程中(比我们不得不担心z(a:)
中剩余的所有字符都是空格的可能性要容易得多):
SUBROUTINE Suche_Wort(z,a,e)
CHARACTER(LEN=*), INTENT(in):: z ! Dummy argument has length of actual argument
INTEGER, INTENT(inout):: a
INTEGER, INTENT(out):: e
CHARACTER(len=1):: temp
temp=z(a:a)
DO WHILE((temp == " ").and.(a<len(z))) !Find start of next word
temp=z(a+1:a+1)
a=a+1
END DO
e=a ! Allows 1-character words
DO WHILE((temp/= " ").and.(e<len(z))) !find where the word ends by finding a space
temp=z(e+1:e+1)
e=e+1
END DO
END SUBROUTINE Suche_Wort
答案 1 :(得分:1)
为什么您需要完全考虑单词? 只需转换A:Z或a:z。
PROGRAM zeichen
CHARACTER(LEN=132) :: z
INTEGER :: iostat, Verschiebung = 1
OPEN (UNIT=39, FILE="ziechen.f90", ACTION="READ", STATUS="OLD")
OPEN (UNIT=40, FILE="outext.txt", STATUS="UNKNOWN")
DO
READ (39,fmt="(A)", iostat=iostat) z
if ( iostat /= 0 ) exit
call convert_line ( z, Verschiebung )
WRITE (40,fmt="(A)") trim (z)
WRITE ( *,fmt="(A)") trim (z)
END DO
END PROGRAM zeichen
Subroutine convert_line ( z, inc )
character*(*) z
integer inc, i
do i = 1, len_trim (z)
call convert_character ( z(i:i), inc )
end do
end Subroutine convert_line
Subroutine convert_character ( z, inc )
character z
integer inc, ia, ic
!
if ( z >= 'A' .and. z <= 'Z' ) then
ia = ichar ('A')
else if ( z >= 'a' .and. z <= 'z' ) then
ia = ichar ('a')
else
return
end if
ic = mod ( ichar (z) - ia + inc + 26, 26 )
z = char (ia + ic )
!
end Subroutine convert_character