我从键盘输入字符串并进行比较。但它永远不会返回 0,即使字符串相等。
CREATE word1 14 ALLOT
: .getword1 word1 14 BLANK word1 14 EXPECT ;
CREATE word2 14 ALLOT
: .getword2 word2 14 BLANK word2 14 EXPECT ;
: input_words
." " CR
." First word:" CR
.getword1
." " CR
." Second word:" CR
.getword2
word2
word1
word1 word2 compare
." " CR
.s
;
它永远不会为我的字符串返回 0:
First word:
e
Second word:
e
<1> 1
定义有问题吗?
compare
具有以下堆栈签名(cstr1 count1 cstr2 count2 -- n (。它需要字符串的地址及其计数(长度(
create word1 ," Test" Store counted string in word1
create word2 ," Test" Store counted string in word2
word1 count type Test ok
word2 count type Test ok
word1 . word2 . 4992752 4992848 ok addresses of word1 and word2
word1 count word2 count .s compare
DATA STACK
top
4 0000:0004 count2
4992849 004C:2F51 address word2 + 1
4 0000:0004 count1
4992753 004C:2EF1 address word1 + 1
ok-1
. 0 ok
进一步比较
char S word1 1+ c! ok Change the T in word1 to an S
word1 count type Sest ok
word1 count word2 count compare . -1 ok
char R word2 1+ c! ok Change the T in word1 to an R
word2 count type Rest ok
word1 count word2 count compare . 1 ok
我的系统上没有 EXPECT 词来测试您的代码,但我怀疑您的getwordx
词在堆栈上留下了各种项目,这意味着比较不会导致异常,但不会进行您期望的比较。
编辑:已实现EXPECT的答案。
Creating ANS forth EXPECT. I have ACCEPT not EXPECT in my Forth
CELL USER SPAN
: EXPECT addr ct --
ACCEPT SPAN ! ;
Answer to question
: getword addr ct --
2DUP BL FILL Fill addr for count with blanks
EXPECT ; Refill addr with characters typed at the prompt.
CREATE word1 14 ALLOT
CREATE word2 14 ALLOT
: wtype addr -- ;
14 -TRAILING TYPE ;
: input_words ;
CR ." First Word : " word1 14 getword
CR ." Second Word : " word2 14 getword
CR word1 wtype 5 SPACES word2 wtype
CR word1 14 word2 14 compare .
;
由于两个缓冲区都填充了任何未使用字符的空白,因此比较仍然有效。
input_words
First Word : Test
Second Word : Test
Test Test
0 ok
input_words
First Word : Rest
Second Word : Test
Rest Test
-1 ok