2013-06-29 86 views
1

如何比较Forth中的两个字符串?如何将字符串与Forth中的字符串进行比较?

而且我可以在if语句中执行它,还是应该创建一个辅助布尔变量?这是我迄今为止的代码。顺便说一下,[email protected]是从用户那里获得输入。

: var 
compile: VARIABLE 
complile: ; 

: lock 
compile: var realPass$ 
compile: realPass$ "password" ! 
compile: ." Enter Password: " 
compile: var pass$ 
compile: [email protected] pass$ ! 
compile: var match 
compile: realPass$=pass$ match ! Unknown token: realPass$=pass$ 
+1

user2302197:你用的是什么Forth的味道? –

回答

3

要比较字符串的ANSI字是COMPARE (c-addr_1 u_1 c-addr_2 u_2 -- n)。如果比较的两个字符串相等,n等于0。一般来说,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 
相关问题