真值表的最佳数据集,用于计算和减少 C 语言中的乘积总和#

Best data set for truth table to compute and reduce Sum of Products in C#

提问人:Ventus 提问时间:12/4/2019 更新时间:12/4/2019 访问量:347

问:

所以我试图用 C# 创建一个真值表,以便我可以在上面预演一些布尔代数。它应该是一个有 8 行的三变量真值表。到目前为止,我现在正在尝试使用数组的字符串数组来输入真值表。

string[][] truthTable = new string[8][];
            truthTable[0] = new string[2] { "1" , "000"};
            truthTable[1] = new string[2] { "0", "001" };
            truthTable[2] = new string[2] { "0", "010" };
            truthTable[3] = new string[2] { "0", "011" };
            truthTable[4] = new string[2] { "0", "100" };
            truthTable[5] = new string[2] { "1", "101" };
            truthTable[6] = new string[2] { "1", "110" };
            truthTable[7] = new string[2] { "1", "111" };





            for (int i = 0; i < truthTable.Length; i++)
            {
                // print out strings  that have "1 as first element"
                if (truthTable[i].GetValue(i) == "1" )

                {
                    Console.WriteLine(truthTable[i]);

                }

            }

我现在要做的是打印出第一个元素中带有“1”的数组。例如,对于第一个数组,控制台输出应该类似于“1”“000”,并且它应该只打印其他三个具有“1”的数组。但是现在它给了我一个越界错误,并且没有打印任何东西。

这是开始使用真值表来计算产品总和的好方法,还是有更好的方法可以在 C# 中实现它?

C# 布尔表达式 TruthTable 布尔代数

评论

0赞 12/4/2019
应该是 。当我> 0 时,你会出界if (truthTable[i].GetValue(0) == "1"iGetValue
0赞 Ventus 12/4/2019
它解决了越界问题,但它打印的只是 System.String[] 4 次,这比我以前的要好,但是我如何打印出“000”、“101”等?
0赞 12/4/2019
Console.WriteLine(truthTable[i][1]);打印数组 i 索引 1 处的项。for objects 的默认实现仅输出类型的名称,除非被覆盖。基元类型(如数组中的字符串)将其实际值输出为字符串。ToString()
0赞 Ventus 12/4/2019
非常感谢!这可行,但在您看来,您认为这是实现真值表的好方法吗?如果你不知道,那也没关系。
0赞 12/4/2019
这取决于您的用例,因为它们的范围可能非常复杂。正如另一张海报所提到的,Dictionary 或 HashSet 可能是更好的修复方法。如果你想跳到深处,看看这个 TruthTable 的实现

答:

1赞 Bondolin 12/4/2019 #1

一个简单的实现是改用 a 代替。键将保存三个变量值,第二个值将保存相应的真值:Dictionary<string, string>stringstring

var truthTable = new Dictionary<string, string>
{
    { "000", "1" },
    { "001", "0" },
    { "010", "0" },
    { "011", "0" },
    { "100", "0" },
    { "101", "1" },
    { "110", "1" },
    { "111", "1" },
};

foreach (var keyValue in truthTable)
{
    // print out strings that have value of "1"
    if (keyValue.Value == "1")
    {
        Console.WriteLine(keyValue.Key);
    }
}

虽然可能更符合感兴趣的领域,但您可以考虑只使用 s 的 a 而不是 a 作为变量键,并使用 a 而不是 a 作为值:Tupleboolstringboolstring

var truthTable = new Dictionary<Tuple<bool, bool, bool>, bool>
{
    { new Tuple<bool, bool, bool>(false, true, false), true },
    { new Tuple<bool, bool, bool>(false, true, true), false },
    { new Tuple<bool, bool, bool>(false, false, false), false },
    { new Tuple<bool, bool, bool>(false, false, true), false },
    { new Tuple<bool, bool, bool>(true, true, false), false },
    { new Tuple<bool, bool, bool>(true, true, true), true },
    { new Tuple<bool, bool, bool>(true, false, false), true },
    { new Tuple<bool, bool, bool>(true, false, true), true },
};

foreach (var keyValue in truthTable)
{
    // print out strings that have true value
    if (keyValue.Value)
    {
        Console.WriteLine(keyValue.Key);
    }
}

更新:将 Linq 用于Dictionary

您可以粗略地将近似为元组的 a。这意味着您可以使用任何可用的所有 Linq 功能 -- 例如,上面的循环可以使用 Linq 扩展方法并简化为以下内容:DictionaryListKeyValuePairCollectionforeachWhere

// print out strings that have true value
var trueKeyValuesList = truthTable.Where(kv => kv.Value).ToList();
foreach (var keyValue in trueKeyValuesList)
{
    Console.WriteLine(keyValue.Key);
}

在这个例子中,就是 -- (I <3 :P )。如果只是想要一个值列表,你可以使用 Linq 方法(其行为类似于 python 的 )和:trueKeyValuesListList<KeyValuePair<Tuple<bool, bool, bool>, bool>>varTupleSelectmapWhere

// print out strings that have true value
var trueValueKeys = truthTable
    .Where(kv => kv.Value)
    .Select(kv => kv.Key)
    .ToList();
foreach (var boolTuple in trueValueKeys)
{
    Console.WriteLine(boolTuple);
}

评论

0赞 Ventus 12/4/2019
嘿,邦多林很抱歉回复晚了,但这真的很好用!不过,我想开始对 foreach 循环中打印的所有元组进行布尔运算。我认为将它们放入一个新列表中,然后将元组分解将允许我这样做。但是如何将它们添加到列表中呢?
0赞 Ventus 12/4/2019
感谢您的更新,我会尝试一下。请注意,我这样做的主要原因是能够减少真值表的乘积总和。如果您对此有任何提示,我将不胜感激。再次感谢您的帮助!
0赞 Ventus 12/4/2019
好的,所以我听从了你的建议,到目前为止还不错,但我遇到了障碍。我试图通过做来减少重复项,但它没有删除任何重复项。有什么帮助吗?顺便说一句,我使用字符串元组,有点像你的第一个和第二个选项的组合。trueKeyListvar reduced1 = trueKeyList.Distinct().ToList();
0赞 Bondolin 12/4/2019
好吧,在你调用它的时候,只是一个字符串元组;当然,它们都会有所不同,因为它们从一开始就是这样,所以我有点不确定你想在那里得到什么?DistincttrueKeyListList
0赞 Ventus 12/4/2019
这是我拥有的元组示例,当我们将它们添加到列表中并打印出来时,列表中将有两个“A”和两个“!B“在列表中,我希望删除重复项,以便只有 1 个 A 和 1 个!B仍然存在。这可能吗?{new Tuple <string,string,string>("A","!B","!C"),true },{new Tuple <string,string,string>("A","!B","C"),true },