需要有关二分搜索算法错误的帮助
使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不会承认我上次使用 FORTRAN 是什么时候,但它看起来确实与我记忆中的有很大不同。所以这只是一个猜测。
根据错误消息,我想说它在这一行(你没有说哪一行):
再次只是猜测,你通常不会通过比较 true/false 来测试逻辑值,而是直接使用它:
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):
Again just guessing, you usually don't test a logical value by comparing to true/false, you use it directly:
.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.