QBasic/QB64:如何清理 IF THEN IF “阶梯”?
Posted
技术标签:
【中文标题】QBasic/QB64:如何清理 IF THEN IF “阶梯”?【英文标题】:QBasic/QB64: How to clean up an IF THEN IF "ladder"? 【发布时间】:2018-03-29 06:09:42 【问题描述】:不确定我是否在这里使用了正确的术语,但无论出于何种原因,QBasic 不理解“x = y = z”的内容。仅限两个。
为了解决这个问题,我这样做了:
IF sum(1) = sum(2) THEN
IF sum(2) = sum(3) THEN
IF sum(3) = sum2(1) THEN
IF sum2(1) = sum2(2) THEN
IF sum2(2) = sum2(3) THEN
IF sum2(3) = sum3 THEN
IF sum3 = sum4 THEN
PRINT "This is a Lo Shu Square, with all sums equaling"; sum(1)
ELSE
PRINT "This is not a Lo Shu Square."
END IF
END IF
END IF
END IF
END IF
END IF
END IF
END
确实有效,但有件事告诉我有一种更简单的方法可以检查所有和是否相等。有什么建议吗?
【问题讨论】:
QBasic 知道AND
。如果 all 值相同,则将所有值与一个值进行比较就足够了。例如:IF sum(1) = sum(2) AND sum(1) = sum(3) AND ...
因为 A=B && A=C => B=C
【参考方案1】:
如果将所有比较放在用 AND 分隔的一行上,这样就可以了:
REM code to shrink IFTHEN ladder:
IF sum(1) = sum(2) AND sum(2) = sum(3) AND sum(3) = sum2(1) AND sum2(1) = sum2(2) AND sum2(2) = sum2(3) AND sum2(3) = sum3 AND sum3 = sum4 THEN
PRINT "This is a Lo Shu Square, with all sums equaling"; sum(1)
ELSE
PRINT "This is not a Lo Shu Square."
END IF
END
【讨论】:
【参考方案2】:您也可以将逻辑编码成一个循环:
DIM testvals(8)
testvals(0) = sum(1)
testvals(1) = sum(2)
testvals(2) = sum(3)
testvals(3) = sum2(1)
testvals(4) = sum2(2)
testvals(5) = sum2(3)
testvals(6) = sum3
testvals(7) = sum4
DO
FOR i = 1+LBOUND(testvals) TO UBOUND(testvals)
IF testvals(i-1) <> testvals(i) THEN
PRINT "This is not a Lo Shu square."
EXIT DO
END IF
NEXT
PRINT "This is a Lo Shu square, with all sums equaling"; sum(1)
LOOP WHILE 1 = 0
这有几个好处:
-
如果您更改代码,更容易发现拼写错误。
您始终可以在类似情况下添加和删除测试值。在这种情况下,不需要这样做,但在其他一些情况下,只需键入
testvals(8) = value
并将 DIM
行中的 8
更改为 9
可能会有所帮助。
它使比较短路,这意味着如果第一个条件为假,它会停止检查并说它不是罗舒广场,类似于IF-THEN-ELSE
语句的塔(其中每个ELSE
是PRINT "This is not a Lo Shu square."
) QB64 的AND
运算符计算两个操作数,即使第一个操作数是 0 或另一个“假”值。这可能会快得多,但在这种情况下您可能不会注意到差异。
另一方面,它确实有一些缺点:
-
在这种情况下不使用
AND
是QB64 中的一种不寻常的模式。事实上,这是AND
存在的一个很好的理由。
您可以轻松删除使用 AND
组合的测试值,而无需重新编号 testvals
数组中的项目或更改其维度。
即使您有很多测试值,最好还是自己编写一个小程序来生成IF a AND b AND c AND ... THEN ... END IF
块(或类似于IF-THEN
塔的东西以保留短路行为)并将输出粘贴到您的需要的程序代码。
【讨论】:
【参考方案3】:检查数组的更简单方法:
testvals(1) = sum(1)
testvals(2) = sum(2)
testvals(3) = sum(3)
testvals(4) = sum2(1)
testvals(5) = sum2(2)
testvals(6) = sum2(3)
testvals(7) = sum3
testvals(8) = sum4
FOR i = 1 TO 7
IF testvals(i) <> testvals(i + 1) THEN
f = -1
EXIT FOR
END IF
NEXT
IF f THEN
PRINT "This is not a Lo Shu square."
ELSE
PRINT "This is a Lo Shu square, with all sums equaling"; sum(1)
END IF
【讨论】:
【参考方案4】:检查循环的更简单方法:
testvals(1) = sum(1)
testvals(2) = sum(2)
testvals(3) = sum(3)
testvals(4) = sum2(1)
testvals(5) = sum2(2)
testvals(6) = sum2(3)
testvals(7) = sum3
testvals(8) = sum4
FOR i = 1 TO 7
IF testvals(i) <> testvals(i + 1) THEN
PRINT "This is not a Lo Shu square."
END
END IF
NEXT
PRINT "This is a Lo Shu square, with all sums equaling"; sum(1)
【讨论】:
【参考方案5】:将逻辑合并为一个函数:
FUNCTION isLoShuSquare (sums() AS DOUBLE)
isLoShuSquare = 1
DIM i AS INTEGER
FOR i = 0 TO UBOUND(sums) - 1
IF sums(i) <> sums(i + 1) THEN
isLoShuSquare = 0
EXIT FOR
END IF
NEXT i
END FUNCTION
然后加载数组并传递给函数:
DIM sums(7) AS DOUBLE
DIM i AS INTEGER
i = 0
sums(i) = sum(1): i = i + 1
sums(i) = sum(2): i = i + 1
sums(i) = sum(3): i = i + 1
sums(i) = sum2(1): i = i + 1
sums(i) = sum2(2): i = i + 1
sums(i) = sum2(3): i = i + 1
sums(i) = sum3: i = i + 1
sums(i) = sum4
PRINT isLoShuSquare(sums())
【讨论】:
【参考方案6】:在函数的循环中检查数组的另一种方法:
DIM sums(8) AS DOUBLE
sums(1) = sum(1)
sums(2) = sum(2)
sums(3) = sum(3)
sums(4) = sum2(1)
sums(5) = sum2(2)
sums(6) = sum2(3)
sums(7) = sum3
sums(8) = sum4
IF isLoShuSquare(sums()) = 0 THEN
PRINT "This is not a Lo Shu square."
ELSE
PRINT "This is a Lo Shu square, with all sums equaling"; sum(1)
END IF
END
FUNCTION isLoShuSquare (sums() AS DOUBLE)
isLoShuSquare = -1
FOR i = 1 TO UBOUND(sums) - 1
IF sums(i) <> sums(i + 1) THEN
isLoShuSquare = 0
EXIT FUNCTION
END IF
NEXT
END FUNCTION
【讨论】:
以上是关于QBasic/QB64:如何清理 IF THEN IF “阶梯”?的主要内容,如果未能解决你的问题,请参考以下文章
如何在 PL/SQL 块 (IF/THEN) 中执行“插入...选择...