如何在字符数组中搜索字符变量?

时间:2012-10-18 19:04:45

标签: arrays variables search fortran character

好吧,在我的程序中,我打开一个包含三列的.txt文件。第1列有整数,第2列有名称(字符串),第3列有实数。我已经想出如何搜索用户输入的项目的整数和实数(将变量与存储列的数组进行比较)...但是当我尝试搜索用户输入的名称时,在字符数组中,它永远不会奏效。这是我的所有代码......我在CASE(2)

寻找帮助

~n保存文件中的行数

PROGRAM database
IMPLICIT NONE

CHARACTER(30)::file, search
INTEGER::err, n, m, i, k, searchtype, recordnum
LOGICAL:: alive
REAL:: grade
REAL, ALLOCATABLE:: arrayr(:)
INTEGER, ALLOCATABLE:: arrayi(:)
CHARACTER(15), ALLOCATABLE:: arrayc(:)
CHARACTER(15)::name

101 FORMAT(A)

DO
    WRITE(*,101) "What is the filename?"
    READ(*,*) file

    INQUIRE(FILE=file, EXIST=alive)
    IF (alive.EQV..TRUE.) THEN
        WRITE(*,101) "The file exists"
        OPEN (UNIT=11, FILE=file, STATUS="OLD", ACTION="READ", IOSTAT=err)
        IF (err .NE. 0) THEN    
            WRITE(*,'(2A)')"There was an error opening ", file
            STOP
        END IF
        EXIT
    ELSE IF (alive.EQV..FALSE.) THEN        
        WRITE(*,'(2A)') "There is no file by the name of: ", file
        CYCLE
    END IF
END DO


n= 0
DO
    READ(11,*,IOSTAT=k)
    IF(k.EQ.-1) EXIT
    n=n+1
END DO
REWIND(11)
ALLOCATE(arrayi(n),arrayc(n),arrayr(n))


DO i=1,n,1
    READ(11,'(I4,A,F12.2)') arrayi(i), arrayc(i), arrayr(i)
END DO

outer: DO
    DO
        WRITE(*,101)"How would you like to search the file?"
        WRITE(*,101)"1) By record number"
        WRITE(*,101)"2) By name"
        WRITE(*,101)"3) By rating"
        READ(*,*) searchtype
        SELECT CASE (searchtype)
            CASE(1)
                WRITE(*,101) "Please enter the record number:"
                READ(*,*) recordnum
                m=0
                DO i=1,n,1
                    IF (arrayi(i).EQ.recordnum) THEN
                        WRITE(*,'(I4,A,F12.2)')arrayi(i),arrayc(i), arrayr(i)
                    ELSE IF (i==n) THEN
                        write(*,*)"Sorry dude. The name you entered     was not found. Search failed"
                    END IF
                END DO
                EXIT
            CASE(2)
                WRITE(*,101) "Please enter the name:"
                READ(*,*) name
                m=0
                DO i=1,n
                    IF (name.EQ.arrayc(i)) THEN
                        WRITE(*,'(I4,A,F12.2)')arrayi(i), arrayc(i), arrayr(i)
                    ELSE IF (i==n) THEN
                        write(*,*)"Sorry dude. The name you entered was not found. Search failed"
                    END IF
                END DO
                EXIT
            CASE(3)
                WRITE(*,101) "This will return all graders greater than your search term"
                WRITE(*,101) "Please enter the minimum grade:"
                READ(*,*) grade
                m=0
                DO i=1,n,1
                    IF (arrayr(i).GT.grade) THEN
                        WRITE(*,'(I4,A,F12.2)')arrayi(i), arrayc(i), arrayr(i)
                    ELSE IF (i==n) THEN
                        write(*,*)"Sorry dude. The name you entered was not found. Search failed"
                    END IF
                END DO
                EXIT
            CASE DEFAULT
                WRITE(*,101) "Invalid entry. Please enter 1, 2, or 3."
                CYCLE
        END SELECT
    END DO


    inner: DO
        WRITE(*,101)"Would you like to search again? (Y/N)"
        READ(*,*)search
            SELECT CASE (search)
                CASE("Y","y")
                    CYCLE outer
                CASE("N","n")
                    EXIT outer
                CASE DEFAULT
                    WRITE(*,101)"Invalid entry. please enter Y or N."
                    CYCLE inner
                END SELECT
    END DO inner
END DO outer


END PROGRAM database

每当我在程序中选择case(2)然后在字符arrayc中输入一个名称I KNOW EXISTS时,它会跳过if子句(name.EQ.arrayc)并返回找不到名称。请帮忙!我对其他方式持开放态度。我不明白为什么会这样。

此链接有截图: http://tinypic.com/r/33tmmuc/6

1 个答案:

答案 0 :(得分:1)

----编辑 - 解决方案----

你用一种奇怪的格式读取字符串,它以空格开头。使用它进行比较:

if (name==adjustl(arrayc(i))) then

检查手册中的内容。

考虑摆脱大写字母和古老的.EQ。运算符。

使用更好的格式,不要使用它们的标签,而是使用字符串变量或常量。

的东西
'(i4,2x,a15,F12.0)'

可能没问题,请检查数字的正确值。请注意,F12.2100读为1.00

你也错误地实现了循环。找到答案后,exit应该就在这个地方。

----编辑 - 解决方案----

适合我:

CHARACTER(15), ALLOCATABLE:: arrayc(:)
CHARACTER(30)::name
INTEGER:: n

    allocate(arrayc(1))
    arrayc = (/"ok1","ok2"/)
    n=2
    write(*,*) arrayc(1)

    WRITE(*,*) "Please enter the name:"
    READ(*,*) name
    m=0
    DO i=1,n,1
        IF(name.EQ.arrayc(i)) THEN
            WRITE(*,'(A)') arrayc(i)
        ELSE IF (name.NE.arrayc(i)) THEN
            m=m+1
        END IF
    END DO
    IF (m.EQ.n) THEN
        WRITE(*,*)"Sorry dude. The name you entered was not found. Search failed"
    END IF
end

运行:

> ./a.out 
 ok1            
 Please enter the name:
ok1
ok1

请务必正确n。还要尝试打印字符数组。注意不要比较name太长(你的变量是数组中字符串的两倍)。你在循环之后比较循环索引值的做法是有问题的,它的值是未定义的。

我会完全重写循环结构。有点像:

   ...
    WRITE(*,*) "Please enter the name:"
    READ(*,*) name

    do i=1,n
        if(name.EQ.arrayc(i)) then
            write(*,'(A)') arrayc(i)
            exit
        else if (i==n) then
            write(*,*)"Sorry dude. The name you entered was not found. Search failed"
        end if

    end do

end