如何在Forth中比较两个字符串? [英] How do I compare two strings in Forth?

查看:100
本文介绍了如何在Forth中比较两个字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在if语句中执行此操作吗?还是应该将辅助布尔值设置为变量?这是我到目前为止的代码.顺便说一句,IOX@是要从用户那里获取输入.

And can I do it in an if statement or should I make a helper boolean variable? Here's the code I have so far. By the way, IOX@ is to get input from the user.

: 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提供两个字符串的内存地址和长度.

The ANS FORTH word to compare strings is COMPARE (c-addr_1 u_1 c-addr_2 u_2 -- n). In FORTH a string is created with the s" word, and a string consists of a memory address and a length. This means that when you're comparing strings, you're providing to COMPARE the memory address and length of both strings.

COMPARE的结果如下:0如果两个字符串相等,则-1表示小于,1表示大于.这与提供比较器或比较运算符的其他语言相同. 注意,因为FORTH true-1,所以检查compare的二进制返回值没有价值.

The result of COMPARE is as follows: 0 if the two strings are equal, -1 for less-than, 1 for greater than. This is same as other languages that provide a comparator, or comparison operator. Note, in FORTH true is -1, so there is no value to checking the binary return of compare.

通常,Forth避免使用显式变量,而倾向于使用堆栈,因此直接与IF一起使用而无需额外的变量就可以了.

Generally Forth eschews explicit variables in favor of using the stack so using it with IF directly without an extra variable is fine.

: 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

这篇关于如何在Forth中比较两个字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆