gpt4 book ai didi

string - 如何在 Forth 中比较两个字符串?

转载 作者:行者123 更新时间:2023-12-02 06:43:59 25 4
gpt4 key购买 nike

我可以在 if 语句中执行此操作还是应该创建一个辅助 bool 变量?这是我到目前为止的代码。顺便说一句,IOX@是从用户那里获取输入。

: var
compile: VARIABLE
complile: ;

: lock
compile: var realPass$
compile: realPass$ "password" !
compile: ." Enter Password: "
compile: var pass$
compile: IOX@ pass$ !
compile: var match
compile: realPass$=pass$ match ! Unknown token: realPass$=pass$

最佳答案

用于比较字符串的 ANS FORTH 单词是 COMPARE (c-addr_1 u_1 c-addr_2 u_2 -- n) 。在 FORTH 中,字符串是使用 s" 单词创建的,字符串由内存地址和长度组成。这意味着当您比较字符串时,您将提供给 COMPARE 两个字符串的内存地址和长度。

COMPARE 的结果如下:0 表示两个字符串相等,-1 表示小于, 1 表示大于。这与提供比较器或比较运算符的其他语言相同。 注意,在 FORTH 中,true-1,因此检查 compare 的二进制返回没有任何值(value)。

一般来说,Forth 会避开显式变量,而倾向于使用堆栈,因此直接将其与 IF 一起使用而无需额外变量即可。

: PWCHECK
COMPARE
IF ." Entry denied."
ELSE ." Welcome friend." THEN
CR
;

S" Hello" ok
S" Goodbye" ok
PWCHECK You are not wanted here. ok
S" Howdy" ok
S" Howdy" ok
PWCHECK Welcome friend. ok

关于string - 如何在 Forth 中比较两个字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17381241/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com