需要有关二分搜索算法错误的帮助

发布于 2024-11-02 06:15:09 字数 973 浏览 1 评论 0原文

使用 g95 编译器时,出现错误:

Error: Operands of comparison operator '.EQ.' at (1) are LOGICAL(4)/LOGICAL(4)

我不知道这意味着什么。我包括子程序。您有任何想法如何解决这个问题吗?

非常感谢您抽出时间。

  SUBROUTINE search(iarray, ItemSought, Found, Location)
CHARACTER(20), DIMENSION(50),INTENT(IN)::itemarray
CHARACTER(20)::ItemSought
LOGICAL, INTENT(OUT)::Found
INTEGER, INTENT(OUT)::Location
INTEGER:: First, Last, Middle

WRITE(*,'(1x,A)',ADVANCE="NO"),"What are you searching for? "
READ*, ItemSought

First=1
Last=SIZE(Iarray)
FOUND = .FALSE.

DO
    IF ((First > Last) .OR. Found) RETURN
        Middle = (First+Last)/2
    IF (ItemSought < Iarray(Middle)) THEN
        Last=Middle-1
    ELSE IF (ItemSought > Iarray(Middle)) THEN
        First=Middle+1
    ELSE
        Found = .TRUE.
        Location = Middle
    END IF
END DO


IF (Found == .TRUE.) THEN
PRINT*, Itemsought

END SUBROUTINE

Using the g95 compiler, I have an error:

Error: Operands of comparison operator '.EQ.' at (1) are LOGICAL(4)/LOGICAL(4)

I have no idea what this means. I'm including the subroutine. Do you have any ideas how to fix this?

Thanks so much for your time.

  SUBROUTINE search(iarray, ItemSought, Found, Location)
CHARACTER(20), DIMENSION(50),INTENT(IN)::itemarray
CHARACTER(20)::ItemSought
LOGICAL, INTENT(OUT)::Found
INTEGER, INTENT(OUT)::Location
INTEGER:: First, Last, Middle

WRITE(*,'(1x,A)',ADVANCE="NO"),"What are you searching for? "
READ*, ItemSought

First=1
Last=SIZE(Iarray)
FOUND = .FALSE.

DO
    IF ((First > Last) .OR. Found) RETURN
        Middle = (First+Last)/2
    IF (ItemSought < Iarray(Middle)) THEN
        Last=Middle-1
    ELSE IF (ItemSought > Iarray(Middle)) THEN
        First=Middle+1
    ELSE
        Found = .TRUE.
        Location = Middle
    END IF
END DO


IF (Found == .TRUE.) THEN
PRINT*, Itemsought

END SUBROUTINE

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱情眠于流年 2024-11-09 06:15:09

我不会承认我上次使用 FORTRAN 是什么时候,但它看起来确实与我记忆中的有很大不同。所以这只是一个猜测。

根据错误消息,我想说它在这一行(你没有说哪一行):

IF (Found == .TRUE.) THEN

再次只是猜测,你通常不会通过比较 true/false 来测试逻辑值,而是直接使用它:

IF (Found) THEN

I'm not going to admit the last time I used FORTRAN, but it sure looks a lot different than I remember. So this is just a guess.

Based on the error message I'd say it's on this line (you didn't say which):

IF (Found == .TRUE.) THEN

Again just guessing, you usually don't test a logical value by comparing to true/false, you use it directly:

IF (Found) THEN
抽个烟儿 2024-11-09 06:15:09

.EQ.(或==)关系运算符,就像.NE. (/=), .LT. (<) 等,仅用于比较数字,要比较逻辑值,您应该使用 .EQV. 和 <代码>.NEQV.

The .EQ. (or ==)relational operator, just like .NE. (/=), .LT. (<) and so on, is for comparing numbers only, for comparing logical values you should use .EQV. and .NEQV.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文