-2

我正在尝试生成“整数”的真值表。首先,我需要有 2 个整数列表,就像这里的这张表一样:

1 1   
1 2
2 1
2 2

然后我需要使用布尔运算符来生成这样的表:

1 1 2 1 2 2
1 2 1 2 2 1
2 1 1 2 1 2
2 2 2 1 1 1

我检查了一些相关的问题,例如: Generating truth tables in Java or boolean operations with integers ,但我仍然不知道如何在 VB.net 中编写它。所以我感谢你的帮助和时间。:) 谢谢!

4

1 回答 1

1

我假设您想要Xor布尔值而不是整数本身的二进制表示。所以我的回答是基于这个假设。

如果您使用 1 forTrue和 2 for False,那么我建议您编写一些转换函数。

Private Function IntegerToBoolean(number As Integer) As Boolean
    Return If(number = 1, True, False)
End Function

Private Function BooleanToInteger(bool As Boolean) As Integer
    Return If(bool, 1, 2)
End Function

然后,使用这些,编写其他函数相当简单:

Private Function IntegerXor(int1 As Integer, int2 As Integer) As Integer
    Dim bool1 As Boolean = IntegerToBoolean(int1)
    Dim bool2 As Boolean = IntegerToBoolean(int2)
    Dim boolResult as Boolean = (bool1 Xor bool2)
    Return BooleanToInteger(boolResult)
End Function

等等

显然,您将为表格中的每个数字执行此操作,并且您将为 和 创建附加And函数Or

于 2014-03-04T14:03:24.243 回答