首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用于计算和减少C#中乘积和的真值表的最佳数据集

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

Stack Overflow用户
提问于 2019-12-04 00:48:14
回答 1查看 143关注 0票数 0

所以我尝试在C#中创建一个真值表,这样我就可以在上面执行一些布尔代数。假设它是一个具有8行三个变量真值表。到目前为止,我正在尝试使用数组的字符串数组来输入真值表。

代码语言:javascript
复制
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#中实现它?

EN

回答 1

Stack Overflow用户

发布于 2019-12-04 01:49:52

一种简单的实现方式是使用Dictionary<string, string>string键将保存三个变量值,第二个string值将保存相应的真值:

代码语言:javascript
复制
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);
    }
}

虽然可能更符合感兴趣的领域,但您可以考虑只使用bools的Tuple代替变量键的string,使用bool代替string作为值:

代码语言:javascript
复制
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);
    }
}

更新:为Dictionary使用Linq

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

代码语言:javascript
复制
// print out strings that have true value
var trueKeyValuesList = truthTable.Where(kv => kv.Value).ToList();
foreach (var keyValue in trueKeyValuesList)
{
    Console.WriteLine(keyValue.Key);
}

在本例中,trueKeyValuesList就是-- List<KeyValuePair<Tuple<bool, bool, bool>, bool>> (I <3 var :P )。如果只是想要一个Linq值列表,您可以将Select Tuple方法(其行为类似于PythonLinq值)与Where一起使用

代码语言:javascript
复制
// 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);
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59161761

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档